search
HomeBackend DevelopmentPHP TutorialPHPUnit Pocket Guide - Command Line Testing Tool_PHP Tutorial
PHPUnit Pocket Guide - Command Line Testing Tool_PHP TutorialJul 13, 2016 pm 05:34 PM
phpunitOrderCommand Lineguideyestest toolstunepass

The PHPUnit command line testing tool is called through the phpunit command. The following code shows how to run tests through the PHPUnit command line testing tool.

phpunit ArrayTest
phpunit ArrayTest
PHPUnit 2.3.0 by Sebastian Bergmann.

Time: 0.067288

OK (2 tests)
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 indicating the process:

·The test successfully prints ".".

·When running the test method, an assertion failure occurs and "F" is printed.

· When running the test method, an error occurs and "E" is printed.

·The test is not completed or the test does not print "I" (see the chapter "Unfinished Tests" at the end of this book).

PHPUnit can distinguish between failures and errors. A failure is a PHPUnit assertion violation, and an error is an unexpected exception or a PHP error. Sometimes this distinction is useful because mistakes are easier to fix than failures. If you have a long list of issues, it's a good idea to resolve all the errors first and then see if any failures remain.
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.

Let’s take a look at some of 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 class UnitTest, 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 a class of the PHPUnit2_ Framework_Test object (for example, an instance of the class PHPUnit2_Framework_TestSuite)

phpunit UnitTest UnitTest.php
<?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>
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 at the end of this book) --testdox-html and --testdox-text Generate agile documentation for running tests in HTML or plain text format (see the "Other uses of testing" chapter at the end of this book) --log-xml Generate log files in XML format for running tests. The next example shows the XML log file generated for a test in ArrayTest.
<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 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
<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 to be used.
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.

The standard test suite loader will look for source files in the current working directory and the path defined by PHP's include_path configuration directive. According to the naming rules of PEAR, the source file mapped to a class name in the form Project_Package_Class is Project/Package/Class.php.

--skeleton
public function testSampleMethod( ) {}

A framework that generates a test case class named UnitTest (in the file UnitTest.php) for the class Unit (in the 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 at the end of this book).

The following example shows how to generate a test class skeleton for a class named 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. 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 a lot of the same code snippets, such as: PHPUnit can help you analyze existing code and generate a framework of test case classes. --wait At the end of each test, wait for a keystroke. This is useful especially if you are running tests in a window where only the test is running at all times.
Tip: When there is a PHP syntax error in the code being tested, the text interface test will exit directly without outputting any error message. The standard test suite loader checks the test suite's source files for PHP syntax errors, however, it does not check the source files included in the test suite for syntax errors. Future versions of PHPUnit will address this issue using sandboxed PHP interpreter classes. http://www.bkjia.com/PHPjc/508482.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508482.htmlTechArticlePHPUnit command line testing tool is called through the phpunit command. The following code shows how to run tests through the PHPUnit command line testing tool. phpunit ArrayTest PHPUnit 2.3.0 by Sebastian...
Statement
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
PHP开发中如何使用PHPUnit进行Mock测试PHP开发中如何使用PHPUnit进行Mock测试Jun 27, 2023 am 10:25 AM

在PHP开发中,测试是非常重要的一个环节,测试可以大大减少错误的发生,提高代码质量。Mock测试是测试中的一种形式,它可以模拟出虚假的对象或数据,以便测试我们代码的某个特定功能或场景。PHPUnit是PHP中非常流行的一个测试框架,它支持Mock测试。在这篇文章中,我们将探讨如何使用PHPUnit进行Mock测试。一、什么是Mock测试在开始之前,我们先来了

PHP中的测试报告工具PHP中的测试报告工具May 24, 2023 am 08:24 AM

