【问题标题】:How can I skip running slow tests when running a particular test suite in PHPUnit, but still run all tests when I need full code coverage?如何在 PHPUnit 中运行特定测试套件时跳过运行缓慢的测试,但在需要完整代码覆盖时仍运行所有测试?
【发布时间】:2020-01-25 00:50:27
【问题描述】:

我有一个测试套件,在我的 PHPUnit 配置中标记为 so。测试套件包含各种许多测试,并且它还具有数据库密集型实时数据库测试,这些测试需要很长时间才能完成。

特别是其中一项测试需要 2 秒以上才能完成(见下文)。

我想分开运行快速测试和慢速测试,这样当我有更多时间时,我可以运行完整的慢速但完整的测试版本,但总的来说我想运行快速测试满足我的日常需求,从而在运行测试套件时省略了缓慢的测试。

我该怎么做?

为了记录,我的 phpunit.xml 配置是这样的:

<phpunit bootstrap="bootstrap.php">
    <testsuite name="Crating">
        <directory>../module/Crating/test/</directory>
    </testsuite>
</phpunit>

我用来运行我的测试套件的命令是这样的:

phpunit -c phpunit.xml --testsuite CratingCalc

我的../module/Crating/test/ 目录中的一个文件是CrateRepositoryTest.php。看起来是这样的:

class CrateRepositoryTest extends TestCase
{

    function testCombine()
    {
        //mocked up hardcoded data
        $fake = new FakeCratingDataModel();

        //connection to real live database
        $real = new CratingDataModel();    

        /* 
         * Tests that verify mocked up data to match live data
         * Purpose to have them is to alert me when live database data or schema change
         */
        $this->assertEquals($fake->getContentsBySalesOrderNumber(7777), $real->getContentsBySalesOrderNumber(7777));
        $this->assertEquals($fake->getContentsByShopJobNumber(17167), $real->getContentsByShopJobNumber(17167));
        $this->assertEquals($fake->getNearCrating(20, 20, 20), $real->getNearCrating(20, 20, 20));
        $this->assertEquals($fake->getContentsByInquiryNumber(640, 2), $real->getContentsByInquiryNumber(25640, 2));
    }
}

【问题讨论】:

    标签: unit-testing configuration phpunit


    【解决方案1】:

    组。

    通常,您可以添加注释 @group small 或者我有 @group ci(仅用于我将在完整 CI 环境中运行的东西)。

    小型、中型或大型测试实际上很常见,有专门的组注释 - @small, @medium &amp; @large,还有 phpunit.xml 文件的设置也可以为每个设置一个时间限制(并且会杀死并失败他们,如果他们花费的时间太长):

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      ....
      timeoutForLargeTests="5"
      timeoutForMediumTests="2"
      timeoutForSmallTests="1"
      .... >
    

    我有两种运行测试的方法 - 不排除任何组的完整版本(运行 1250 次测试大约需要 50 秒,没有覆盖),以及添加 @987654325 的更快测试@ 到 phpunit 命令,它可以在 4 秒内运行 630 个测试。

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 2015-05-20
      • 2011-05-18
      • 2020-06-14
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多