Skip to content
Advertisement

How can I filter by “ends with” using PHPUnit’s –filter option?

I’m trying to filter a PHPUnit test suite. When I try to filter by test name using --filter, the tests that exactly match the specified string are executed, but so are any tests that begin with the string.

eg. If I have two tests, testExample and testExampleTwo, the following runs both tests:

phpunit --filter testExample tests/Example.php

How can I get phpunit to stick to the exact string I gave it? I’ve tried /testExample$/ but that doesn’t run any tests. I’m using PHPUnit 3.7.

Advertisement

Answer

The fully-qualified test name varies depending on whether the test has a data provider.

When there is no data provider, the test’s name is simply Example::testExample, which would be executed using /testExample$/.

phpunit --filter '/testExample$/' tests/Example.php

/**
 * No data provider.
 */
public function testExample() {

}

However, with a data provider, the test becomes Example::testExample with data set [dataset], which does not match the pattern /testExample$/.

I had to use the following pattern to get it to work as expected, which matches either the end of the string or a space character.

phpunit --filter '/testExampleb/' tests/Example.php

/**
 * @dataProvider  exampleData
 */
public function testExample($data) {

}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement