PHPUnit pocket guide-command line testing tool

Source: Internet
Author: User

The PHPUnit command line testing tool is called through the phpunit command. The following code shows how to run the test using 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 testing tool prints a character to indicate the process:

· Print "." After the test is successful.

· The test method is to print "F" when an asserted failure occurs ".

· If an error occurs, print "E ".

· The test was not completed or the test was not implemented to print "I" (See the "unfinished tests" section after this book ).

PHPUnit can distinguish between failures and errors. One failure is an asserted violation of PHPUnit. An 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 large list of problems, it is best to solve all the errors first and check whether there are any failures left behind.

Let's take a look at the following code command line testing tool options:

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 text format to file.
-- Testdox-html <file> Write agile documentation in HTML format to file.
-- Testdox-text <file> Write agile documentation in 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 Unit in Unit. php.
-- Wait Waits for a keystroke after each test.
-- Help Prints this usage information.
-- Version Prints the version and exits.

Phpunit UnitTest

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

The class UnitTest must inherit the PHPUnit2_Framework_TestCase class, or provide a public static method suite and return the class of the PHPUnit2 _ Framework_Test object (for example, an instance of the class PHPUnit2_Framework_TestSuite)

Phpunit UnitTest. php

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

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

Control the analysis and collection of code coverage information for running tests (see the code coverage analysis section after this book)

-- Testdox-html and -- testdox-text

Generate an agile document for running tests in HTML or plain text format (see the "other uses of tests" section after this book)

-- Log-xml

Generate a log file in XML format for running the test.

The next example shows the XML log file generated by 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 the test class FailureErrorTest. One is testFailure and the other is testError. This shows how 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

Specify the test suite loader to be used.

The standard test suite loader searches for the source files in the current working directory and the path defined in the plude_path configuration command of PHP. According to the PEAR naming rules, the source file mapped to the Class name of Project_Package_Class is Project/Package/Class. php.

-- Skeleton

Generate a framework for the test case class UnitTest (in the UnitTest. php file) for the class Unit (in the file UnitTest. php. For each method of the original class, an unfinished test case is provided in the generated test case class (see the "unfinished test" section after this book ).

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

Phpunit -- skeleton Sample
PHPUnit 2.3.0 by Sebastian Bergmann.
Wrote test class skeleton for Sample
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 tests for existing code, you have to repeat many identical code fragments, such:

Public function testSampleMethod (){}

PHPUnit helps you analyze existing code and generate the framework of the test case class.

-- Wait

At the end of each test, wait for a key. This is useful, especially when you run a test in an open window that only the test has been running.

Prompt: If PHP syntax is incorrect in the tested code, the test on the text interface will exit without any error message. The standard test suite loader checks the PHP syntax errors of the test suite's source files, but does not check the syntax errors of the source files contained in the test suite. The PHP interpreter class will be used in the sandbox for future PHPUnit versions 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.