skip to Main Content

I’m using the RequiresFunction attribute in a few of my tests. I want to pass arguments to phpunit that will run the tests with that attribute and the name of the required function (if possible).

Tests look something like this:

use PHPUnitFrameworkAttributesRequiresFunction;
use PHPUnitFrameworkAttributesTest;
use TestsTestCase;

class MyCustomFunctionTest extends TestCase
{
    #[Test]
    #[RequiresFunction('some_function_name')
    public function some_function_name_destroys_the_planet(): void
    {
        $this->assertTrue(true);
    }
}

2

Answers


  1. If you want to run tests that require a specific function (like some_function_name), you can use PHPUnit’s --filter option. However, the --filter option works with the names of the test methods or classes, not directly with attributes like RequiresFunction.

    This command will run all tests whose names include some_function_name.

    phpunit --filter '/some_function_name/'
    
    Login or Signup to reply.
  2. If you have multiple tests for different functions and you want more control, you can use the @group annotation in your test methods and then run PHPUnit with the --group option.

    PHP:

    #[Test]
    #[Group('some_function_group')]
    #[RequiresFunction('some_function_name')]
    public function testSomeFunctionName(): void
    {
        $this->assertTrue(true);
    }
    

    Bash:

    phpunit --group some_function_group
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search