uccdev
2 months ago

Best practice for Laravel test case helper functions?

Posted 2 months ago by uccdev

I'm creating test cases for my laravel project, but I find that I'm repeating a lot of code. I'd like to create helper functions that make it easier to maintain, but I'm not sure what the best practice is for going about that. At the moment my test case looks like this:

     use Tests\TestCase;
     use ...
     ...
     class MyTest extends TestCase {
     
        public function myHelperFunction($arg) {
            return $arg + some actions;
        }
     
        public function testURL() {
            $arg = "my_arg_here";
            $this->assertNotNull($this->myHelperFunction($arg));
        }
            ... //other tests that involve my helper function
     }

One problem among many with the above is that the 'myHelperFunction' is considered its own test, as opposed to something that test cases can use. I need to get around this, but I'm not sure how, without having a trail of redundant code. How best should I implement what I want to implement, would anyone recommend?

Please sign in or create an account to participate in this conversation.