Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uccdev's avatar

Best practice for Laravel test case helper functions?

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?

0 likes
5 replies
JohnBraun's avatar
Level 33

There are multiple options

Keeping them in the same test file

One way to get around this is using annotation to separate your test methods from your other methods. Aditionally, you can make the helper protected or private.
     class MyTest extends TestCase {
     
        protected function myHelperFunction($arg) {
            return $arg + some actions;
        }
     
        /** @test */
        public function put_a_descriptive_method_name_here() {
            $arg = "my_arg_here";
            $this->assertNotNull($this->myHelperFunction($arg));
        }
            ... //other tests that involve my helper function
     }

Put the helpers in the TestCase class

You can put the methods on the base TestCase class if you'd like
abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function setUp()
    {
        parent::setUp();
        //....
    }

    protected function myHelperFunction($arg)
    {
        return $arg + some actions;
    }
}

Extract them to (e.g.) a helpers.php file

A different option is to put them in a helpers.php file in your tests directory, which you autoload using composer.

In composer.json:

    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        },
        "files": [
            "tests/helpers.php"
        ]
    },

Don't forget to dump the autoload using

composer dump-autoload
3 likes
uccdev's avatar

John, thank you very much.

Method #1 works perfectly fine. I'm curious about method #2: how exactly would "CreatesApplication" look like, then, if the helper methods are inside the TestCase class?

JohnBraun's avatar

@uccdev

In method 2 we're not modifying any behaviour (of "CreatesApplication"), just adding a function to the TestCase class.

Next, in your unit/feature tests you extend (and thereby inherit from) that TestCase class. Therefore, the method will be available in all classes (tests) that extend the TestCase class.

DLUK44's avatar

If using method 3 suggested by @johnbraun above, how would one call the helper functions from the Test?

I've got this and it seems to work. Is this the correct way to go about including helper functions in tests?

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ShopTest extends TestCase
{
    public $helpers;

    public function setUp()
    {
        parent::setUp();

        $this->helpers = new \Tests\MyHelpers;

        $something = $this->helpers->doSomething();

    }

public function test_something_happens()
{
        $this->assertEquals ("Something", $something);

namespace Tests;

class MyHelpers extends TestCase {

//doSomething() will be used by many tests so is in it's own file.
function doSomething()
{
        return ("Something");
}

Is it right to have the help function extend TestCase?

is it right to make a new Helper class in the test?

Thanks David.

JohnBraun's avatar

hi @dluk44

If you're registering the helpers file (e.g. tests/helpers.php) using the PSR-4 autoload key, you can just use all defined functions in that file. No need to extend anything. I listed an example below.

Example for tests/helpers.php:

<?php

  function doDomething()
  {
    return 'doing something';
  }

In your test:

class ShopTest extends TestCase
{
  public function testItDoesSomething()
  {
    $this->assertEquals('doing something', doSomething());
  }
}

However, in your example I would create a new directory Helpers in your tests directory and create a new TestHelper class, which offers the desired methods. Depending on your exact use case, you can declare them static and access them in your tests as TestHelper::doSomething(). Do not forget to import the TestHelper class in your test.

<?php

namespace Tests\Helpers;

class TestHelper
{
    public static function doSomething(): string
    {
        return 'doing something';
    }
}

In your test:

class ShopTest extends TestCase
{
  public function testItDoesSomething()
  {
        $this->assertEquals('doing something', TestHelper::doSomething());
  }
}
1 like

Please or to participate in this conversation.