Home  >  Article  >  Backend Development  >  Learn the essence of PHP and write automatic tests for efficient PHP code_PHP Tutorial

Learn the essence of PHP and write automatic tests for efficient PHP code_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:471042browse

If you want to create a perfect product, it must be inspected and tested in all aspects. There are several types of testing, each targeting a specific aspect of the application.

This article will introduce unit testing, database testing, system testing, and load testing.

1. Unit testing

Unit testing is testing every step of the application to ensure that its various components are functioning properly. Without unit tests, it is generally difficult to find the cause of incorrect behavior in an application.

Unit testing usually uses a unit testing framework, which provides the basic structure needed to write and run tests and output the results.

Some of the more commonly used unit testing frameworks include:

PHPUnit: http://phpunit.de/

SimpleTest: http://www.simpletest.org/

PHPT: http://qa.php.net/write-test.php

2. Database testing

PHPUnit database extension is modeled after JUnit’s DbUnit extension, which is the de facto Java unit testing framework. The PHPUnit database extension does not handle creating databases, tables, or user credentials; it operates on the assumption that these have already been created.

PHPUnit database test reference: http://phpunit.de/manual/current/en/database.html

3. System Test

Once the various components of the system and interactions with external systems have been tested, then we are going to test the application as a whole, which becomes system testing.

For web applications, this is often done by writing automated tests to interact with the browser in the same way a real user would.

A popular software package for writing and executing this kind of testing is Selenium, which is a Java-based server that runs clients to connect to and execute commands to launch and interact with the browser. This software is often used to perform a series of actions within a web application and make assertions on the contents of the last loaded file to confirm that it functions as expected.

Selenium: http://seleniumhq.org/

PHPUnit includes a Selenium extension that allows these interactions to be performed.

4. Load Test

Once an application is working properly, both in its individual components and as a whole, it is very necessary to understand how the application is functioning as a whole.

Load testing simulates the behavior of a group of users to determine how an application behaves under load.

Two testing tools will be introduced next: ab and siege

1. ab testing tool

As an integral part of the Apache HTTP server project, ab is a relatively simple benchmarking tool developed and suitable for most environments where Apache is installed.

It has many parameters. We have slightly adjusted how it guides testing. There are 3 commonly used parameters:

1) -c #: The number of concurrent requests per second, or the number of users accessing the application at the same time.

2)-n #: The request letter to be sent.

3) -t #: The maximum time in seconds that the test lasts, assuming -n 50000.

So, for example, if you wanted to simulate the behavior of 10 concurrent users in one minute, you could use the following command:

ab -c <span>10</span> -t <span>60</span> http:<span>//</span><span>localhost/phpinfo.php</span>

ab usage reference: http://httpd.apache.org/docs/2.0/programs/ab.html

2. Siege testing tool

Another commonly used load testing tool is Siege, developed by Joe Dog Software. ab's load testing is limited to a specific URL, but in addition to URLs, Siege can also load test the entire application. The Siege manual describes its supported options.

Siege: http://www.joedog.org/siege-home/

Siege Manual: http://www.joedog.org/siege-manual/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/780765.htmlTechArticleIf you want to create a perfect product, it must be inspected and tested in all aspects. There are several types of testing, each targeting a specific aspect of the application. This article will...
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