Question

I am starting with unit-testing for my project, but run into difficulties. I have one test that runs perfectly when I run it alone. When I run the whole lot of tests I have, it fails: (I have removed other code and specific class/variable/function names on purpose)

class XYZTest extends PHPUnit_Framework_TestCase {
    public function setUp() {
        $xml = $this->createXML();
        //...
    }


    public function testWillFailUponRunAllTests() {
        $this->subject = new XYZ();
        $result = $this->subject->getHelpText();
        //assert this and that
    }
}

Execute single test: Runs perfectly.

Execute all tests: I get an error of a class that I have injected. This is strange, so I'd like to debug the test to see what is going on.

Debug all tests: I don't even get to the error, because setUp() already causes this error message: XYZTest::setUp(): Invalid State Error

Obviously, this happens neither when I run the test isolated, nor when I run all tests in "normal" mode. Only in debug mode!

Debug single test: I get yet another behaviour:
PHPUnit_Framework_TestResult::run(): Invalid State Error' in phar://C:/xampp/php/phpunit.phar/phpunit/Framework/TestResult.php on line 668. This happens only when I set breakpoints, not when I disable them, for example. Seems a PHPStorm bug.

I am using PHP 5.5.9, PHPStorm 7.1.3, PHPUnit 3.7.31, xDebug v2.2.3. I'm not using any PHP framework. Thanks in advance for your help!

Was it helpful?

Solution

I do not believe you have followed the tutorials on PHPUnit correctly. The setUp() function is run before each test, as each test is run in isolation. Therefore, you need to write your tests for that.

class XYZTest extends PHPUnit_Framework_TestCase {
    protected $xml;     // Internal Object for testing

    // Assume you have autoloader configured in bootstrap. If not, Require your class to test
    //require_once('YourXMLClass.php');
    public function setUp()
    {
        $this->xml = new YourXMLClass();        // Create the XML object so it exists for testing.
    }

    public function testXMLIsEmpty()
    {
        $this->assertNull($this->xml->subject);
    }

    public function testXMLSubjectSet()
    {
        $this->xml->SetSubject('Basic Text to be returned as XML subject field');
        $this->assertEquals('<subject>Basic Text to be returned as XML subject field</subject>', $this->xml->subject);
    }
}

The assumption in my test is you have a SetSubject method in your class that adds the parameter to the subject field of your XML. IE:

public function SetSubject($SubjectText)
{
    $this->subject = '<subject>' . $SubjectText . '</subject>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top