For example, explain the PHP script test method and the script Test Method
I. Common test examples
We often encounter this situation: rewrite and test some legacy code that has not passed any tests, and even use object-oriented code. To test such code, I suggest breaking the code into blocks so that it is easy to test.
However, these legacy codes are not so well reconstructed. For example, before testing, you cannot rewrite the code to avoid affecting the original program and, of course, it is difficult to perform unit testing.
In PHP programs, some code is usually written in several index. php and script. PHP files. These. php files are stored in several different folders. If they cannot be found, they cannot be directly accessed by the Web server.
Test copy
To test a PHP script, we need to simulate an HTTP request and check whether the returned response (response) is equal to the expected value. It is worth noting that simulating a request requires defining response and request. This is not only the difference in content, but also the difference in their header information.
In addition, if we want to test a transaction script for data operations, we need to ensure that it is not allowed to connect to the rest of the real database or application.
In reality, no one will directly use the original PHP script for rewrite testing. For fear of making the code unrecoverable. I suggest using a copy of the PHP script so that we can perform minor operations on the PHP code.
How to minimize code modification: delete include and require statements (if they are not used) and modify the calling method of internal functions, such () write it as $ object-> header ().
Finally, let's test the transaction script. After testing, we can extract them from the copy script and put them into the new script file.
Procedure
1. Simulate an HTTP request and redefine the variables $ _ GET and $ _ POST, and modify the header of $ _ SERVER.
2. Get the request response. The response body can be captured through ob_start () and ob_get_clean (). It can collect every response using echo () or <? Buffer (buffer content) output by the php tag ).
Note: The output buffer supports multiple levels of nesting in PHP, so it can be captured in most cases, even if the script is calling itself using ob.
3. The test script should contain internal methods of the transaction script, so all methods within the script scope can be called. For example:
1. variables required by the script can be defined as local variables encapsulated, for example, $ connection as a database connection.
2. It is not a built-in function of the original PHP, and an object should be added for calling. For example, header () is written as $ this-> header ().
Code
This is the transaction script object we want to test. Specifically, we need to encapsulate it in the script:
<?phpclass ForumPosting{ private $headers = array(); public function handleRequest($postRequest) { $_POST = $postRequest; $connection = $this->getAConnection(); ob_start(); include 'forum/post_new_copy.php'; $content = ob_get_clean(); return array( 'content' => $content, 'headers' => $this->headers ); } private function header($headerLine) { $this->headers[] = $headerLine; } ...}
Here is our test code:
public function testANewPostIsCreated(){ $action = new ForumPosting(); $response = $action->handleRequest(array( 'id_thread' => 42, 'text' => 'Hello, world', ... )); $this->assertEquals('...', $response['content']); $this->assertContains('Content-type: text/html', $response['headers']);}
The test copy is temporary! It makes the tests we have written remain unchanged. In the end, we need to refactor the tested PHP script to eliminate redundant code.
After the test is complete, we can replace the content of handleRequest () with the real logic code. If you want to write many such test scripts, you can write a common test object to meet your testing needs.
2. unit test toolkit for PHP developers
In the PHP field, there are three major unit testing tools: PHPUNIT, PHPUNIT2, and SimpleTest. PHPUNIT has very simple functions and is not perfect. PHPUNIT2 is a unit test tool specially designed for PHP5, which is consistent with Junit in terms of structure and function; simpleTest is a set of very practical testing tools. Among them, webTest supports testing web program interfaces and is the most recommended test tool for Easy. In this article, we will choose SimpleTest for introduction.
Related Knowledge: PHPUNIT2 is also a good tool, especially because it has many interesting points in architecture. I hope to share it with you in special articles in the future.
SimpleTest: That's Simple.
Installing SimpleTest is simple. You can download a source code package on sf.net and decompress it to the web directory.
Let's take a look at an example: Write a test to check whether a website can be accessed.
First, we introduce the files to be used:
Code List:
require_once("../simpletest/unit_tester.php");require_once("../simpletest/web_tester.php");require_once("../simpletest/reporter.php");
Then we create a test class:
Code List:
Class TestOfSite extends WebTestCase {function TestOfSite () {$ this-> WebTestCase ("test");} function testSite () {$ this-> get ("http://howgo.net/prettyface/display.php "); $ this-> assertTitle (".: facebook :. ");}}
First, we extended the webTestCase class so that we can automatically obtain the web testing capability. Then, in the constructor, we directly use the base class, just pass the title to it. Next we should write the test method, which starts with 'test' to identify which methods of the class are called when we run the test.
And $ this-> get will get the content of the webpage. we specify its title ".: facebook :. ", what we need to do is to instantiate the object of this class and run it.
Code List:
$test = &new TestOfSite();$test->run(new HtmlReporter());
Below is the running result:
If a test error occurs, the following interface is displayed:
A simple test is complete.
Practical drills-a Login Test
Next we will go into practice and complete a login test on this basis. This time, we first post the complete code:
Code List:
Require_once (".. /simpletest/unit_tester.php "); require_once (".. /simpletest/web_tester.php "); require_once (".. /simpletest/reporter. php "); class TestOfLogin extends WebTestCase {function TestOfLogin () {$ this-> WebTestCase (" Login test ");} function testLoginOk () {// get page $ this-> get ("http://howgo.net/prettyface/login.php"); // Add test table item $ this-> setField ("name", "Easy "); $ this-> setField ("pass", "*******"); // submit $ this-> clickSubmit ("Submit "); // check whether the returned page is correct after submission $ this-> assertWantedPattern ("/Successful Logon /"); // click the Page Link $ this-> clickLink ("Click here to enter the management page"); // view the specified page title and key content $ this-> assertTitle ("ADMINCP "); $ this-> assertWantedPattern ("/select the task to be executed/"); // log out to $ this-> clickLink ("exit management "); $ this-> clickLink }}