PHP是一种常见的开源编程语言,广泛应用于Web开发中,它的优点就在于易学、易用、可拓展性强等优点。而作为开发者,我们为了在保证代码质量的同时提高开发效率,必不可少的就是测试和测试报告的使用。在PHP开发中,有很多测试和测试报告工具,其中最常见的就是PHPUnit。然而,PHPUnit虽然简单易用,但是需要一些编写测试用例的基础知识,如果不熟悉,使用起来还是

如何使用PHP和PHPUnit检查代码规范和质量如何使用PHP和PHPUnit检查代码规范和质量Jun 25, 2023 pm 04:57 PM

在现代的软件开发中,代码质量和规范是极为重要的因素。不仅可以让代码更加整洁易于维护,还可以提高代码的可读性和可扩展性。但是,如何检查代码的质量和规范呢?本文将介绍如何使用PHP和PHPUnit来实现这一目标。第一步:检查代码规范在PHP开发中,有一种非常流行的代码规范,它被称为PSR(PHP标准规范)。PSR规范的目的是使PHP代码更具可读性和可维护性。其中

php如何使用PHPUnit和Mockery进行单元测试?php如何使用PHPUnit和Mockery进行单元测试?May 31, 2023 pm 04:10 PM

在PHP项目开发中,单元测试是一项很重要的任务。PHPUnit和Mockery是两个相当流行的PHP单元测试框架,其中PHPUnit是一个被广泛使用的单元测试工具,而Mockery则是一个专注于提供统一而简洁的API以创建和管理对象Mock的对象模拟工具。通过使用PHPUnit和Mockery,开发人员可以快速高效地进行单元测试,以确保代码库的正确性和稳定性

PHP编程中有哪些常见的代码质量工具?PHP编程中有哪些常见的代码质量工具?Jun 12, 2023 am 08:16 AM

PHP编程中有哪些常见的代码质量工具?在现代的软件开发中,代码质量是非常重要的。如果代码质量不好,不仅会降低代码的可读性,增加维护难度,还会造成安全漏洞等一系列问题。而在PHP编程中,我们可以使用一些代码质量工具来检查代码的质量。本文将介绍一些常见的PHP代码质量工具。PHP_CodeSnifferPHP_CodeSniffer是一个用于静态分析PHP代码的

PHP中的代码检查工具PHP中的代码检查工具May 24, 2023 pm 12:01 PM

检查代码质量是每个程序员都必须要做的任务,而PHP中也有很多工具可以用于检查代码的质量和风格,从而提高代码的可读性和可维护性,提高代码的可靠性和安全性。本文将介绍几种常见的PHP代码检查工具,并对它们进行简单的比较和评估,希望可以帮助读者在开发过程中选择合适的工具,提高代码质量和效率。PHP_CodeSnifferPHP_CodeSniffer是一个广泛应用

如何使用PHPUnit进行PHP单元测试如何使用PHPUnit进行PHP单元测试May 12, 2023 am 08:13 AM

随着软件开发行业的发展,测试逐渐成为了不可或缺的一部分。而单元测试作为软件测试中最基础的一环,不仅能够提高代码质量,还能够加快开发者开发和维护代码的速度。在PHP领域,PHPUnit是一个非常流行的单元测试框架,它提供了各种功能来帮助我们编写高质量的测试用例。在本文中,我们将介绍如何使用PHPUnit进行PHP单元测试。安装PHPUnit在使用PHPUnit

PHP Jenkins 与 PHPUnit:单元测试 PHP 代码PHP Jenkins 与 PHPUnit:单元测试 PHP 代码Mar 09, 2024 am 10:10 AM

PHPUnitはphpでのユニットテストを効率化するためのフレームワークです。jenkinsと組み合わせると、CI(継続的インテグレーション)プロセスにテストを組み込み、コード変更のたびにテストを実行できます。JenkinsのPHPUnitプラグインJenkinsのPHPUnitプラグインを使用すると、JenkinsジョブにPHPUnitテストを簡単に追加できます。このプラグインは、テストの実行、結果の表示、および失敗したテストの自動通知を行います。PHPUnitのインストールと構成PHPUni

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft