Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-run last failed test in PHPUnit

You may use --stop-on-failure flag to break the unit testing when one of the tests fails.

Is there any way quick way to tell PHPUnit to re-run this failed test, instead providing the full path manually?

like image 663
Sfisioza Avatar asked Aug 19 '11 09:08

Sfisioza


2 Answers

Since PHPUnit 7.3, you can cache the results of your tests, then order your tests by defects.

In phpunit.xml, enable cacheResults:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit cacheResult="true"
         ...>

If you don't want to edit your phpunit.xml, you could also run your tests with the --cache-result flag.

When caching results, PHPUnit will create a .phpunit.result.cache file after running tests. Make sure to add this file to your global gitignore file.

You can run your tests like this to run previously failed tests first:

phpunit --order-by=defects --stop-on-failure
like image 100
Sjors Ottjes Avatar answered Sep 27 '22 22:09

Sjors Ottjes


Take a look at the --filter cli option. You can find an example in the organisation docs and in the CLI Docs.

--filter

Only runs tests whose name matches the given pattern. The pattern can be either the name of a single test or a regular expression that matches multiple test names.

Assume your run phpunit Tests/ and Tests/Stuff/ThatOneTestClassAgain::testThisWorks fails:

your options are:

phpunit --filter ThatOneTestClassAgain

and

phpunit --filter testThisWorks

or most other strings that somehow make sense

like image 28
edorian Avatar answered Sep 28 '22 00:09

edorian