Skip to content
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

How to run tests in separate process? #270

Closed
dingo-d opened this issue Feb 13, 2021 · 5 comments
Closed

How to run tests in separate process? #270

dingo-d opened this issue Feb 13, 2021 · 5 comments

Comments

@dingo-d
Copy link
Collaborator

dingo-d commented Feb 13, 2021

I have looked in the documentation and here in the issues, but couldn't find any info about running the test in a separate process. The

@runInSeparateProcess

annotation doesn't seem to be working.

@nunomaduro
Copy link
Member

I am afraid Pest don't support that feature.

@dingo-d
Copy link
Collaborator Author

dingo-d commented Feb 14, 2021

Are there plans for implementing that in the future?

@nunomaduro
Copy link
Member

You can try to pull request this. It may be a little bit complicated tho.

@dingo-d
Copy link
Collaborator Author

dingo-d commented Mar 22, 2021

@nunomaduro what would be the best way to start working on implementing testing in a separate process?

My idea was to first set unit tests. they would go something like this:

Add three tests, two of those will meddle with set globals:

  • first where the globals will be set and verified that they exist and are set correctly
  • second where it will be verified that globals are still accessible in the test from the previously set test
  • third that would have the modification that would isolate the test case and run it in a separate process.

Running these would fail in the first pass ofc because the third won't be run in a separate process. Once the feature is implemented, the third test should pass.

I was thinking of adding runInSeparateProcess method to the TestCall class, similar to what only is doing.

    /**
     * Run the current test in a separate process.
     */
    public function runInSeparateProcess(): TestCall
    {
        $this->testCaseFactory->separateProcess = true;

        return $this;
    }

The TestCaseFactory would have a property

    /**
     * Identifier whether the test should be run in a separate process.
     *
     * @readonly
     *
     * @var bool
     */
    public $separateProcess = false;

Based on which we could trigger running a test in a separate process. This could (maybe) be extended to include preserving globals like in PHPUnit.

We could mark the test which should be run in a separate process like:

test('This should be run in a separate process', function(){
  // Test body.
})->runInSeparateProcess();

I've seen that PHPUnit's TestRunner has a runInSeparateProcess method, which could be utilized here.
But the problem is that I'm not familiar with how Pest handles running test cases. The TestRepository's build method seems to be responsible for creating the tests using PHPUnit\Framework\TestCase.

Any pointers on what should I look at when trying to work on this? Thanks!

@KorvinSzanto
Copy link

Is it possible to mention this discrepancy (and any others you can think of) on https://pestphp.com/docs/migrating-from-phpunit-guide ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants