Home  >  Article  >  Backend Development  >  Use unit testing to check PHP code_PHP tutorial

Use unit testing to check PHP code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:37:09798browse

  测试驱动的开发和单元测试是确保代码在经过修改和重大调整之后依然能如我们期望的一样工作的最新方法。在本文中,您将学习到如何在模块、数据库和用户界面(UI)层对自己的 PHP 代码进行单元测试。

  现在是凌晨 3 点。我们怎样才能知道自己的代码依然在工作呢?

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

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

  PHP 单元测试

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

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

  模块单元测试

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

  清单 1. TestAdd.php

  assertTrue( add( 1, 2 ) == 3 ); }  function test2() { $this->assertTrue( add( 1, 1 ) == 2 ); }}?>

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

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

  清单 2. Add.php

  

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

  清单 3. 测试失败

  % phpunit TestAdd.phpPHPUnit 2.2.1 by Sebastian Bergmann.FFTime: 0.0031270980834961There were 2 failures:1) test1(TestAdd)2) test2(TestAdd)FAILURES!!!Tests run: 2, Failures: 2, Errors: 0, Incomplete Tests: 0.

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

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

  

  清单 4. 测试通过

  % phpunit TestAdd.phpPHPUnit 2.2.1 by Sebastian Bergmann...Time: 0.0023679733276367OK (2 tests)%

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

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

  数据库测试

  在进行模块测试之后,就可以进行数据库访问测试了。数据库访问测试带来了两个有趣的问题。首先,我们必须在每次测试之前将数据库恢复到某个已知点。其次,要注意这种恢复可能会对现有数据库造成破坏,因此我们必须对非生产数据库进行测试,或者在编写测试用例时注意不能影响现有数据库的内容。

  数据库的单元测试是从数据库开始的。为了阐述这个问题,我们需要使用下面的简单模式。

  清单 5. Schema.sql

  DROP TABLE IF EXISTS authors;CREATE TABLE authors (  id MEDIUMINT NOT NULL AUTO_INCREMENT,  name TEXT NOT NULL,  PRIMARY KEY ( id ));

  清单 5 是一个 authors 表,每条记录都有一个相关的 ID。

Next, you can write test cases.

Listing 6. TestAuthors.php

 assertTrue( Authors::delete_all() ); } function test_insert() { $this->assertTrue( Authors::delete_all() ); $this->assertTrue( Authors::insert( Jack )); } function test_insert_and_get() { $this->assertTrue( Authors::delete_all( ) ); $this->assertTrue( Authors::insert( Jack ) ); $this->assertTrue( Authors::insert( Joe ) ); $found = Authors::get_all(); $this-> assertTrue( $found != null ); $this->assertTrue( count( $found ) == 2 ); }}?>

This set of tests covers functions such as deleting authors from the table, inserting authors into the table, and inserting authors while verifying the existence of the author. This is a cumulative test, which I find very useful for finding bugs. By observing which tests work and which don't, you can quickly figure out what's going wrong and then further understand the differences.

The original version of the dblib.php PHP database access code that failed is shown below.

Listing 7. dblib.php

 getMessage()); } return $db; } public static function delete_all() { return false; } public static function insert ( $name ) { return false; } public static function get_all() { return null; }}?>

Executing unit tests on the code in Listing 8 shows that all three tests fail:

Listing 8. dblib.php

 % phpunit TestAuthors.phpPHPUnit 2.2.1 by Sebastian Bergmann.FFFTime: 0.007500171661377There were 3 failures:1) test_delete_all(TestAuthors)2) test_insert(TestAuthors)3) test_insert_and_get(TestAuthors)FAILURES!!!Tests run: 3, Failures: 3, Errors: 0, Incomplete Tests: 0.%

Now we can start adding the code to access the database correctly - method by method - until all 3 tests pass. The final version of dblib.php code looks like this.

Listing 9. Complete dblib.php

 getMessage()); } return $db; } public static function delete_all() { $db = Authors::get_db( ); $sth = $db->prepare( DELETE FROM authors ); $db->execute( $sth ); return true; } public static function insert( $name ) { $db = Authors::get_db() ; $sth = $db->prepare( INSERT INTO authors VALUES (null,?) ); $db->execute( $sth, array( $name ) ); return true; } public static function get_all() { $db = Authors::get_db(); $res = $db->query( "SELECT * FROM authors" ); $rows = array(); while( $res->fetchInto( $row ) ) { $ rows []= $row; } return $rows; }}?>

HTML Test

The next step in testing the entire PHP application is to test the front-end Hypertext Markup Language (HTML) interface. To conduct this test, we need a web page like the one below.


Listing 10. TestPage.php

 get( $ url ); $resp = $clie

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486600.htmlTechArticleTest-driven development and unit testing are to ensure that the code still performs as we expect after modifications and major adjustments The latest ways to work. In this article, you will learn how to...

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