I have a PHP deployment script that I want to run PHPUnit tests first, and stop if the tests fail. I've been googling this a lot, and it's very hard to find documentation on running unit tests from php, rather than from the command line tool.
For the newest version of PHPUnit, can you do something like:
$unit_tests = new PHPUnit('my_tests_dir');
$passed = $unit_tests->run();
Preferably a solution that doesn't require me to manually specify each test suite.
working with PHPUnit 7.5:
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
$test = new TestSuite();
$test->addTestSuite(MyTest::class);
$result = $test->run();
and $result object contains lot of usefull data:
$result->errors()
$result->failures
$result->wasSuccessful()
etc...
Solution for PHP7 & phpunit ^7
use PHPUnit\TextUI\Command;
$command = new Command();
$command->run(['phpunit', 'tests']);
Does the same effect as CLI command:
vendor/bin/phpunit --bootstrap vendor/autoload.php tests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With