2014-03-04 34 views
3

我们已经在我们的代码库中实现了一些PHPUnit测试。然而,为了最小化运行测试所需的时间,我想将测试分为两组,或等级:PHPUnit中的测试级别/组

  1. 测试是非常低级别的击中测试数据库(单元测试)
  2. 测试,更高水平,然后用测试数据库进行交互(有时也被称为组件或集成测试)

这将允许我们只需运行1级测试工作时,基本功能,同时在处理更高级别的功能时运行级别1和2,或者在提交主线路/构建之前运行。

我该如何去完成这个PHPUnit?

+0

由于我们使用PHPUnit的,我们的测试都写在纯醇” PHP。我想一个想法是在测试运行之前定义()一个常量,指定所需的“测试级别”。所有的测试都可以这样写,如果它们高于要求的运行级别,它们只会返回true。 – rinogo

+0

看起来我们也可以使用'filter'命令行选项:'--filter 过滤器测试运行.'如果我们追加,例如,“单元”或“集成”我们的测试名称,我们可以表面上使用'filter'选项来指定“单元”,“集成”,或者都没有。 – rinogo

回答

4

@Schleis的回答完全正确,非常有用。唯一的问题(我承认在我的问题中没有包括)是我希望能够在我们的测试文件中分散单元和集成测试 - 我想避免有两个测试文件 - 一个文件用于单元测试对于SomeClass,还有一个单独的功能测试文件,也适用于SomeClass。我发现,使这是groups

PHPUnit命令行选项:

--group Only runs tests from the specified group(s). A test can be tagged as belonging to a group using the @group annotation.

要使用这种方法,您只需将您的测试,例如前添加@group注释的多行注释:

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'); 
    } 
} 

这使我能够做到以下几点:

  • phpunit --group Unit(只是单元测试)
  • phpunit --group Integration(只是集成测试)
  • phpunit(所有测试)
+1

使用此方法的另一个好处是'@author注释是@group的别名,允许根据作者过滤测试。“(http://phpunit.de/manual/3.7/en/textui.h​​tml# textui.clioptions) – rinogo

6

您可以编写一个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_Testsphpunit --testsuite Functional_Tests需要。

+0

非常感谢您的回复!唯一的问题是,我想对同一个测试文件(例如test/example.php)中给定的PHP源文件(例如src/example.php)进行单元测试和集成测试。你知道这种方法是否能很好地在文件中包含/排除特定的测试吗? – rinogo