Running unit test shows an error
Ratko-Solaja opened this issue · 5 comments
Describe your bug
After doing a clean install of wp-pest and running the command vendor/bin/pest --group=unit
- the following error appears:
Steps to Reproduce
- Install wp-pest:
composer require dingo-d/wp-pest-integration-test-setup --dev
- Setup wp-pest:
vendor/bin/wp-pest setup theme
- Run the unit test:
vendor/bin/pest --group=unit
Expected behavior
It should do the tests.
Environment info
- macOS Ventura 13.1
- PHP 7.4 or 8.0
Please confirm that you have searched existing issues in this repo.
Yes
@Ratko-Solaja for now it looks to be an upstream issue. I'm not sure if this will be fixed, or if this was intended to be done in this way, but based on what I'll get as an answer I might have to limit the functionality of this package to integration tests only :/
As an intermediate step, you could try to see if modifying the bootstrap.php
file could help. The uses()
for the unit tests should stay as is (so that the unit tests are using the BrainMonkey base test class), but still load WordPress, even though you wouldn't use it. It would be a bit of an overhead (loading WordPress for unit tests), but I think it could work (will try to test it out).
The above suggestion won't work. In that case even the unit tests will use the integration test base test class :/
@Ratko-Solaja I think I found a dirty workaround that should do the trick.
In your integration tests, before the uses()
statement add
if (!empty($GLOBALS['argv']) && $GLOBALS['argv'][1] === '--group=unit') {
return;
}
That way, you'll bail out of the integration test file during unit tests.
EDIT:
You can also create a helper in the Pest.php
file
function isUnitTest() {
return !empty($GLOBALS['argv']) && $GLOBALS['argv'][1] === '--group=unit';
}
And then just use in your integration test:
<?php
use Yoast\WPTestUtils\WPIntegration\TestCase;
if (isUnitTest()) {
return;
}
uses(TestCase::class);
// Rest of the tests.
Looks a bit cleaner 😄
I have stumbled upon this error today and after many attempts I think that I found what causes that error.
By default phpunit.xml.dist
has set bootstrap
attribute to tests/bootstrap.php
and that's fine. However Pest loads tests/Pest.php
before bootstraper. That's why using Yoast TestCase
or anything else from WordPress testing repository in Pest.php
file throws errors, because it's not yet loaded.
Switching attribute to bootstrap="vendor/autoload.php"
and requiring tests/bootstrap.php
on top of tests/Pest.php
file solved that issue for me.
I will try to test this and if this works update the lib 👍🏼
Thanks!