Analysis of the official Automation test method of PHP ____php

Source: Internet
Author: User
Tags diff php language php script php source code sapi zend

This article analyzes and studies the official PHP automated test method, the automated test framework structure, and the specific implementation from a tester's point of view. Give the reader a practical example of how PHP implements an effective automated test to ensure that its code is of high quality. By reading this article, the reader can learn how to automate the automated test method and implementation of PHP, and apply this automated test method to its own development process to improve the quality of code functionality.


A brief introduction to the official automated test method of PHP

The following is an example of an analysis of the LINUX system platform based on the latest official version of PHP 5.2.8 's source code. First look at the PHP Automated test script PHPT script. Automated test Script PHPT sample

PHP's test script is a ". Phpt" suffix, containing test,file,expect and other paragraphs of the file, referred to as PHPT. In each paragraph, Test,file,expect is the basic paragraph, and each test script must include at least three paragraphs. Where the test section can be used to fill in the name of the testing case; The FILE segment is a test case implemented by a PHP script; The EXPECT segment is the expected value of the test case. In the running of a test case, PHP uses the tested PHP executable object to run the test cases in the FILE section, using the actual results to compare the expected values listed in the EXPECT section of the test case, and if the actual and expected values are the same, the test passes, and if it is inconsistent, the test fails.

Table 1 lists the commonly used paragraph names and their corresponding fill descriptions. table 1. Paragraph descriptions in the PHP test script

paragraph name Fill Content Notes
TEST Test Case Name Required paragraph
ARGS Input parameters for the FILE segment Optional paragraph
SKIPIF Skip the condition of this test Optional paragraph
POST POST variables for incoming test scripts Select the paragraph to fill. If you use POST segments, it is recommended that you use SKIPIF segments, such as:
--skipif--
<?php if (php_sapi_name () = = ' cli ') echo ' skip ';?>
Get Get variables for incoming test scripts Select the paragraph to fill. If you use POST segments, it is recommended that you use SKIPIF segments, such as:
--skipif--
<?php if (php_sapi_name () = = ' cli ') echo ' skip ';?>
Ini INI settings applied to the test script Select the paragraph to fill. such as Foo=bar. The value can be obtained by function Ini_get (string name_entry).
FILE Test Script Statement Required paragraphs. Script statements that are written in the PHP language. The results of its execution will be compared with the expected value of the expect* segment.
EXPECT Expected value of the test script Required paragraph
Expectf The expected value of the test script, using the format of the function sscanf () to express the expected value Variants of the EXPECT segment
Expectregex The expected value of the test script, the expected value can be expressed in the regular expression Variants of the EXPECT segment

Take the test script "001.phpt" from the official bag as an example (see Listing 1), which appears to be a test case that validates the PHP version from the content of the test section. The contents of the SKIPIF paragraph are written in the "Skipip.inc" document. In the FILE section, the test case prints out the version of PHP that is set up in the environment variable test_php_executable, and the result is formatted with the string in EXPECTF. Listing 1. PHPT test Script "001.PHPT" sample

			--test-- 
 Version string 
 --skipif-- 
 <?php include "Skipif.inc"; 

 ?>--file--<?php $php = getenv (' test_php_executable '); 

 Var_dump (' $php-n-V '); 

 echo "done\n"; 
 ? > 
 --expectf-- 	
 string (%d) "PHP%s (CLI) (built:%s)%s 
 Copyright (c) 1997-20%d the PHP Group 
 Zend En Gine v%s, Copyright (c) 1998-20%d Zend technologies 
 " 
 done
How to run the PHP Automation test script

Before running the test footsteps, first compile the tested PHP source code into an executable object.

You then import several environment variables. Table 2 describes how the main environment variables are set. table 2. environment variable settings in PHP Automation testing

environment variable name environment variable Value Example
Test_php_executable Set the test object PHP, or "auto". When "Auto" is set, it is "./sapi/cgi/php-cgi" If it is a CGI mode, or "./sapi/cli/php" If it is CLI mode. test_php_executable=
$HOME/php-5.2.8/sapi/cli/php
Test_php_detailed Set whether verbose log output is required. Set a value of 1 or 0. Test_php_detailed=1
Test_php_user Set whether a specially crafted user directory is required. Test_php_detailed= "/usr/test1"
Test_php_log_format Set the format of the log. Sets the subset of the value "Leod" substring. Where L represents the need to generate a ". Log" file after the test, E represents ". Exp", O represents ". Out", and D represents ". diff". test_php_log_format= "LD"

In this example, the environment variables are set in the Bash environment as follows: Listing 2. Setting an example of an environment variable

Export home=/home/user_dir/ 
 export test_php_executable= $HOME/php-5.2.8/sapi/cli/php 
 export Test_php_ Detailed=1 
 export test_php_log_format= "Leod"

After this setup, the tested PHP executable object is the "PHP" executable file that was compiled under the directory "$HOME/php-5.2.8/sapi/cli/".

You will also need to edit the test script PHPT to save as a ". PHPT" file before you perform the test. For example, take the test script "001.phpt" from the official bag and run it as follows: listing 3. PHPT Test Summary Report Example

bash-2.03$ CD $HOME/php-5.2.8/ 
 bash-2.03$ $HOME/php-5.2.8/sapi/cli/php run-tests.php  \ 
 $HOME/php-5.2.8 /sapi/cli/tests/001.phpt

If the actual output of the test case is consistent with the expected value, the test results are printed on the screen as follows: listing 4. PHPT Test Summary Report Example

===================================================================== CWD:/home/user_dir/php-5.2.8/sapi/cli/ph 
 P PHP:/home/user_dir/php-5.2.8/sapi/cli/php php_sapi:cli php_version:5.2.8 zend_version:2.1.0 Php_os:linux rhas05 2.6.9-55.elhugemem #1 SMP Fri Apr 17:20:11 EDT 2007 i686 i686 i386 G Nu/linux INI Actual:more. 
 Inis:extra dirs: ===================================================================== Running selected Tests. Pass Test version string [001.PHPT] ===================================================================== number of Tests:1 1 tests skipped:0 (0.0%)--------tests warned:0 (0%) 
 (0%) Tests failed:0 (0%) 
 (0%) Tests passed:1 (100%) (100.0%)---------------------------------------------------------------------time taken:0 seconds = = =============================================================== 

If the test fails, in addition to the screen output failure results, several files are generated under the current running directory for the user to analyze the cause of the test failure. The types of log files generated are set by the environment variable Test_php_log_format, as detailed in table 2. If "Leod" is set, the build log file includes the five types of files listed in table 3. Table 3. Output script file for PHP Automation test

The settings in the
log file name Log file Contentscorresponding Test_php_log_format
001.out The actual output results obtained after running the test statement. O
001.exp The expected result in the script, which is the content of the expect* section in the test script. E
001.log The output of the actual run and the expected result in the script, the collection of ". Exp" and ". Out". L
001.diff The results of the actual running output and the expected result in the script are compared with the diff command. D
001.php

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.