PHPUnit | assertStringContainsString()函数
assertStringContainsString ()函数是PHPUnit 中的内置函数,用于断言包含子字符串的字符串。如果字符串包含子字符串作为子字符串,则此断言将返回 true,否则返回 false,如果为 true,则断言的测试用例通过,否则测试用例失败。
语法:
assertStringContainsString(string $substring, string $string, string $message = ''])
参数:此函数接受三个参数,如上述语法所示。参数说明如下:
- $子串:该参数表示要串定字符串的字符串。
- $ 字符串 :这个参数是一个字符串,断言函数将检查它是否包含子字符串。
- $message :此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。
下面的程序说明了 assertStringContainsString()函数:
程序一:
assertStringContainsString($substring, $testString, "testString doesn't contains 'geeks' as substring") ;
}
}
?>
输出:
PHPUnit 8.2.5 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 65 ms, Memory: 10.00 MB
There was 1 failure:
1) GeeksPhpunitTestCase::testNegativeTestcaseForAssertStringContainsString
testString doesn't contains 'geeks' as substring
Failed asserting that 'geekforgeek' contains "geeks".
/home/shivam/Documents/geeks/phpunit/abc.php:11
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
方案二:
assertStringContainsString($substring, $testString, "testString doesn't contains 'geeks' as substring") ;
}
}
?>
输出:
PHPUnit 8.2.5 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 67 ms, Memory: 10.00 MB
OK (1 test, 1 assertion)
注意:要使用 phpunit 运行测试用例,请按照此处的步骤操作。此外,phpunit 7 及更高版本支持 assertStringContainsString()。