Codeception 失败测试花费的时间太长 运行

Codeception failing tests take too long to run

我刚刚将 Codeception 添加到我的一些项目中,但它的性能对于失败的测试来说是不可接受的。通过测试在 0.1 秒或更短时间内执行,但失败测试需要 15 分钟到 1.5 小时 每个。当他们最终失败时,他们将数百 KB 的数据转储到控制台。有很多提到 Selenium,我不明白,因为到目前为止我只使用单元测试,而不是验收测试或功能测试,所以我不明白为什么它需要 Selenium。为什么这么慢?有没有我可以配置的东西使失败的测试 运行 和通过的测试一样快?这些是我正在使用的测试:

<?php

class englishTest extends \Codeception\Test\Unit {
  /**
   * @var \UnitTester
   */
  protected $tester;

  protected function _before() {
    include_once('country.model.php');
  }

  protected function _after() {
  }

  // tests
  public function testCreateCountryModel() {
    $country = new CountryModel;
    $this->assertEquals('Afghanistan', $country->get_all_countries()[0]);
  }

  public function testGetCountriesEndingWithEndOfWord() {
    $country = new CountryModel;
    $this->assertEquals(['Costa Rica', 'Dominica', 'Jamaica', 'South Africa'], $country->get_countries_ending_with('ca'));
  }

}

这是测试通过后的输出:

C:\server\Apache24\htdocs\localhost\public_html\english>codecept run
Codeception PHP Testing Framework v2.2.1
Powered by PHPUnit 4.8.26 by Sebastian Bergmann and contributors.

Acceptance Tests (0) ---------------------------------------
------------------------------------------------------------

Functional Tests (0) ---------------------------------------
------------------------------------------------------------

Unit Tests (2) ---------------------------------------------
+ englishTest: Create country model (0.1s)
+ englishTest: Get countries ending with end of word
------------------------------------------------------------


Time: 2.23 seconds, Memory: 11.00MB

OK (2 tests, 2 assertions)

编辑

我创建了一个空白目录:

C:\server\Apache24\htdocs\localhost\public_html>md deception

我引导了 Codeception:

C:\server\Apache24\htdocs\localhost\public_html>codecept bootstrap deception
 Initializing Codeception in C:\server\Apache24\htdocs\localhost\public_html\deception

File codeception.yml created       <- global configuration
tests/unit created                 <- unit tests
tests/unit.suite.yml written       <- unit tests suite configuration
tests/functional created           <- functional tests
tests/functional.suite.yml written <- functional tests suite configuration
tests/acceptance created           <- acceptance tests
tests/acceptance.suite.yml written <- acceptance tests suite configuration
 ---
tests/_bootstrap.php written <- global bootstrap file
Building initial Tester classes
Building Actor classes for suites: acceptance, functional, unit
 -> AcceptanceTesterActions.php generated successfully. 0 methods added
\AcceptanceTester includes modules: PhpBrowser, \Helper\Acceptance
AcceptanceTester.php created.
 -> FunctionalTesterActions.php generated successfully. 0 methods added
\FunctionalTester includes modules: \Helper\Functional
FunctionalTester.php created.
 -> UnitTesterActions.php generated successfully. 0 methods added
\UnitTester includes modules: Asserts, \Helper\Unit
UnitTester.php created.

Bootstrap is done. Check out C:\server\Apache24\htdocs\localhost\public_html\deception/tests directory

然后我切换到目录,运行 在其中执行以下命令:

C:\server\Apache24\htdocs\localhost\public_html\deception>codecept generate:test unit Deception
Test was created in C:\server\Apache24\htdocs\localhost\public_html\deception\tests\unit\DeceptionTest.php

然后我在 DeceptionTest.php 中添加了一行 ($this->assertTrue(false);):

<?php


class DeceptionTest extends \Codeception\Test\Unit
{
  /**
   * @var \UnitTester
   */
  protected $tester;

  protected function _before()
  {
  }

  protected function _after()
  {
  }

  // tests
  public function testMe()
  {
    $this->assertTrue(false);
  }
}

最后,我 运行 以下内容:

C:\server\Apache24\htdocs\localhost\public_html\deception>codecept run
Codeception PHP Testing Framework v2.2.1
Powered by PHPUnit 4.8.26 by Sebastian Bergmann and contributors.

Acceptance Tests (0) ---------------------------------------
------------------------------------------------------------

Functional Tests (0) ---------------------------------------
------------------------------------------------------------

Unit Tests (1) ---------------------------------------------
x DeceptionTest: Me
------------------------------------------------------------


Time: 3.36 minutes, Memory: 103.25MB

There was 1 failure:

---------
1) DeceptionTest: Me
 Test  tests\unit\DeceptionTest.php:testMe
Failed asserting that false is true.
#1  C:\server\Apache24\htdocs\localhost\public_html\deception\tests\unit\DeceptionTest.php:22
#2  DeceptionTest->testMe
#3  C:\usr\bin\codecept.phar:7

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

如您所见,计算 3.36 分钟103.25MB true !== false.

我总是将 Xdebug 与 PHP 一起使用。凭直觉我禁用了它,突然之间,Codeception 完成了所有测试,通过和失败都在几秒钟内完成。罪魁祸首是 php.ini 中的 xdebug.collect_params = 3。将其设置为 0、1 或 2 中的任何一个都可以解决问题。