dingo-d / wp-pest

A package that will add WordPress integration test suite with Pest
MIT License
119 stars 5 forks source link

Running both Unit and Integration causes integration test to fail #4

Closed palmiak closed 11 months ago

palmiak commented 2 years ago

Describe your bug

When I run vendor/bin/pest --group=integration it works perfectly. But when running vendor/bin/pest it gives one failed test (the integration one).

It's all because of bootstrap.php where there is this if: if ( ( isset($GLOBALS['argv']) && isset($GLOBALS['argv'][1]) && strpos($GLOBALS['argv'][1], 'integration') !== false ) ) {

It causes that all the setup is prepared only if we run group integration. Maybe it should be done another way around - skip the setup only for selected groups.

Steps to Reproduce

  1. just run vendor/bin/pest - it should bring a failed test

Expected behavior

Either no fail (by running the setup) or skipping the integration group by default.

Screenshots, screen recording, code snippet

No response

Environment info

Both Windows and CI env.

Please confirm that you have searched existing issues in this repo.

Yes

dingo-d commented 2 years ago

Yeah, as noted in the Readme this is the expected result. If you want to run unit tests you need to provide --group=unit flag.

The problem is that I don't need to use the bootstrapping process of the entire WP in unit tests and this is the only way I can differentiate if you're running integration or unit tests.

I usually set up composer scripts in my project like:

"scripts": {
    "test:unit": "@php ./vendor/bin/pest --group=unit",
    "test:integration": "@php ./vendor/bin/pest --group=integration",
}

So I can just run composer run test:unit or composer run test:integration as a shorthand 🙂

And by default just running pest will run all the tests, no matter what the grouping is. I'll try to see if I can get some information on whether the test file is located in the integration folder or something else instead of relying on the group parameter.

palmiak commented 2 years ago

But wouldn't it be better to run the boostrap.php another way around? Something like:

if ( ( isset($GLOBALS['argv']) && isset($GLOBALS['argv'][1]) && strpos($GLOBALS['argv'][1], 'unit') !== false ) ) {
    // bail out early
    return;
}

This would solve the problem - of course instead of unit it would be great to have an array.

dingo-d commented 2 years ago

The issue is that just running vendor/bin/pest will run all the test suites (unit and integration), and even though the unit tests would work with the integration test setup, it's unnecessary to load the integration test bootstrap and use the default test case for unit tests (although that can be argued because the default test case offers brain monkey helpers that are useful in the unit tests - this is me thinking out loud 😂).

I'd definitely avoid bootstrapping the entire WP for unit tests, as that does have an impact on the test speed.

That's why I'm grouping them like that.