Home php教程 php手册 PHPUnit袖珍指南之命令行测试工具

PHPUnit袖珍指南之命令行测试工具

Jun 21, 2016 am 09:00 AM
file php phpunit unittest

  PHPUnit命令行测试工具是通过phpunit命令调用的。如下代码显示如何通过PHPUnit命令行测试工具运行测试。

phpunit ArrayTest
PHPUnit 2.3.0 by Sebastian Bergmann.

Time: 0.067288

OK (2 tests)

  对每个测试,PHPUnit命令行测试工具打印一个字符表示进程:

  ·测试成功打印“.”。

  ·运行测试方法是发生了断言失败打印“F”。

  ·运行测试方法是发生了错误打印“E”。

  ·测试没有完成或测试没有实现打印“I”(见本书后“未完成的测试”一章)。

  PHPUnit可以区分失败和错误。一个失败是PHPUnit的断言违例,错误是一个意料外的异常或一个PHP错误。有时候这种差别是有用的,因为错误相比失败更容易修正。如果你有一大串问题列表,最好先解决所有错误,然后看看有没有失败遗留下来。

  让我们看看如下一些代码命令行测试工具的选项:

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

  运行类UnitTest提供的测试,该类应该定义在源文件UnitTest.php中。

  类UnitTest必须继承PHPUnit2_Framework_TestCase类,或是提供了公有静态方法suite,并返回PHPUnit2_ Framework_Test对象的类(例如,类PHPUnit2_Framework_TestSuite的一个实例)

phpunit UnitTest UnitTest.php

  运行类UnitTest提供的测试,该类要定义在命令指定的源文件(UnitTest.php)中。

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

  控制运行测试的代码覆盖信息的分析和集合(参见本书后代码覆盖分析一节)

--testdox-html and --testdox-text

  以HTML或普通文本格式生成运行测试的敏捷文档(参见本书后的“测试的其他用途”一章)

--log-xml

  生成运行测试的XML格式的日志文件。

  下一个例子显示为ArrayTest中的测试生成的XML日志文件。

<?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>

  下面的XML日志文件是为名为FailureErrorTest的测试类两个测试生成的,一个是testFailure,一个是testError。这显示了失败和错误是如何分别表示的。

<?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

  指定将要使用的测试套件加载器。

  标准测试套件加载器会在当前工作目录和PHP的include_path configuration指令定义的路径中寻找源文件。按照PEAR的命名规则,形如Project_Package_Class的类名会映射到的源文件为Project/Package/Class.php。

  --skeleton

  为类Unit(在文件Unit.php中)生成一个名为UnitTest(在文件UnitTest.php中)的测试用例类的框架。对原始类的每个方法,在生成的测试用例类中提供了一个未完成的测试用例(见本书后的“未完成测试”部分)。

  下面的例子显示了如何为一个名为Sample的类生成一个测试类的框架。

phpunit --skeleton Sample
PHPUnit 2.3.0 by Sebastian Bergmann.
Wrote test class skeleton for 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.

  当你为现有代码书写测试时,你不得不重复很多相同的代码片断,如:

public function testSampleMethod( ) {}

  PHPUnit能帮助你分析现有代码,生成测试用例类的框架。

--wait

  每个测试结束时,等待一次击键。这很有用,特别是你在一个只有测试一直运行在打开的窗口中运行测试时。

  提示 当被测试代码中有PHP语法错误时,文本界面的测试会直接退出,不输出任何错误信息。标准的测试套件加载器会检查测试套件的源文件的PHP语法错误,但是,它不会检查测试套件包含的源文件的语法错误。PHPUnit的未来版本会用在砂箱中PHP解释器类解决这个问题。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles