Phpunit unit test

Source: Internet
Author: User

1. Install ubuntu 12.04

wget https://phar.phpunit.de/phpunit.pharchmod +x phpunit.pharmv phpunit.phar /usr/local/bin/phpunit

2. Test Case phpunit1.php test dependency)

Demonstrate how to use @ depends annotation to express dependencies between test methods

<?phpclass StackTest extends PHPUnit_Framework_TestCase{    public function testEmpty()    {        $stack = array();        $this->assertEmpty($stack);                                                                                                                                     return $stack;    }                                                                                                                                 /**     * @depends testEmpty     */    public function testPush(array $stack)    {        array_push($stack, 'foo');        $this->assertEquals('foo', $stack[count($stack)-1]);        $this->assertNotEmpty($stack);                                                                                                                                     return $stack;    }                                                                                                                                 /**     * @depends testPush     */    public function testPop(array $stack)    {        $this->assertEquals('foo', array_pop($stack));        $this->assertEmpty($stack);    }}

3. Test Results

phpunit phpunit1.phpPHPUnit 3.7.27 by Sebastian Bergmann....Time: 4 ms, Memory: 4.25MbOK (3 tests, 5 assertions)

4. to quickly locate defects, we hope to focus on relevant failure tests. This is why PHPUnit skips the test when the test on which a test depends fails. Through the dependency between tests, defect locating is improved.

The following example shows phpunit2.php:

<?phpclass DependencyFailureTest extends PHPUnit_Framework_TestCase{    public function testOne()    {        $this->assertTrue(FALSE);    }                                      /**     * @depends testOne     */    public function testTwo()    {    }}?>

5. Execution result

phpunit phpunit2.phpPHPUnit 3.7.27 by Sebastian Bergmann.FSTime: 2 ms, Memory: 4.00MbThere was 1 failure:1) DependencyFailureTest::testOneFailed asserting that false is true./home/wwwroot/local.guazi.com/webroot/phpunit2.php:6FAILURES!Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.

6. More than one @ depends annotation can be used for testing. PHPUnit does not change the test running sequence, so you need to ensure that all tests on which a test depends appear before this test.

There are multiple tests marked by @ depends. The first parameter is the basis provided by the first producer, the second parameter is the basis provided by the second producer, and so on.

Case study: phpunit3.php

<?phpclass MultipleDependenciesTest extends PHPUnit_Framework_TestCase{    public function testProducerFirst()    {        $this->assertTrue(true);        return 'first';    }             public function testProducerSecond()    {        $this->assertTrue(true);        return 'second';    }             /**     * @depends testProducerFirst     * @depends testProducerSecond     */    public function testConsumer()    {        $this->assertEquals(            array('first', 'second'),            func_get_args()        );    }}?>

7. Execution result

phpunit phpunit3.phpPHPUnit 3.7.27 by Sebastian Bergmann....Time: 4 ms, Memory: 4.25MbOK (3 tests, 3 assertions)


This article is from the "phper" blog, please be sure to keep this source http://janephp.blog.51cto.com/4439680/1298842

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.