I have a couple of really basic tests written in PHPUnit and for some reason when I enable one of the assertions, I get "No Tests Executed."
I'm not a skilled unit tester, so it's clearly something I'm doing wrong, but I'm having a hard time tracking down a solution.
I'm testing for a string coming back from a Soap server it's either "true" or "false."
In my unit test if I write this
$hasErrors = 'false';
$this->assertTrue('true' == $hasErrors);
I get "No tests executed!"
If I do the following - I know it's not the same test but it is successful and PHPUnit reports it as successful - I'm just curious why this one runs the tests, while that previous doesn't just show a failure but says no test are executed.
$hasErrors = 'false';
$this->assertFalse('true' == $hasErrors);
I would like the failure to fail rather say "No tests executed."
The same thing happens if I use assertNotEquals rather than assertFalse.
More information. I might have stumbled across a phpunit bug when dealing with a text string "true." I think php unit is wanting to treat it as a boolean rather than a string. So I made some code changes to work with booleans.
$result = $xmlresponse->xpath('//result');
$hasErrors = (string) $result[0]->hasErrors;
if ($hasErrors == 'true') {
$errors = TRUE;
} else {
$errors = FALSE;
}
$this->assertFalse($errors);
Now I get.
PHP Fatal error: Uncaught Failed asserting that true is false.
Why do I need to "catch" this failed asserting? Shouldn't phpunit just report that the test failed?
I'm executing phpunit like this from cli.
phpunit --verbose --debug --configuration testSuite.xml
testSuite.xml looks like this:
<phpunit>
<testsuites>
<testsuite name="connector">
<file>TestNewAccount.class.php</file>
<file>TestModifyAccount.class.php</file>
</testsuite>
</testsuites>
</phpunit>
Update So I guess my expectations of what PHPunit does out the box are not valid. I expected that phpunit would run the tests, report success and failures and that's that. But apparently it reports successes and throws fatal exceptions on an assertion failure. I don't understand why it's so heavy handed with failures and why it doesn't simply report the failure without me having to catch exceptions in my tests and handle it on my own.
I'm sure I'm still missing something, but for now, I'm not super impressed with phpunit out of the box. I guess it works but it seems to make me work harder than I want to in order to write a few simple tests and assertions.
Update 2 So I'm getting the response from phpunit that I was originally expecting. Now the failures are reporting as failures. I'm not sure what I'm doing differently now than I was before. Now I feel like I'm just cluttering up Stackoverflow with my own senseless rambling. Thanks for the help.
public function test*
are executed. Hope this help – Matteo