PHPUnit Pocket Guide Command line test tool

Source: Internet
Author: User
Tags command line documentation log naming convention php error php syntax error
The PHPUnit command Line test tool is invoked by the PHPUnit command. The following code shows how to run a test through the PHPUnit command-line test tool.

PHPUnit Arraytest
PHPUnit 2.3.0 by Sebastian Bergmann.

time:0.067288

OK (2 Tests)
For each test, the PHPUnit command line test tool prints a character representing the process:

• Test successfully print "."

• Run test method is an assertion failed to print "F".

• Run test method is an error printing "E".

• The test did not complete or the test did not implement print "I" (see the "Unfinished Test" chapter after this book).

PHPUnit can distinguish between failures and errors. One failure is phpunit assertion violation, error is an unexpected exception or a PHP error. Sometimes this difference is useful because errors are easier to fix than failures. If you have a list of questions, it's best to fix all the errors and see if there are any failures left.

Let's look at some of the following options for the Code Command-Line test tool:

PHPUnit--help
PHPUnit 2.3.0 by Sebastian Bergmann.

usage:phpunit [Switches] unittest [unittest.php]
--coverage-data <file> Write code-coverage data in RAW format to file.
--coverage-html <file> Write code-coverage data in HTML format to file.
--coverage-text <file> Write code-coverage data in the text format to file.
--testdox-html <file> Write Agile Documentation in HTML format to file.
--testdox-text <file> Write Agile Documentation in the text format to file.
--log-xml <file> Log test progress in XML format to file.
--loader <loader> Testsuiteloader implementation to use.
--skeleton Generate skeleton UnitTest class for the unit in unit.php.
--wait Waits for a keystroke after the each test.
--help prints this usage information.
--version prints the version and exits.
PHPUnit UnitTest

Run the test provided by the class UnitTest, which should be defined in the source file unittest.php.

Class UnitTest must inherit the Phpunit2_framework_testcase class, or provide a public static method suite, and return a class of Phpunit2_ Framework_test objects (for example, class phpunit2_ An instance of Framework_testsuite)

PHPUnit UnitTest unittest.php

Run the test provided by the class UnitTest, which is defined in the source file (unittest.php) specified by the command.

--coverage-data,--coverage-html, and--coverage-text

Controls the analysis and collection of code coverage information for running tests (see section on Code Coverage analysis after this book)

--testdox-html and--testdox-text

Generate an agile document that runs tests in HTML or plain text format (see the "Other uses for tests" chapter later in this book)

--log-xml

Generates an XML-formatted log file that runs tests.

The next example shows the XML log file generated for the test in Arraytest.

<?xml version= "1.0" encoding= "UTF-8"?
<testsuites>
<testsuite name= "Arraytest" tests= "2" failures= "0" errors= "0" time= "0.020026"
<testcase name= "Testnewarrayisempty" class= "Arraytest" time= "0.014449"
<testcase name= "testarraycontainsanelement" class= "Arraytest" time= "0.005577"
</testsuite>
</testsuites>
The following XML log file is generated for two Tests of the test class named Failureerrortest, one is testfailure, and the other is testerror. This shows how the failures and errors are represented separately.

<?xml version= "1.0" encoding= "UTF-8"?
<testsuites>
<testsuite name= "Failureerrortest" tests= "2" failures= "1" errors= "1" time= "0.013603"
<testcase name= "Testfailure" class= "Failureerrortest" time= "0.011872"
<failure message= "" Type= "Phpunit2_framework_assertionfailederror" > </failure>
</testcase>
<testcase name= "Testerror" class= "Failureerrortest" time= "0.001731"
<error message= "" Type= "Exception" > </error>
</testcase>
</testsuite>
</testsuites>
--loader
Specifies the test suite loader that will be used.

The standard test suite loader looks for source files in the current working directory and in the path defined by the PHP include_path configuration directive. According to Pear's naming convention, a Project_package_class class name is mapped to a source file of project/package/class.php.

--skeleton

Generates a framework for a test case class named UnitTest (in file unittest.php) for class unit (in file unit.php). For each method of the original class, an unfinished test case is provided in the generated test case class (see the "Unfinished Tests" section after this book).

The following example shows how to generate a test class framework for a class named Sample.

PHPUnit--skeleton Sample
PHPUnit 2.3.0 by Sebastian Bergmann.
Wrote test class skeleton for the Sample to
sampletest.php.
PHPUnit Sampletest
PHPUnit 2.3.0 by Sebastian Bergmann.
I
time:0.007268
There was 1 incomplete test case:
1) testsamplemethod (sampletest)
OK, but incomplete test cases!!!
Tests run:1, incomplete test cases:1.
When you write a test for an existing code, you have to repeat many of the same pieces of code, such as:

Public Function Testsamplemethod () {}
PHPUnit can help you analyze existing code and generate a framework for test case classes.

--wait

At the end of each test, wait for one keystroke. It's very useful, especially if you're in a test that runs only when tests are run in an open window.

Tip When there is a PHP syntax error in the test code, the test for the text interface exits directly without outputting any error messages. The standard test suite loader will check the PHP syntax error for the source file of the test suite, but it will not check the source files contained in the test suite for syntax errors. Future versions of PHPUnit will use the PHP interpreter class in the sandbox to solve this problem.

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.