function testFormatn7() {
$result = $this->;abc->;formatn("1343.01");
$expected = "1343";
$this->;assertTrue($result == $expected);
}
function testFormatn8() {
$result = $this->;abc->;formatn("1343.05");
$expected = "1343.1";
$this->;assertTrue($result == $expected);
}
function testFormatn9() {
$result = $this->;abc->;formatn("0");
$expected = "0";
$this->;assertTrue($result == $expected);
}
function testFormatn10() {
$result = $this->;abc->;formatn("105.2342");
$expected = "105.23";
$this->;assertTrue($result == $expected);
}
function testFormatn11() {
$result = $this->;abc->;formatn("105.2375");
$expected = "105.24";
$this->;assertTrue($result == $expected);
}
function testFormatn12() {
$result = $this->;abc->;formatn("0.000523751");
$expected = "0.00052375";
$this->;assertTrue($result == $expected);
}
function testFormatn13() {
$result = $this->;abc->;formatn("0.000523755");
$expected = "0.00052376";
$this->;assertTrue($result == $expected);
}
}最后还需要一个运行测试的程序
CODE: ----------runtest.php-----------
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';
$suite = new PHPUnit_TestSuite("foTest");
$result = PHPUnit::run($suite);
echo $result->;toString();
?>;现在就可以通过命令行运行这个testcase
php runtest.php
得到结果如下:
CODE: TestCase foTest->;testFormatn1() passed
TestCase foTest->;testFormatn2() passed
TestCase foTest->;testFormatn3() passed
TestCase foTest->;testFormatn4() passed
TestCase foTest->;testFormatn5() passed
TestCase foTest->;testFormatn7() passed
TestCase foTest->;testFormatn8() passed
TestCase foTest->;testFormatn9() passed
TestCase foTest->;testFormatn10() passed
TestCase foTest->;testFormatn11() passed
TestCase foTest->;testFormatn12() passed
TestCase foTest->;testFormatn13() passed
TestCase foTest->;testFormatn6() failed: expected TRUE, actual FALSE其中testFormatn6的测试失败,
我们就可以去检查一下我们的代码在什么地方出问题了。
补充一点
也可以把assertTrue方法换assertEquals,如下:
CODE: function testFormatn6() {
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertEquals($expected, $result);
}如果失败得到对应的结果会直观一些(可以显示错误的结果):
CODE: TestCase foTest->;testFormatn8() failed: expected 1343 , actual 134.
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/