So far, we have only two Tests on Array and the built-in function sizeof. When we start to test a large number of array _ * () functions, each of them requires a test. We can start from scratch. However, the better way is to write a test at a time so far. we only have two tests for Array and the built-in function sizeof. When we start to test a large number of array _ * () functions, each of them requires a test. We can start from scratch. However, a better way is to write a test infrastructure at a time, and then write only the different parts of each test. PHPUnit is such a basic architecture.
Example 5 shows how to use PHPUnit to override two tests in Example 4.
Example 5: Use PHPUnit to test Array and sizeof ().
Class ArrayTest extends PHPUnit2_Framework_TestCase {
Public function testNewArrayIsEmpty (){
// Create an array fixture.
$ Fixture = Array ();
// The size of the assertion array fixture is 0.
$ This-> assertEquals (0, sizeof ($ fixture ));
}
Public function testArrayContainsAnElement (){
// Create an array fixture.
$ Fixture = Array ();
// Add a member to the array fixture.
$ Fixture [] = 'element ';
// The size of the assertion array fixture is 1.
$ This-> assertEquals (1, sizeof ($ fixture ));
}
}
?>
Example 5 shows the basic steps for writing a test using PHPUnit:
1. the test Class of the Class is ClassTest.
2. ClassTest generally persists in PHPUnit2 _ Framework_TestCase.
3. the test is a public method with no parameter and the name is test *.
4. in the test method, the asserted function, such as assertEquals () (see Table 6), is used to determine whether the actual value matches the value during the period.
A framework such as PHPUnit needs to solve a series of titles, and some seem to conflict with each other. The test must meet both the following conditions:
Easy to Learn
Testing must be easy to learn; otherwise, developers will not learn
Easy to develop
Testing must be easily developed. otherwise, developers will not develop
Easy to Read
The test code must have no external relationships, so that the test itself will not be lost in disorder.
Easy to perform
The test should be carried out easily, and the fulfillment results should be expressed in a clear and clear pattern.
Quick fulfillment
Tests should be performed quickly, so that thousands of tests can be performed every day.
Code isolation
Tests cannot affect each other, and changes in the Test order should not affect the results.
Composite
We should be able to run the test in any combination, which is an inevitable result of code isolation.
These constraints have two important conflicts:
Easy to Learn vs easy to develop
Testing usually does not require the full flexibility of programming. Many test tools provide their own test script language. these languages only have the minimum set of features required for writing tests. because there is no noise to interfere with your test content, the written test is easy to read and write. However, it is inconvenient to learn a new kind of woven mail and a set of tools, and it is easy to mix black and white.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service