We already have some PHPUnit tests implemented on our codebase. However, moving forward, in the interest of minimizing the time required to run tests, I'd like to separate out our tests into two groups, or levels:
This would allow us to just run the level 1 test when working on basic functionality, while running both level 1 and 2 when working on higher-level functionality, or before committing to the main line/build.
How would I go about accomplishing this with PHPUnit?
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.
The <testsuites> Element This element is the root for one or more <testsuite> elements that are used to configure the tests that are to be executed.
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.
Unit testing is a level of software testing where individual units/components of software are tested. The purpose of unit tests is to validate that each unit of software performs as designed. In terms of PHP and WordPress, a single 'unit' is a function or a class.
@Schleis' answer is entirely correct and very useful. The only catch (which I admittedly didn't include in my question) is that I'd like to be able to scatter Unit and Integration tests throughout our test files - I want to avoid having two files of tests - one file for Unit tests for SomeClass, and a separate file for Functional tests, also for SomeClass.
The PHPUnit command-line option that I found that enables this is groups:
--group
Only runs tests from the specified group(s). A test can be tagged as belonging to a group using the @group annotation.
To use this approach, you simply add the @group
annotation to the multiline comment before your tests as such:
class SomeClassTest extends PHPUnit_Framework_TestCase {
/**
* @group Unit
*/
public function testSomeUnitFunctionality() {
$this->assertEquals(xyz(' .a1#2 3f4!', true), ' 12 34');
}
/**
* @group Integration
*/
public function testSomeIntegrationFunctionality() {
$this->assertEquals(xyz(' .a1#2 3f4!', true), ' 12 34');
}
}
This allows me to do the following:
phpunit --group Unit
(Just unit tests)phpunit --group Integration
(Just integration tests)phpunit
(All tests)You can write a phpunit.xml
that will seperate your tests into test suites that you can run separately.
http://phpunit.de/manual/current/en/organizing-tests.html#organizing-tests.xml-configuration
It would look similar to:
<phpunit>
<testsuites>
<testsuite name="Unit_Tests">
<directory>SomeTests</directory>
<directory>MoreTests</directory>
</testsuite>
<testsuite name="Functional_Tests">
<directory>OtherTests</directory>
</testsuite>
</testsuites>
</phpunit>
Then when you want to run only one group of tests you would call phpunit --testsuite Unit_Tests
or phpunit --testsuite Functional_Tests
as needed.
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