20

I want to execute few selected test cases from my class of multiple test cases using php unit with ease.

As my 1-2 test cases are failing from bunch of test cases and also finding difficult to execute whole test suite again for these two, is there any method without adding comment to the others or copying these two methods in different suite.

Thanks to all in advance

edorian
  • 38,542
  • 15
  • 125
  • 143
  • Related: [Group/Filter in phpunit doesnt execute specific test case](http://stackoverflow.com/questions/6570568/group-filter-in-phpunit-doesnt-execute-specific-test-case) – hakre Jul 04 '11 at 13:07

3 Answers3

31

You may run single test cases or single test classes from your suites using the --filter cli option:

--filter <pattern>        Filter which tests to run.

--filter runs tests whose name matches the given pattern. The pattern can be either the name of a single test or a regular expression that matches multiple test names.

Example

Take the following example test class BlaTest containing test cases testSame and testElse in file BlaTest.php:

// BlaTest.php
<?php

class BlaTest extends PHPUnit_Framework_TestCase {

    public function testSame() { $this->assertSame(1,1); }
    public function testElse() { $this->assertSame(1,1); }

}

Running all test cases within BlaTest

This filter matches the test class name.

$ phpunit --filter BlaTest

Running a single test case within BlaTest

This filter matches the test case name, then indicates to run this filter across file BlaTest.php.

$ phpunit --filter testSame BlaTest.php
John Kary
  • 6,703
  • 1
  • 24
  • 24
edorian
  • 38,542
  • 15
  • 125
  • 143
  • 1
    It performs filter depending on test case name as you did or by group naming annotations please let me know, Thanks for everything –  Jul 04 '11 at 09:08
  • 1
    @Mike On the testCase (or testClass) name afaik. For group annotations there is `--group MyGroup` – edorian Jul 04 '11 at 09:09
  • 1
    you provided an good solution and it working fine at my side thanks for that! but i have a suite that calls multiple suites and many functions for LOG,REPORT and Execution if i m trying the same 'Group or Filter' pattern its executes all the test cases. Any idea about this –  Jul 04 '11 at 09:32
5

--filter option accepts regular expression as its value (I'm using phpunit 3.7). This lets you specify the tests which will be excluded by using assertion like following: --filter='/::((?!test(Else|Same))\w+)/'

TheHippo
  • 61,720
  • 15
  • 75
  • 100
ku-
  • 126
  • 2
  • 3
2

If you prefer to filter code-wise you could mark the test to be skipped within the setUp()-method [1] by checking which test is about to be run using $this->getName(). That way these tests will show up as being skipped.

An example:

class FooTest extends PHPUnit_Framework_TestCase {

  public function setUp() {
    if( 'testIwantToSkip' === $this->getName() ) {
      $this->markTestSkipped( 'Test skipped!' );
    }
  }

  ...
}

[1] http://www.phpunit.de/manual/current/en/fixtures.html

wonk0
  • 13,402
  • 1
  • 21
  • 15