i'm using PHPUNIT 3.4.5, and i would like to get the total number of assertions done by my tests (3 assertions for my sample code). The method "getNumAssertions" keeps returning 0.
public function test_DateUtil()
{
echo "\nNumAssertions : " . $this->getNumAssertions() . "\n";
$this->assertEquals(1, 1);
echo "NumAssertions : " . $this->getNumAssertions() . "\n";
}
public function test_DateUtil_2()
{
echo "\nNumAssertions : " . $this->getNumAssertions() . "\n";
$this->assertEquals(1, 1);
$this->assertEquals(1, 1);
echo "NumAssertions : " . $this->getNumAssertions() . "\n";
}
}
PHPUnit returns :
phpunit --configuration phpunit_dev.xml --verbose
PHPUnit 3.4.5 by Sebastian Bergmann.
UtilTest
NumAssertions : 0
NumAssertions : 0
.
NumAssertions : 0
NumAssertions : 0
.
Time: 1 second, Memory: 13.50Mb
OK (2 tests, 3 assertions) ------------> what i want to get
You should use PHPUnit_Framework_Assert::getCount()
public function test_DateUtil()
{
echo "\nNumAssertions : " . PHPUnit_Framework_Assert::getCount() . "\n";
$this->assertEquals(1, 1);
echo "NumAssertions : " . PHPUnit_Framework_Assert::getCount() . "\n";
}
Your code is not producing the expected output because the method PHPUnit_Framework_TestCase::addToAssertionCount is called outside of the test itself, and it is not called until the completion of the test.
Therefore, you will not be able to get the assertion count from within the test method using getNumAssertions().
You will find the call to
$test->addToAssertionCount(PHPUnit_Framework_Assert::getCount());
near the end of the method
PHPUnit_Framework_TestResult::run
EDIT
The output after running all tests is done by PHPUnit_TextUI_ResultPrinter which contains the protected property $numAssertions. There is no accessor(getter) for this property.
You can extend the PHPUnit framework and add this functionality.
One option would be to extend the PHPUnit_Framework_TestCase class, overload the run method and increment a custom total assertions counter from there.
Another option would be to extend the PHPUnit_TextUI_ResultPrinter class similar the HTML Result Printer in this article.
If you want to send an email of the entire output without extending PHPUnit, you can pipe the results to an email application.
There is also jenkins, which can handle this as well as much more.
NOTE
I am using 3.3.17
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With