您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertXmlStringEqualsXmlFile
发布时间:2021-10-06 12:26:49编辑:雪饮阅读()
assertXmlStringEqualsXmlFile断言用于断言一个实际的字符串(xml)与一个预期的xml文件中的文件内容(xml)是否相同,并且有无xml头都算。
参与测试的文件1.xml有xml头:
C:\Users\Administrator>type D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
参与测试的文件2.xml有xml头:
C:\Users\Administrator>type D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\2.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<foo><baz/></foo>
参与测试的文件3.xml无头,仅xml内容:
C:\Users\Administrator>type D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\3.xml
<foo><baz/></foo>
那么具体的测试实例:
XmlStringEqualsXmlFileTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class XmlStringEqualsXmlFileTest extends TestCase
{
public function testFailure(): void
{
$this->assertXmlStringEqualsXmlFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.xml', '<foo><baz/></foo>');
}
public function testTwo(): void
{
$this->assertXmlStringEqualsXmlFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\2.xml', '<foo><baz/></foo>');
}
public function testThree(): void
{
$this->assertXmlStringEqualsXmlFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\3.xml', '<foo><baz/></foo>');
}
}
use PHPUnit\Framework\TestCase;
final class XmlStringEqualsXmlFileTest extends TestCase
{
public function testFailure(): void
{
$this->assertXmlStringEqualsXmlFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\1.xml', '<foo><baz/></foo>');
}
public function testTwo(): void
{
$this->assertXmlStringEqualsXmlFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\2.xml', '<foo><baz/></foo>');
}
public function testThree(): void
{
$this->assertXmlStringEqualsXmlFile('D:\software\FormatFactory_5.3.0_PortableSoft\FormatFactoryPortable\Data\3.xml', '<foo><baz/></foo>');
}
}
运行结果:
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\XmlStringEqualsXmlFileTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F.. 3 / 3 (100%)
Time: 00:00.010, Memory: 6.00 MB
There was 1 failure:
1) XmlStringEqualsXmlFileTest::testFailure
Failed asserting that two DOM documents are equal.
--- Expected
+++ Actual
@@ @@
<?xml version="1.0"?>
-<note>
- <to>George</to>
- <from>John</from>
- <heading>Reminder</heading>
- <body>Don't forget the meeting!</body>
-</note>
+<foo>
+ <baz/>
+</foo>
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\XmlStringEqualsXmlFileTest.php:8
FAILURES!
Tests: 3, Assertions: 3, Failures: 1.
从运行结果可以看到仅仅是1.xml不匹配,而无论有头还是无头,只要xml内容一致,即2.xml和3.xml断言成功。
关键字词:phpunit,assertXmlStringEqualsXmlFile
相关文章
- phpunit断言-assertXmlFileEqualsXmlFile
- phpunit断言-assertTrue
- phpunit断言-assertStringStartsWith
- phpunit断言-assertStringEqualsFile
- phpunit断言-assertStringEndsWith
- phpunit断言-assertSame-对象断言
- phpunit断言-assertSame
- phpunit断言-assertStringMatchesFormatFile
- phpunit断言-assertStringMatchesFormat
- phpunit断言-assertMatchesRegularExpression