PHPUnit provider does not work with dependencies

I am using PHPUnit 3.4.9, but I am having some annotation issues @depends. It works, as in the examples, but breaks when the manufacturer imposes on the supplier. I don't know if this is intended to work or not, but my code is basically in the form of:

<?php
    class StackTest extends PHPUnit_Framework_TestCase
    {
      /**
       * @dataProvider provider
       */
      public function testEmpty ($data)
      {
        $stack = array();
        $this->assertTrue(empty($stack));

        return $stack;
      }

      /**
       * @depends testEmpty
       */
      public function testPush (array $stack)
      {
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertFalse(empty($stack));

        return $stack;
      }

      /**
       * @depends testPush
       */
      public function testPop (array $stack)
      {
        $this->assertEquals('foo', array_pop($stack));
        $this->assertTrue(empty($stack));
      }

      public function provider ()
      {
        return array(
           // Some testing data here...
        );
      }
    }

The code above is just an example, but shows what my code structure looks like. When he ran, he skipped consumer tests, acting as if the manufacturer had failed. I expect that for each set of testing data in the provider, the manufacturer will work with this data, and all its consumers will work accordingly.

+3
source share
3

2 , :

, , .

@depends , . @annotations .

( phpunit, 100%), @depends " " , , "testEmpty" .

, , , , - "-".

<?php
class StackTest extends PHPUnit_Framework_TestCase {
  /**
   * @dataProvider provider
   */
  public function testEmpty($data) {
    $stack = array();
    $this->assertTrue(empty($stack));
    $this->nextTestOrJustAllInOneTestcaseSaidly($stack);
    return $stack;
  }

  protected function nextTestOrJustAllInOneTestcaseSaidly($data) { ... }

, - ;)

+3

, ( , ), .

, ( ) , .

<?php
class StackTest extends PHPUnit_Framework_TestCase {
    protected static $foo;

    public function provider() { ... }

    /**
     * @dataProvider provider
     */
    public function testOne( $data ) {
        self::$foo = array();
        $this->assertTrue( empty( self::$foo ) );
    }

    /**
     * @depends testOne
     */
    public function testTwo() { 
        $this->assertTrue( empty( self::$foo ) );
    }

, , , , .

+2

, . , . , , , , .

@dataProvider @depends. , , . ​​, .

On the other hand, such a test setup may not be obvious in the first place, but the tests should be easy to understand. Consider refactoring tests.

0
source

Source: https://habr.com/ru/post/1759683/


All Articles