Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the reason for a risky test in PHPUnit

Tags:

php

phpunit

From time to time I get a risky test in PHPUnit. Usually I can find the reason for a risky test. But the task can be time consuming, because I do not see any messages from PHPUnit, why a test is marked as risky. I only get something like this:

PHPUnit 4.4.5 by Sebastian Bergmann.  Configuration read from phpunit.xml.dist  R...................R.R...  Time: 11,91 seconds, Memory: 42,50Mb  OK, but incomplete, skipped, or risky tests! Tests: 26, Assertions: 32, Risky: 3.         

Is there any option to tell PHPUnit to show messages or better something like stack traces to the code causing the risky flag? A complete list of causes for a risky test might prove helpful, too.

like image 734
Trendfischer Avatar asked May 11 '15 08:05

Trendfischer


People also ask

What is assertion in PHPUnit?

The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.

Which method is used to create a mock with PHPUnit?

PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.

What is unit testing in laravel?

Unit tests are tests that focus on a very small, isolated portion of your code. In fact, most unit tests probably focus on a single method. Tests within your "Unit" test directory do not boot your Laravel application and therefore are unable to access your application's database or other framework services.


2 Answers

This question deserves an answer, taken from comments above.

phpunit -v 

This gives a short description of the reason for the risky test

 -v|--verbose              Output more verbose information. 
like image 187
tread Avatar answered Oct 12 '22 22:10

tread


As of PHPUnit 5.3, the complete list of risky reasons:

  • A test makes no assertions, when being strict about tests that do nothing. (Source.)
  • A test marked with @todo, when being strict about TODO annotations. (Source.)
  • Using a function that operates on a resource, when running a small test and being strict about resource usage. (Source: thrown here based on this condition.)
  • Code does not close exactly the output buffers it opened. (Source.)
  • Code executes methods not explicitly listed in @covers for the test. (Source. A blog post on this scenario.)
  • Code manually throws a PHPUnit_Framework_RiskyTestError.
  • Code manually throws a PHPUnit_Framework_UnintentionallyCoveredCodeError.
like image 22
bishop Avatar answered Oct 12 '22 22:10

bishop