-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Risky / No assertions check hinders use of Hamcrest #2744
Comments
If you do not want the risky test check then simply disable it. |
But the risky test check is useful, in case I have forgotten to use any assertions. I think it would be nice to support other assertion libraries other than PHPUnit's - as many people have done in the past - whilst also benefiting from the risky tests feature in PHPUnit. Can this be done? Or will that not be a goal of PHPUnit going forward? |
I can only assume this has been declined because listeners are not intended to be used to modify the outcome of tests. This may be why Mockery ditched its use of listeners for performing this tasks and adopted a trait instead.
Overriding use Hamcrest\MatcherAssert;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
protected function assertPostConditions()
{
$this->addToAssertionCount(MatcherAssert::getCount());
MatcherAssert::resetCount();
parent::assertPostConditions();
}
// ...
} If you are using use Hamcrest\MatcherAssert;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
use MockeryPHPUnitIntegration {
MockeryPHPUnitIntegration::assertPostConditions as mockeryAssertPostConditions;
}
protected function assertPostConditions()
{
$this->addToAssertionCount(MatcherAssert::getCount());
MatcherAssert::resetCount();
self::mockeryAssertPostConditions();
}
// ...
} It is tedious to have to do this for every test, so I extend |
+1 This last answer is the only correct answer and should in fact be added to the hamcrest-php documentation! |
Using the hamcrest assertions library (https://github.com/hamcrest/hamcrest-php) with PHPUnit 6 is proving tricky. All tests are being marked as Risky due to them not having any assertions.
Graze's hamcrest-test-listener (https://github.com/graze/hamcrest-test-listener) has added to the assertion count previously by using a test listener and calling
$test->addToAssertionCount($x)
for the number of Hamcrest assertions made in the test.
However, in PHPUnit 6 (and perhaps 5, I've not used 5) this does not work because the risky check happens before the
endTest()
method in the listeners is called.I had considered using
assertPostConditions()
but this would be limited by the single inheritance model - for example, Mockery provides a trait to verify its post conditions in that method, which could conflict with the usage for the above purpose.Is there another unobtrusive way that this could be performed using the current PHPUnit 6 feature set?
The text was updated successfully, but these errors were encountered: