PHPUnit installation and use example

Source: Internet
Author: User
PHPUnit is an open-source software developed in PHP programming language and a unit testing framework. PHPUnit was created by SebastianBergmann and originated from the SUnit of KentBeck. It is one of the xUnit family frameworks. This article introduces

PHPUnit is an open-source software developed in PHP programming language and a unit testing framework. PHPUnit was created by Sebastian Bergmann and originated from Kent Beck's SUnit. It is one of the xUnit family frameworks. This article introduces

PHPUnit is a testing framework officially supported by zend. It ensures the foundation of project quality in high-quality unit tests, effectively reduces bugs and improves programs.

Install PHPUnit:

Under the php Directory:

The Code is as follows:


Pear channel-discover pear;
Pear install phpunit/PHPUnit

Add the php environment variable to the PATH environment variable in windows.
Easy to use:

The Code is as follows:


<? Php
Class StackTest extends PHPUnit_Framework_TestCase
{

Public function testArray ()
{
$ Stack = array ();
$ This-> assertEquals (0, count ($ stack ));

Array_push ($ stack, 'foo ');
$ This-> assertEquals ('foo', $ stack [count ($ stack)-1]);
$ This-> assertEquals (1, count ($ stack ));

$ This-> assertEquals ('foo', array_pop ($ stack ));
$ This-> assertEquals (0, count ($ stack ));
}

/**
* @ Test
*/
Public function Stringlen ()
{
$ Str = 'abc ';
$ This-> assertEquals (3, strlen ($ str ));
}
}

From the above, we can see the basic rules for compiling PHPUnit:
(1) Test the Class in ClassTest
(2) ClassTest inherits from PHPUnit_Framework_TestCase
(3) The test method is in the test * format. You can use @ test to mark it as a test method.
(4) assertEquals can be used to assert the actual and expected values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.