Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect version of PHPUnit

Tags:

php

phpunit

I'm writing unit tests using an older version of PHPUnit (3.4) and thus can't use all supported assertions listed in manual of 3.5 and 3.6. Though I could reengineer tests for instant support in my environment here, I'd like to make my tests dependent on current version of PHPUnit, so that it's using assertsInstanceOf() as soon as my or any other one's testing environment is providing PHPUnit 3.5+.

I thought there would be some sort of constant automatically defined by PHPUnit, but I couldn't find any documentation on it.

Is there any way of achieving this without requiring definition of constant when calling on command line?

like image 381
soletan Avatar asked Jan 29 '11 15:01

soletan


People also ask

How do I run a test in PHPUnit?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

What is Codeception PHP?

Codeception is a framework used for creating tests, including unit tests, functional tests, and acceptance tests. Despite the fact that it is based on PHP, the user needs only basic knowledge for starting work with the framework, thanks to the set of custom commands offered by Codeception.


1 Answers

You can get the version of PHPUnit running using the \PHPUnit\Runner\Version[git] class and its static id() method:

$phpUnitVersion = \PHPUnit\Runner\Version::id(); # 9.5.19

And based on that - halt your tests execution or do whatever you want.


Before PHPUnit fully switched to PHP namespaces (PHPUnit 6), the old class-name was PHPUnit_Runner_Version[git].

like image 77
zerkms Avatar answered Oct 07 '22 15:10

zerkms