您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertJsonFileEqualsJsonFile
发布时间:2021-10-04 18:35:05编辑:雪饮阅读()
assertJsonFileEqualsJsonFile断言用于对文件内容进行断言,内容一致则断言成功。
不过该方法名中有包含“Json”,按理来说和json应该有点关系,但实际上官方文档中就我目前所了解也就只说了大概就是文件内容相同,没有额外的其它说明。
接下来看看我们的实例:
JsonFileEqualsJsonFileTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class JsonFileEqualsJsonFileTest extends TestCase
{
public function testFailure(): void
{
$this->assertJsonFileEqualsJsonFile('path/to/fixture/file', 'path/to/actual/file');
}
public function testSuccess(): void
{
$this->assertJsonFileEqualsJsonFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.txt', 'D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\2.txt');
}
}
use PHPUnit\Framework\TestCase;
final class JsonFileEqualsJsonFileTest extends TestCase
{
public function testFailure(): void
{
$this->assertJsonFileEqualsJsonFile('path/to/fixture/file', 'path/to/actual/file');
}
public function testSuccess(): void
{
$this->assertJsonFileEqualsJsonFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.txt', 'D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\2.txt');
}
}
那么这里正确断言所参与的两个文件内容:
C:\Users\Administrator>type D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.txt
1
C:\Users\Administrator>type D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\2.txt
1
我故意都写成一样的。
那么接下来看看运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\JsonFileEqualsJsonFileTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 00:00.008, Memory: 6.00 MB
There was 1 failure:
1) JsonFileEqualsJsonFileTest::testFailure
Failed asserting that file "path/to/fixture/file" exists.
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\JsonFileEqualsJsonFileTest.php:8
FAILURES!
Tests: 2, Assertions: 7, Failures: 1.
关键字词:phpunit,assertJsonFileEqualsJsonFile