# PHPUnit袖珍指南-第二章 自动测试

4770人阅读 评论(0)

1. 测试数组和sizeof()

<?php
$fixture = Array( ); //$fixture应该为空。

$fixture[] = "element"; //$fixture应该包含一个数组成员。
?>

2. 采用打印语句测试Arraysizeof()

<?php
$fixture = Array( ); print sizeof($fixture) . "/n";

$fixture[] = "element"; print sizeof($fixture) . "/n";
?>
0
1

3. 比较Arraysizeof()的期望值和实际值

<?php
$fixture = Array( ); print sizeof($fixture) == 0 ? "ok/n" : "not ok/n";

$fixture[] = "element"; print sizeof($fixture) == 1 ? "ok/n" : "not ok/n";
?>
ok
ok

4.使用断言函数来测试Arraysizeof()

<?php
$fixture = Array( ); assertTrue(sizeof($fixture) = = 0);

$fixture[] = "element"; assertTrue(sizeof($fixture) = = 1);

function assertTrue($condition) {  if (!$condition) {
  throw new Exception("Assertion failed.");
 }
}
?>

--------------------------------------------------------------------------------------------------------------------

Chapter 2. Automating Tests

Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that the good programmer uses tests to detect his mistakes as soon as possible. The sooner you test for a mistake, the greater your chance of finding it, and the less it will cost to find and fix. This explains why it is so problematic to leave testing until just before releasing software. Most errors do not get caught at all, and the cost of fixing the ones you do catch is so high that you have to perform triage with the errors because you just cannot afford to fix them all.

Testing with PHPUnit is not a totally different activity from what you should already be doing. It is just a different way of doing it. The difference is between testingthat is, checking that your program behaves as expectedand performing a battery of testsrunnable code-fragments that automatically test the correctness of parts (units) of the software. These runnable code-fragments are called unit tests.

In this section, we will go from simple print-based testing code to a fully automated test. Imagine that we have been asked to test PHP's built-in Array. One bit of functionality to test is the function sizeof( ). For a newly created array, we expect the sizeof( ) function to return 0. After we add an element, sizeof( ) should return 1. Example 1 shows what we want to test.

Example 1. Testing Array and sizeof( )

<?php

$fixture = Array( ); //$fixture is expected to be empty.

$fixture[] = "element"; //$fixture is expected to contain one element.

?>

A really simple way to check whether we are getting the results we expect is to print the result of sizeof( ) before and after adding the element (see Example 2). If we get 0 and then 1, Array and sizeof( ) are behaving as expected.

Example 2. Using print to test Array and sizeof( )

<?php

$fixture = Array( ); print sizeof($fixture) . "/n";

$fixture[] = "element"; print sizeof($fixture) . "/n";

?>

0

1

Now, we would like to move from tests that require manual interpretation to tests that can run automatically. In Example 3, we write the comparison of the expected and actual values into the test code and print ok if the values are equal. If we see a not ok message, we know something is wrong.

Example 3. Comparing expected and actual values to test Array and sizeof( )

<?php

$fixture = Array( ); print sizeof($fixture) == 0 ? "ok/n" : "not ok/n";

$fixture[] = "element"; print sizeof($fixture) == 1 ? "ok/n" : "not ok/n";

?>

ok

ok

We now factor out the comparison of expected and actual values into a function that raises an exception when there is a discrepancy (Example 4). Now our test output gets simpler. Nothing gets printed if the test succeeds. If we see an unhandled exception, we know something has gone wrong.

Example 4. Using an assertion function to test Array and sizeof( )

<?php

$fixture = Array( ); assertTrue(sizeof($fixture) = = 0);

$fixture[] = "element"; assertTrue(sizeof($fixture) = = 1);

function assertTrue($condition) { if (!$condition) {

throw new Exception("Assertion failed.");

}

}

?>

The test is now completely automated. Instead of just testing as we did with our first version, with this version, we have an automated test.

The goal of using automated tests is to make fewer mistakes. While your code will still not be perfect, even with excellent tests, you will likely see a dramatic reduction in defects once you start automating tests. Automated tests give you justified confidence in your code. You can use this confidence to take more daring leaps in design (see "Refactoring," later in this book), get along better with your teammates (see "CrossTeam Tests," later in this book), improve relations with your customers, and go home every night with proof that the system is better now than it was that morning because of your efforts.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：186227次
• 积分：2110
• 等级：
• 排名：第18988名
• 原创：13篇
• 转载：5篇
• 译文：13篇
• 评论：41条
评论排行
最新评论