How to run a single PHPUnit test?

by ryan.murray , in category: PHP General , 9 months ago

How to run a single PHPUnit test?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 8 months ago

@ryan.murray 

To run a single PHPUnit test, you can follow these steps:

  1. Open your command-line interface (e.g., Terminal, Command Prompt).
  2. Navigate to the root directory of your project where the phpunit.xml or phpunit.xml.dist file resides.
  3. Run the following command, replacing MyTest with the name of the specific test class or method you want to run:
1
phpunit --filter MyTest


Note: If your test class is in a namespace, you need to include the namespace as well. For example, if your test class MyTest is in the namespace AppTests, the command should be:

1
phpunit --filter AppTestsMyTest


  1. PHPUnit will execute the specified test(s) and display the results in the command-line interface.


You can also use the --testdox option to display a more readable output of the test results:

1
phpunit --filter MyTest --testdox


By specifying a particular test class or method with the --filter option, you can run just that specific test instead of running all the tests in your test suite.