views:

29

answers:

1

I have a simple unit test case (extensive question here) on a configuration class that by design triggers PHP errors on type mismatches and undefined configuration settings. In addition to the error, the method is exited returning false.

In my test case, I want to have a number of tests that fail. Trouble is, I can't do a simple 'assert' on the return value: Every error thrown during running a test will be converted into a PHPUnit_Framework_Error exception.

Now I can make PHPUnit aware that I am expecting an error:

/**
 * @expectedException PHPUnit_Framework_Error
 */

public function testSetInvalidKey()
 {
     $this->assertEquals(true, conf::set("invalid_key", "12345"));     
 } 

This works fine, but what I don't understand is that any additional assertion within that function will not be evaluated. Whether it would fail or not, PHPUnit seems to only wait for the exception to happen, and be satisfied with the whole function when it has happened.

To wit, this test will run OK:

/**
 * @expectedException PHPUnit_Framework_Error
 */

 public function testSetInvalidKey()
 { 
   // The error will be triggered here
   $this->assertEquals(true, conf::set("invalid_key", "12345"));                      
   $this->assertEquals(12345, 67810);   // Huh?    
   $this->assertEquals("abc", "def");   // Huh?
   $this->assertEquals(true, false);    // Huh?

 }

Why? Is this the intended behaviour?

I realize you would just separate the assertions into different functions, but I would like to understand the behaviour.

+2  A: 

Since conf::set() is executed within the method testSetInvalidKey() the corresponding catch block must be outside. Once caught and logged as the expected exception, I don't see how PHP could resume execution after the first assertion.

Extreme psuedo-code:

class Tester
{
  public function run()
  {
    try {
      $test->testSetInvalidKey();
    }
    catch (PHPUnit_Framework_Error $e) {
      // Expected exception caught! Woohoo!
      // How can I continue to run the above method where I left off?
    }
  }
}

This type of behavior would be a great proponent to those who believe in the 1 assertion per test axiom.

Mike B