2

I am trying to get the results of a php unit tests folder , but not using the CLI, and instead using a php file.

I would like to get the answer ( OK, 4 tests passed ) in a variable or something so I can decide whether the script should execute or not, what's the best way to do this? i don't want to use batch files, I want to force the execution of the tests, inside the library itself.

I started with

require_once 'PHPUnit/Autoload.php';

When I included the tests, don't know how to start them thought.

Any advice?

1 Answer 1

2

You can check PHPUnits exit code, 0 means no test failed.

To run the tests from a php file, then to check the exit code, try something like this:

//Composer installs phpunit to /vendor/bin/phpunit
exec('/vendor/bin/phpunit', $result, $exitCode);

if ($exitCode == 0) {
  // continue exiting the script  
} else {
  // there was a test failure, more info will be in $result
}

If you're looking for an enterprise quality solution, look into a Continuous Integration product like Jenkins or Bamboo.

Sign up to request clarification or add additional context in comments.

4 Comments

many thanks, I'll look at the continous integration frameworks
They can have a bit of a learning curve, but very worth it if you want automated deployments. I'd go with Jenkins if you are working with a small budget, and Bamboo if you're already using JIRA.
I just reread your question and was able to provide a little more information.
Note that $result simply contains the output of the command, whether or not the command exited successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.