zoukankan      html  css  js  c++  java
  • PHPUnitTest Dependencies

    PHPUnit supports the declaration of explicit dependencies between test methods. Such dependencies do not define the order in which the test methods are to be executed but they allow the returning of an instance of the test fixture by a producer and passing it to the dependent consumers.

    • A producer is a test method that yields its unit under test as return value.

    • A consumer is a test method that depends on one or more producers and their return values.

    Using the @depends annotation to express dependencies between test methods.

     

    <?php
    class 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);
        }
    }
    ?>
    

     

    In the example above, the first test, testEmpty(), creates a new array and asserts that it is empty. The test then returns the fixture as its result. The second test, testPush(), depends on testEmpty() and is passed the result of that depended-upon test as its argument. Finally, testPop() depends upon testPush().

    To quickly localize defects, we want our attention to be focussed on relevant failing tests. This is why PHPUnit skips the execution of a test when a depended-upon test has failed. This improves defect localization by exploiting the dependencies between tests as shown below

    <?php
    class DependencyFailureTest extends PHPUnit_Framework_TestCase
    {
        public function testOne()
        {
            $this->assertTrue(FALSE);
        }
     
        /**
         * @depends testOne
         */
        public function testTwo()
        {
        }
    }
    ?>
    
    phpunit --verbose DependencyFailureTest
    PHPUnit 3.7.0 by Sebastian Bergmann.
    
    FS
    
    Time: 0 seconds, Memory: 5.00Mb
    
    There was 1 failure:
    
    1) DependencyFailureTest::testOne
    Failed asserting that false is true.
    
    /home/sb/DependencyFailureTest.php:6
    
    There was 1 skipped test:
    
    1) DependencyFailureTest::testTwo
    This test depends on "DependencyFailureTest::testOne" to pass.
    
    
    FAILURES!
    Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.
    

     A test may have more than one @depends annotation. PHPUnit does not change the order in which tests are executed, you have to ensure that the dependencies of a test can actually be met before the test is run.

  • 相关阅读:
    day_2:re
    day_1:Requests
    CTF-PHP漏洞总结(持续更新)
    BugkuCTF-Web- flag在index里 80
    中缀表达式转换为后缀表达式(C语言实现)
    逆波兰计算器1.0 (c语言 栈实现)支持小数计算
    二进制数转化为十进制数(栈的学习练习)
    数据结构:统计学生信息(c++动态链表完成)
    数据结构:删除数组中的元素(c++)链表形式
    约瑟夫环 (c++循环链表方法书写)
  • 原文地址:https://www.cnblogs.com/Hebe/p/3099694.html
Copyright © 2011-2022 走看看