Skip to content

Dependency hierarchy branches affect eachother #1057

Closed
@RSSchermer

Description

@RSSchermer

I may be doing something wrong or maybe I'm missing something. This is a very simplified version of the type of class I'm trying to test:

class SomeStack
{
    private $items = array();

    public function addItem($item)
    {
        $this->items[] = $item;
    }

    public function getItems()
    {
        return $this->items;
    }
}

And the test:

class SomeStackTest extends \PHPUnit_Framework_TestCase
{
    public function testStackInitiallyEmpty()
    {
        $stack = new SomeStack();

        $this->assertEmpty($stack->getItems());

        return $stack;
    }

    /**
     * @depends testStackInitiallyEmpty
     */
    public function testAddItem($stack)
    {
        $stack->addItem('SomeItem');

        $this->assertNotEmpty($stack->getItems());
    }

    /**
     * @depends testStackInitiallyEmpty
     */
    public function testSomethingElseThatWantsAnEmptyStack($stack)
    {
        $this->assertEmpty($stack->getItems()); // Fails
    }
}

In the example there's two different 'dependency hierarchy branches'. I was expecting to receive an empty stack in testSomethingElseThatWantsAnEmptyStack, but instead it seems that the earlier branch affected the state of the stack. Is this intended behavior? Am i doing something wrong? Is this a known limitation that is unfortunately a necessary evil for performance reasons? (I think the dependency would need to be rerun for every branch?)

There are workarounds of course, but they don't seem as elegant as having multiple tests that depend on the same 'parent' test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions