Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Installation and usage of PHPUnit in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "installation and usage of PHPUnit in PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the installation and usage of PHPUnit in PHP".

Start hands-on installation of PHPUnit

This article will introduce the unit test tool PHPUnit (http://phpunit.de/),) in PHP and explain how to use PHPUnit in practical work through practical examples. First of all, you can install PHPUnit through pear under PHP:

Pear channel-discover pear.phpunit.de

Pear channel-discover components.ez.no

Pear channel-discover pear.symfony-project.com

Pear install phpunit/PHPUnit

If you want to install manually, you can refer to the PHPUnit manual to install (http://www.phpunit.de/manual/3.0/en/installation.html).

Write * unit test cases

Let's start writing * unit test cases. When writing test cases, follow the following PHPUnit rules:

In general, in the test case, you can extend the PHPUnit_Framework_TestCase class so that you can use methods like setUp (), tearDown (), and so on.

2 the name of the test case * * is to use the established format, that is, add "Test" after the class to be tested. For example, if the class to be tested is RemoteConnect, the test case is named RemoteConnectTest.

3 all test methods in a test case should be named after the test+ test method name, such as testDoesLikeWaffles (). Note that the method must be declared as a public type. Of course, you can include private methods in your test cases, but they cannot be called by phpunit.

(4) the parameters cannot be received in the test method.

Let's start with a simple example. The code is as follows:

In the above code, because you inherit the PHPUnit_Framework_TestCase class, you don't need to write any code in the setUp and tearDown methods. The SetUp method does some initialization work before each test case runs, while tearDown does some work such as resource release after each test case runs. In the test method, the assertion assertTrue of PHPUnit is used to determine whether the returned Boolean value is true. Here, the connectToServe method in RemoteConnect.php is called to determine whether the server can be connected.

Next, let's run the unit test and enter the code at the command line:

Just phpunit / path/to/tests/RemoteConnectTest.php. You can see that if the test passes smoothly, the following result will be output:

PHPUnit 3.4 by Sebastian Bergmann.Time: 1 secondTests: 1, Assertions: 1, Failures 0

As you can see, the above passed the test. By default, PHPUnit runs all the test methods in the test case. Here are a few related assertions in PHPUnit:

Whether the AssertTrue/AssertFalse assertion is true or false AssertEquals determines whether the output is equal to the expected AssertGreaterThan assertion and whether the result of the AssertGreaterThan assertion is greater than a certain value. Similarly, there are LessThan (less than), GreaterThanOrEqual (greater than or equal to), LessThanOrEqual (less than or equal to). AssertContains judges whether the input contains the specified value AssertType determines whether it belongs to the specified type, AssertNull judges whether the file is null, and AssertRegExp judges whether the file exists according to the regular expression.

For example, to illustrate the use of AssertType, we can still use AssertType to determine whether the object instance returned by returnSampleObject is remoteConnect. The code is as follows:

Current PHP framework support for unit testing

At present, many excellent PHP frameworks (such as Zend Framework,Symfony, etc.) provide good support for unit testing. Take Zend Framework as an example to illustrate how unit tests are run.

The above code is actually a unit test of the framework of Zend itself, you can see that in Zend, the controller of Zend is tested by inheriting Zend_Test_PHPUnit_ControllerTestCase, you can see that the unit test in zend is similar to that in PHPUnit, but some new assertions have been added, such as assertController above, for details, you can refer to the reference manual of Zend.

PHPUnit is a lightweight PHP testing framework. It is a complete migration of JUnit3 series versions under PHP5 and is a member of the xUnit testing framework family (they are all based on the design of pattern pioneer Kent Beck).

Unit testing is the foundation of several modern agile development methods, making PHPUnit a key tool for many large PHP projects. This tool can also be used by Xdebug extensions to generate code coverage reports, integrate with phing for automated testing, and finally integrate with Selenium for large-scale automated integration testing.

At this point, I believe you have a deeper understanding of "the installation and use of PHPUnit in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report