您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit对输出进行测试
发布时间:2021-09-14 12:47:36编辑:雪饮阅读()
有时候希望断言预期某个方法有一些输出,而不是具体返回值。
则可以像是这样:
OutputTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class OutputTest extends TestCase
{
public function testExpectFooActualFoo(): void
{
$this->expectOutputString('foo');
print 'foo';
}
public function testExpectBarActualBaz(): void
{
$this->expectOutputString('bar');
print 'ba';
}
}
use PHPUnit\Framework\TestCase;
final class OutputTest extends TestCase
{
public function testExpectFooActualFoo(): void
{
$this->expectOutputString('foo');
print 'foo';
}
public function testExpectBarActualBaz(): void
{
$this->expectOutputString('bar');
print 'ba';
}
}
运行结果如:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar C:\Users\Administrator\PhpstormProjects\untitled\OutputTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.003, Memory: 20.00 MB
There was 1 failure:
1) OutputTest::testExpectBarActualBaz
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'bar'
+'ba'
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
可以看到这里第一个断言是没有问题的预期结果与实际输出结果一致,而第二个断言预期输出为bar,而实际输出为ba,所以第二个断言就不正确了。
关键字词:phpunit,输出