Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a script after phpunit tests finished

Tags:

phpunit

Can I run a PHP script automatically after all PHPUnit tests are finished?

I'd like to report some non-fatal issues (i.e. correct but suboptimal test results) after all tests are finished.

like image 951
Martijn Avatar asked Jul 02 '26 01:07

Martijn


2 Answers

Assuming you can detect such sub-optimal results, PHPUnit (v9 and before) has a 'TestListener' facility, that can be extended with custom code and enabled in the phpunit.xml file. It can run code before and after tests, and for each potential result (pass/fail/errors, etc, etc).

PHPUnit 10 replaces the TestListener with a new events system.

like image 181
Alister Bulman Avatar answered Jul 05 '26 15:07

Alister Bulman


Another way to do this is to call a bootstrap script for PHPUnit. Inside this file, you can use the function register_shutdown_function, which will be executed once when all tests are completed.

On the command line, it would look something like this:

phpunit --bootstrap "path/to/bootstrap.php"

And in the bootstrap.php file, you define something like:

register_shutdown_function(function () {
    echo "This code was called after all tests were executed"; 
});
like image 41
ledark Avatar answered Jul 05 '26 16:07

ledark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!