我正在将单元测试添加到以前从未有过的旧Symfony 2.8生产代码库中,因此可以将Symfony升级到维护版本。由于我们当前正在运行的PHP 5.5也将不再维护,因此我们还将升级PHP,但是如果Symfony 2.8在PHP 7.3下运行,则会抛出太多错误。
尽管我能够在PHPUnit和Codeception(由PHPUnit提供支持)下运行单元测试,但是它们的运行速度却非常慢。
例如:
SimpleTest.php
<?php
class SimpleTest extends PHPUnit_Framework_TestCase {
public function testShouldDoNothing() {
$this->assertTrue(true);
}
}
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<php>
<server name="KERNEL_DIR" value="app/"/>
</php>
<testsuites>
<testsuite name="Application Test Suite">
<directory>tests/unit</directory>
</testsuite>
</testsuites>
</phpunit>
PHPUnit运行输出
PHPUnit 4.8.36 by Sebastian bergmann and contributors.
Runtime: PHP 5.5.9 with Xdebug 2.5.5
.
Time: 19.52 seconds,Memory: 6.00MB
OK (1 test,1 assertion)
codeception.yml
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
coverage:
enabled: true
include:
- src/*
low_limit: 50
high_limit: 100
代码接收运行输出
Codeception PHP Testing Framework v2.3.9
Powered by PHPUnit 4.8.36 by Sebastian bergmann and contributors.
Unit Tests (1) --------------------------------------------------
Testing unit
+ SimpleTest: Should do nothing (0.29s)
-----------------------------------------------------------------
Time: 30.96 seconds,Memory: 23.25MB
OK (1 test,1 assertion)
此测试应始终在0.00s
中运行,并且总时间应少于5 seconds
,而不是20 seconds
或更多。
如何在PHP 5.5中的Symfony 2.8下加速单元测试?
我已将问题归结为运行PHP 5.5,目前这是必需的。我用PHP 5.5创建了一个普通的Symfony 2.8安装,没有代码,测试仍然非常慢。当我切换到PHP 7.3时,测试的速度与过去预期的一样快,但是,我们的项目尚未在PHP 7.3下运行,因为它会导致致命错误。
D:\server\Apache24\htdocs\symfony-2.8>php55 vendor\codeception\codeception\codecept run unit
Codeception PHP Testing Framework v2.3.9
Powered by PHPUnit 4.8.36 by Sebastian bergmann and contributors.
Unit Tests (1) --------------------------------------------------
+ SimpleTest: Should do nothing (0.20s)
-----------------------------------------------------------------
Time: 21.84 seconds,Memory: 14.50MB
OK (1 test,1 assertion)
D:\server\Apache24\htdocs\symfony-2.8>php73 vendor\codeception\codeception\codecept run unit
Codeception PHP Testing Framework v2.3.9
Powered by PHPUnit 4.8.36 by Sebastian bergmann and contributors.
Unit Tests (1) --------------------------------------------------
+ SimpleTest: Should do nothing (0.00s)
-----------------------------------------------------------------
Time: 264 ms,Memory: 10.00MB
OK (1 test,1 assertion)