Testing Levels / Groups in PHPUnit

We already have some PHPUnit tests implemented on our code base. However, moving forward, in the interest of minimizing the time required to run the tests, I would like to single out our tests into two groups or levels:

  • Tests that are very low and do not fall into the test database (unit tests)
  • Tests that are higher level and interact with the test database (sometimes called test or integration tests)

This will allow us to simply run a level 1 test when working with basic functions while simultaneously starting both levels 1 and 2 when working with higher-level functions or before moving on to the main line / assembly.

How can I do this using PHPUnit?

+4
source share
2 answers
Answer to

@Schleis is completely correct and very useful. The only catch (which I admittedly did not include in my question) is that I would like to scatter Unit and Integration tests in our test files - I want to avoid two test files - one file for unit tests for SomeClass and separate file for functional tests, also for SomeClass.

The PHPUnit command-line option I found that allows these 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 an annotation @groupto the multi-line 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 (Unit tests only)
  • phpunit --group Integration (Integration tests only)
  • phpunit (All tests)
+5

phpunit.xml, , .

http://phpunit.de/manual/current/en/organizing-tests.html#organizing-tests.xml-configuration

:

<phpunit>
  <testsuites>
    <testsuite name="Unit_Tests">
      <directory>SomeTests</directory>
      <directory>MoreTests</directory>
    </testsuite>
    <testsuite name="Functional_Tests">
      <directory>OtherTests</directory>
    </testsuite>
  </testsuites>
</phpunit>

, , phpunit --testsuite Unit_Tests phpunit --testsuite Functional_Tests .

+6

Source: https://habr.com/ru/post/1530079/


All Articles