Capturing the general state of PHPUnit validation at runtime

I have a PHPUnit bootstrap file that creates a test database used for unit tests related to the database and registers a shutdown function to destroy the database after the tests are completed. New database for every launch!

Problem: when tests fail, I want to save the database for debugging purposes. Currently, I have to manually disconnect my register_shutdown_function() call and then run the tests again.

If I could access the final PHPUnit success / failure state to run, I could dynamically initiate the switch-based database destruction process in the PHPUnit bootstrap file.

PHPUnit stores this information somewhere to trigger the correct source event, i.e. exit OK vs FAILURES! . However, from what I discovered, this information does not appear on the user level boot file. Has anyone ever done anything like this?

If you want to learn, here is the PHPUnit stack trace that occurs when starting PHPUnit from the command line ...

 PHP 1. {main}() /usr/bin/phpunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46 PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130 PHP 4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:139 PHP 5. PHPUnit_TextUI_Command->handleBootstrap() /usr/share/php/PHPUnit/TextUI/Command.php:620 PHP 6. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/TextUI/Command.php:867 PHP 7. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:79 PHP 8. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:95 PHP 9. [YOUR PHPUNIT BOOTSTRAP RUNS HERE] 
+4
source share
1 answer

To access the state of PHPUnit test cases, I usually recommend using:

See the docs: PHPUnit_Framework_TestListener and how to add this to the xml configuration .

A small sample:

XML file extension with this

 <listeners> <listener class="DbSetupListener" file="/optional/path/to/DbSetupListener.php"/> </listeners> 

Listener Example

 <?php class DbSetupListener implements PHPUnit_Framework_TestListener { private $setupHappend = false; public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) { $this->error = true; } public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) { $this->error = true; } public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) { } public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) { } public function startTest(PHPUnit_Framework_Test $test) { } public function endTest(PHPUnit_Framework_Test $test, $time) { } public function startTestSuite(PHPUnit_Framework_TestSuite $suite) { if(!$this->setupHappend) { $this->setupDatabase(); $this->setupHappend = true; } } public function endTestSuite(PHPUnit_Framework_TestSuite $suite) { // If you have multiple test suites this is the wrong place to do anything } public function __destruct() { if($this->error) { // Something bad happend. Debug dump or whatever } else { // Teardown } } } 

This should make you move quite easily. If you only need to “listen” for specific tests or testsuites , then you can use the startTest and startTestSuite .

Both objects have a getName() method, which gives the name of the test suite and test case, respectively.

+5
source

Source: https://habr.com/ru/post/1436392/


All Articles