【问题标题】:Codeception failing tests take too long to runCodeception 失败的测试需要很长时间才能运行
【发布时间】:2016-06-25 02:42:50
【问题描述】:

我刚刚在我的一些项目中添加了 Codeception,但它的性能对于失败的测试来说是不可接受的。通过测试在 0.1 秒或更短时间内执行,但失败的测试需要 15 分钟到 1.5 小时每个。当它们最终失败时,它们会将数百千字节的数据转储到控制台。有很多提到 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-&gt;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.

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

【问题讨论】:

  • 我几乎 100% 确定这是您的代码而不是 Codeception 的错误,请添加失败的测试、unit.suite.yml、country.model.php 和所有依赖项(如果有)任何。此外,最好知道将哪些信息转储到控制台。
  • 如果是 Codeception 的错误,$this->assertTrue(false);也需要 15 分钟,对吧?
  • @Naktibalda 很好的建议!是的,$this-&gt;assertTrue(false); 也需要很长时间。完成后我会通知您。
  • 你是如何创建一个空白目录的?
  • 我认为调查此问题的最佳方法是使用 xdebug 对其进行分析。

标签: unit-testing codeception


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2021-06-15
    • 2011-01-17
    相关资源
    最近更新 更多