Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you run PHPUnit tests from a script?

Tags:

php

phpunit

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.

like image 642
Charles Avatar asked Mar 08 '13 20:03

Charles


2 Answers

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...

like image 67
myxaxa Avatar answered Sep 30 '22 03:09

myxaxa


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
like image 40
Budkovsky Avatar answered Sep 30 '22 05:09

Budkovsky