您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit-phpunit.xml-testsuites、testsuite
发布时间:2021-10-17 23:26:21编辑:雪饮阅读()
<testsuites>
元素
父元素:<phpunit>
此元素是一个或多个 <testsuite>
元素的根元素,用于配置需要执行的测试。
<testsuite> 元素
父元素:<testsuites>
<testsuite> 元素必须拥有 name 属性,可以有一个或多个 <directory> 及 <file> 子元素,分别代表需要搜索测试的目录及元素。
<testsuites>
<testsuitename="unit">
<directory>tests/unit</directory>
</testsuite>
<testsuitename="integration">
<directory>tests/integration</directory>
</testsuite>
<testsuitename="edge-to-edge">
<directory>tests/edge-to-edge</directory>
</testsuite>
</testsuites>
可以用 phpVersion 和 phpVersionOperator 属性来指定 PHP 版本需求:
指定php版本的需求可以看看如我这样的:
<phpunit bootstrap="src/autoload.php">
<testsuites>
<testsuite name="demo">
<directory phpVersion="7.4.3" phpVersionOperator=">=">tests/test1</directory>
</testsuite>
<testsuite name="demo2">
<directory>tests/test2</directory>
</testsuite>
</testsuites>
</phpunit>
这里稍微解释下demo要对应在src目录中有demo.php
同理demo2也要在src目录中有demo2.php
那么我这里demo和demo2.php如:
C:\Users\Administrator>type D:\organizing\src\demo.php D:\organizing\src\demo2.php
D:\organizing\src\demo.php
<?php
?>
D:\organizing\src\demo2.php
<?php
?>
在上面的示例中,仅当 PHP 版本至少为 7.4.3 时,才会将
tests/test1
目录中的测试添加到测试套件中。phpVersionOperator
属性是可选的,默认为 >=
。接下来就是对应要用tests/test1中的一个测试用例类和tests/test2中的一个测试用例类,且这两个测试用例类的文件名都是以***Test.php这样,而且类名不能重复。
则我这里这两个类如:
C:\Users\Administrator>type D:\organizing\tests\test1\MyTest.php D:\organizing\tests\test2\MyTwoTest.php
D:\organizing\tests\test1\MyTest.php
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
public function testEmpty()
{
sleep(1);
$this->assertTrue(true);
}
public function testPush()
{
sleep(2);
$this->assertTrue(true);
}
public function testPop()
{
sleep(3);
$this->assertTrue(true);
}
}
D:\organizing\tests\test2\MyTwoTest.php
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class MyTwoTest extends TestCase
{
public function testEmpty()
{
sleep(1);
$this->assertTrue(true);
}
public function testPush()
{
sleep(2);
$this->assertTrue(true);
}
public function testPop()
{
sleep(3);
$this->assertTrue(true);
}
}
那么第一个testsuite的第一个测试用例类目录要求php版本是7.4.3.
而第二个testsuite的第一个测试用例类目录对php版本没有要求。
所以运行结果如:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar -c D:\organizing\phpunit.xml --testsuite demo
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
No tests executed!
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar -c D:\organizing\phpunit.xml --testsuite demo2
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 00:06.017, Memory: 18.00 MB
OK (3 tests, 3 assertions)
也就是由于第一个testsuite的第一个测试用例类目录要求php版本是7.4.3而实际上以php7.3.4运行则不满足,所以就显示0个tests。
而第二个testsuite的第一个测试用例类目录对php版本没有要求。
所以其tests都被运行测试了。
那么更换为php7.4.3则上面两个测试用例类其对应目录限制就都满足了,所以也就是说都可以运行了:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar -c D:\organizing\phpunit.xml --testsuite demo2
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 00:06.018, Memory: 18.00 MB
OK (3 tests, 3 assertions)
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar -c D:\organizing\phpunit.xml --testsuite demo
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 00:06.041, Memory: 18.00 MB
OK (3 tests, 3 assertions)
不过说句实话,一般php版本限制这种很少有实际业务需求的,一般都是手动去配置环境吧,至少我还没有自己写过关于php版本这块儿判断兼容的类似业务逻辑代码的。
关键字词:phpunit,testsuite
相关文章
- phpunit-phpunit.xml-phpunit-testdox
- phpunit-phpunit.xml-phpunit-executionOrder
- phpunit-phpunit.xml-phpunit-stderr
- phpunit-phpunit.xml-phpunit-verbose
- phpunit-phpunit.xml-phpunit-defaultTestSuite
- phpunit-phpunit.xml-phpunit-testSuiteLoaderClass与
- phpunit-phpunit.xml-phpunit-timeoutForLargeTests
- phpunit-phpunit.xml-phpunit-timeoutForMediumTests
- phpunit-phpunit.xml-phpunit-timeoutForSmallTests
- phpunit-phpunit.xml-phpunit的enforceTimeLimit与def