How to run multiple PHPUnit tests?


by brandy , in category: PHP General , a year ago

How to run multiple PHPUnit tests?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by kadin , a year ago


To run multiple PHPUnit tests, you can follow these steps:

  1. Create separate test classes for each test case, or group related test cases in the same test class.
  2. Ensure that each test class extends the PHPUnitFrameworkTestCase class.
  3. Create a test suite that includes all the test classes or groups you want to run. You can do this by creating a new class and implementing the PHPUnitFrameworkTestSuiteProvider interface, or by using the @group annotation to group the tests. Example using a test suite class: use PHPUnitFrameworkTestSuite; final class MyTestSuite { public static function suite() { $suite = new TestSuite('My Test Suite'); $suite->addTestSuite(MyTestClass1::class); $suite->addTestSuite(MyTestClass2::class); // Add more test classes return $suite; } } Example using the @group annotation: /** * @group group1 */ class MyTestClass1 extends TestCase { // Test methods } /** * @group group2 */ class MyTestClass2 extends TestCase { // Test methods }
  4. Run the PHPUnit command, specifying the test suite class or the group(s) you want to run: phpunit MyTestSuite.php or phpunit --group group1,group2 This will run all the tests included in the test suite class or the specified test group(s).

You can also use wildcards to select test files based on a pattern:

phpunit tests/Unit/*

This will run all the tests in the tests/Unit directory.

By following these steps, you can easily run multiple PHPUnit tests using either test suites or test groups.