2

Hello again SO!

I'm trying to get PHPunit to run on localhost, here are some of my specs

xDebugger : v 2.2 (enabled)
php : 5.4.3
PHPunit : tried with 3.7.31 && 4.0.17

Running tests works fine, However whenever I use the coverage-html the output is always 0% covered. I've tried this with both version of PHPunit.

Whenever i try the --coverage-text command I get the same result, the tests run fine(fail/success), however the coverage is 0%.
1 test - 1 assertion - 0

For simplicity, I created these two classes :

class my
{
  function method()
   {
    $bool = true;
    echo $bool;
   }
}

and the test class :

require_once 'my.php';
class myTest extends PHPUnit_Framework_TestCase
{
function testequal()
    {
    $bool = true;
    echo $bool;
    $this->assertTrue($bool);
    }
}

two different files, the file names are my.php and myTest.php.
If I can provide anymore information please let me know, Thanks in advance.

1 Answer 1

3

You're actually not testing the code of my. Isn't it? That's why the coverage is 0%.

Change the test code to this:

require_once 'my.php';
class myTest extends PHPUnit_Framework_TestCase
{
    function testSomething()
    {
        $object = new my();
        $this->assertEquals('1', $object->method());
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I feel utterly stupid, Ofcourse! :) So the names of the test function / real functions doesn't have to match?
No problem ;) No, they don't have to match. But of course they can. But when tests are getting more complex, you'll often write multiple test methods to test one single method. At least then having matching names will not work anymore. Have changed it to a foo-name to avoid misunderstandings..
Checking 'echo'ed output also isn't the best way to run that either. Just returning $bool from my::method, and then $this->assertEquals(true, $object->method()); would be more normal as a test. Indeed, PHPUnit has an option to throw a warning when a function actually outputs anything.
Thanks alister for that clarification, That echo was something that remained after many fiddles, return is naturally much better than echo, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.