Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expectOutputString or expectOutputRegex in PHPUnit

Hi I am using PHPUnit for my unit testing

I have a problem regarding testing output using the expectOutputString/expectOutputRegex method

Problem :

function test_myTest() {
    $this->expectOutputString('testxzxzxzxzxz');
    $this->expectOutputString('test');
    echo 'test';
}

This test pass when i generate unit test report even though the first expectation fails

Unlike in the assert methods, test fail if there is one assertion that fails

Example assertTrue :

// this test fail because the first assertTrue fails
function test_myAssert() {
  $this->assertTrue(false);
  $this->assertTrue(true);
}

Looks like this is a lacking functionality in PHPUnit..

Anyone have ideas or alternative way to achieve what I want when testing output?

like image 469
Annie B. Avatar asked Dec 20 '11 02:12

Annie B.


1 Answers

expectOutputString stores the given string to compare against the output of the whole test, but it overwrites any previously stored string. In other words, only the last call to expectOutputString has any effect. You must build the full output string and call expectOutputString just once.

function test_myTest() {
    $this->expectOutputString('testxzxzxzxzxz' . 'test');
    echo 'test';
}

The above will fail because testxzxzxzxzxztest does not equal the output test.

like image 114
David Harkness Avatar answered Dec 11 '22 04:12

David Harkness