0
votes

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.

2
Can you post the class or the signature of the test method name? Only test named like public function test* are executed. Hope this helpMatteo
It's executing tests sometimes. public function testModifyAccount() { ... }Halfstop
I updated the question to be more clear. The first test executes as "No tests executed" while the second test succeeds as it should. Why doesn't the first one fail.Halfstop

2 Answers

2
votes

It's good practice to use a quite specific test - assertNotEquals('x', 'y') rather than assertFalse('x'=='y') - having to think about one thing is better than having to then also translate that to a different condition.

When I create a new test class, I tend to put a couple of sanity-checks in, to make sure the test itself is being run, something as simple and obviously failing as 'assertTrue(false)'. If that doesn't fail - then there is something very wrong going on. I've often found it to be name (even upper/lower-case) related.

To debug what is going on, I'd try to get down to the simplest, smallest possible that that does not do what you expect. Debug it from there, or have someone else take a look. The "other person" could equally be a Rubber Duck that you talk it through to, step, by step.

0
votes

I figured this out. It had nothing to do with PHPUnit as I began to suspect. There's just no way this is only happening to me and it be PHPUnit's fault. I'm doing anything remarkable with PHPUnit.

The problem was that I am making curl calls that had too short of a timeout set. The timeout was set to 10 seconds, but the service I was hitting needed like 2 minute wait times from time-to-time (it's a machine-to-machine sort of thing, so not a huge deal). The short timeout was being hit and PHPUnit would report No Tests Executed!