Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing levels/groups in PHPUnit

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:

  1. Tests that are very low-level and don't hit the test database (unit tests)
  2. Tests that are higher level and interact with the test database (sometimes called component or integration tests)

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?

like image 828
rinogo Avatar asked Mar 04 '14 18:03

rinogo


People also ask

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 the PHPUnit XML configuration file used to organize tests?

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 do I run a single PHPUnit test?

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 unit testing WordPress?

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.


2 Answers

@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)
like image 119
rinogo Avatar answered Sep 30 '22 01:09

rinogo


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.

like image 26
Schleis Avatar answered Sep 30 '22 02:09

Schleis