Home php教程 php手册 利用单元测试在每个层上对PHP代码进行检查(1)

利用单元测试在每个层上对PHP代码进行检查(1)

Jun 13, 2016 am 11:00 AM
php web superior code use unit exist right app yes test

Web应用程序是24x7不间断运行的,因此我的程序是否还在运行这个问题会在晚上一直困扰我。单元测试已经帮我对自己的代码建立了足够的信心——这样我就可以安稳地睡个好觉了。

单元测试是一个为代码编写测试用例并自动运行这些测试的框架。测试驱动的开发是一种单元测试方法,其思想是应该首先编写测试程序,并验证这些测试可以发现错误,然后才开始编写需要通过这些测试的代码。当所有测试都通过时,我们开发的特性也就完成了。这些单元测试的价值是我们可以随时运行它们——在签入代码之前,重大修改之后,或者部署到正在运行的系统之后都可以。

PHP单元测试

对于PHP来说,单元测试框架是PHPUnit2。可以使用PEAR命令行作为一个PEAR模块来安装这个系统:% pear install PHPUnit2。

在安装这个框架之后,可以通过创建派生于PHPUnit2_Framework_TestCase的测试类来编写单元测试。

模块单元测试

我发现开始单元测试最好的地方是在应用程序的业务逻辑模块中。我使用了一个简单的例子:这是一个对两个数字进行求和的函数。为了开始测试,我们首先编写测试用例,如下所示。

清单1.TestAdd.php

<p><?php <br>require_once 'Add.php';<br>require_once 'PHPUnit2/Framework/TestCase.php';</p><p>class TestAdd extends PHPUnit2_Framework_TestCase<br>{<br>function test1() { $this->assertTrue( add( 1, 2 ) == 3 ); }<br>function test2() { $this->assertTrue( add( 1, 1 ) == 2 ); }<br>}<br>?></p>
Copy after login

这个TestAdd类有两个方法,都使用了test前缀。每个方法都定义了一个测试,这个测试可以与清单1一样简单,也可以十分复杂。在本例中,我们在第一个测试中只是简单地断定1加2等于3,在第二个测试中是1加1等于2。

PHPUnit2系统定义了assertTrue()方法,它用来测试参数中包含的条件值是否为真。然后,我们又编写了Add.php模块,最初让它产生错误的结果。

清单2.Add.php

<p><?php <br>function add( $a, $b ) { return 0; }<br>?></p>
Copy after login

现在运行单元测试时,这两个测试都会失败。

清单3.测试失败

<p>% phpunit TestAdd.php<br>PHPUnit 2.2.1 by Sebastian Bergmann.</p><p>FF</p><p>Time: 0.0031270980834961<br>There were 2 failures:<br>1) test1(TestAdd)<br>2) test2(TestAdd)</p><p>FAILURES!!!<br>Tests run: 2, Failures: 2, Errors: 0, Incomplete Tests: 0.</p>
Copy after login

现在我知道这两个测试都可以正常工作了。因此,可以修改add()函数来真正地做实际的事情了。

<p><?php <br>function add( $a, $b ) { return $a+$b; }<br>?></p>
Copy after login

现在这两个测试都可以通过了。

清单4.测试通过

<p>% phpunit TestAdd.php<br>PHPUnit 2.2.1 by Sebastian Bergmann.<br>..<br>Time: 0.0023679733276367<br>OK (2 tests)<br>%</p>
Copy after login

尽管这个测试驱动开发的例子非常简单,但是我们可以从中体会到它的思想。我们首先创建了测试用例,并且有足够多的代码让这个测试运行起来,不过结果是错误的。然后我们验证测试的确是失败的,接着实现了实际的代码使这个测试能够通过。

我发现在实现代码时我会一直不断地添加代码,直到拥有一个覆盖所有代码路径的完整测试为止。在本文的最后,您会看到有关编写什么测试和如何编写这些测试的一些建议。

1

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles