您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit标注-runInSeparateProcess
发布时间:2021-10-07 18:58:18编辑:雪饮阅读()
runInSeparateProcess标注用于将一个测试方法可以独立运行在一个进程中。
首先看看没有runInSeparateProcess标注情况下每个测试方法所获取的进程id是如何?
MyTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
public function testInSeparateProcess(): void
{
//posix_getpid()用于获取当前进程id
var_dump("\r\n".posix_getpid()."\r\n");
}
public function testTwo(): void
{
var_dump("\r\n".posix_getpid()."\r\n");
}
}
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
public function testInSeparateProcess(): void
{
//posix_getpid()用于获取当前进程id
var_dump("\r\n".posix_getpid()."\r\n");
}
public function testTwo(): void
{
var_dump("\r\n".posix_getpid()."\r\n");
}
}
运行结果:
[root@localhost ~]# /usr/local/php734/bin/php /usr/local/phpunit-9.5.8.phar /usr/local/organizing/tests/MyTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
Rstring(8) "
4368
"
R 2 / 2 (100%)string(8) "
4368
"
Time: 00:00, Memory: 18.00 MB
There were 2 risky tests:
1) MyTest::testInSeparateProcess
This test did not perform any assertions
/usr/local/organizing/tests/MyTest.php:6
2) MyTest::testTwo
This test did not perform any assertions
/usr/local/organizing/tests/MyTest.php:11
OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 0, Risky: 2.
可以看到两个测试方法获取到的当前进程id都是4368。
接下来再看看有runInSeparateProcess标注时候每个测试方法获取到的进程id是如何?
MyTest2.php:
use PHPUnit\Framework\TestCase;
final class MyTest2 extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testInSeparateProcess(): void
{
var_dump("\r\n".posix_getpid()."\r\n");
}
/**
* @runInSeparateProcess
*/
public function testTwo(): void
{
var_dump("\r\n".posix_getpid()."\r\n");
}
}
运行结果:
[root@localhost ~]# /usr/local/php734/bin/php /usr/local/phpunit-9.5.8.phar /usr/local/organizing/tests/MyTest2.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
Rstring(8) "
5272
"
R 2 / 2 (100%)string(8) "
5273
"
Time: 00:00.178, Memory: 18.00 MB
There were 2 risky tests:
1) MyTest2::testInSeparateProcess
This test did not perform any assertions
/usr/local/organizing/tests/MyTest2.php:9
2) MyTest2::testTwo
This test did not perform any assertions
/usr/local/organizing/tests/MyTest2.php:16
OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 0, Risky: 2.
这次每个测试方法获取到的当前进程id都是不同的。这就是runInSeparateProcess标注实现了每个测试方法运行于一个独立的进程中。
补充:posix_getpid方法在windows上面不可用。要在linux上面才能使用。
关键字词:phpunit,runInSeparateProcess