Summer Sale! All accounts are 50% off this week.

CLab's avatar
Level 3

Is there a way to run tests in parallel with filters?

I want to run a single test class in parallel. Laravel 8 has new feature to run tests in parallel using command php artisan test --parallel.

php artisan test also has the ability to filter the test which you want to run using the --filter flag.

When I try to combine the two commands using php artisan test --parallel --filter=MyTestClass it gives me an error:

In Options.php line 379:

  Option --filter is not implemented for non functional mode

paratest [--bootstrap BOOTSTRAP] [--colors] [-c|--configuration CONFIGURATION] [--coverage-clover COVERAGE-CLOVER] [--coverage-cobertura COVERAGE-COBERTURA] [--coverage-crap4j COVERAGE-CRAP4J] [--coverage-html COVERAGE-HTML] [--coverage-php COVERAGE-PHP] [--coverage-test-limit COVERAGE-TEST-LIMIT] [--coverage-text [COVERAGE-TEXT]] [--coverage-xml COVERAGE-XML] [--exclude-group EXCLUDE-GROUP] [--filter FILTER] [-f|--functional] [-g|--group GROUP] [-h|--help] [--log-junit LOG-JUNIT] [--log-teamcity LOG-TEAMCITY] [-m|--max-batch-size MAX-BATCH-SIZE] [--no-coverage] [--no-test-tokens] [--order-by [ORDER-BY]] [--parallel-suite] [--passthru PASSTHRU] [--passthru-php PASSTHRU-PHP] [--path PATH] [-p|--processes PROCESSES] [--random-order-seed [RANDOM-ORDER-SEED]] [--repeat [REPEAT]] [--runner RUNNER] [--stop-on-failure] [--testsuite TESTSUITE] [--tmp-dir TMP-DIR] [-v|vv|--verbose] [--whitelist WHITELIST] [--] [<path>]

Is there a way to run a single class in parallel?

0 likes
18 replies
mabdullahsari's avatar

Wait, what? You want to run a single test class in parallel?

How would that even work? I'm confused. This doesn't make sense.

PHPUnit doesn't work that way.

CLab's avatar
Level 3

@mabdullahsari In the single test class I have several tests which take a long time to run. Is it possible to run them in parallel?

Or I suppose I would have to split up my test class?

But even in that case, is it possible to run only a handful of test classes in parallel by filtering, say a folder of classes, rather than the entire test suite?

mabdullahsari's avatar

@CLab The test cases within a file belong to a single test. You can only run the tests in parallel, not the individual cases. Since you are talking about one test, there is no point in parallelizing that.

You need to create separate tests. You should be able to run a specific test namespace is parallel, yeah.

CLab's avatar
Level 3

@mabdullahsari Hi- so my test cases are now in separate classes/files. How can I run a specific test namespace in parallel as you mentioned? What command do I use?

mabdullahsari's avatar

@CLab Create a test suite for these tests inside of phpunit.xml and run them as

php artisan test --testsuite=MyTestSuite --parallel
1 like
CLab's avatar
Level 3

@mabdullahsari So I created a test suite by adding the following lines to my phpunit.xml file:

    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
        <testsuite name="MyTestSuite">
            <directory suffix="Test.php">./tests/Feature/MyTestSuite/</directory>
        </testsuite>
    </testsuites>

I tried running php artisan test --testsuite=MyTestSuite --parallel and it gives an error:

Unknown option "--parallel"
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@CLab You need to have --parallel first

php artisan test --parallel --testsuite=MyTestSuite
3 likes
Sinnbeck's avatar

@CLab no worries. The parallel flag is a laravel one while testsuite is phpunit. So laravels comes first

1 like
Sinnbeck's avatar

If the issue was solved, you can mark a best answer to set the thread as solved

CLab's avatar
Level 3

@mabdullahsari I wish that there was a way to mark two best answers, as without your conversation we wouldn't have come to the point. But yes I will change Best answer to @sinnbeck as his works. Thank you both for taking time from your day to help a novice out.

martinbean's avatar

@clab I think you need to address the actual problem rather than the symptoms in this case.

You have slow tests. You should be looking at why your tests are slow; not trying to find workarounds to make them run faster by parallelising them. You still have slow tests; just now split across multiple classes.

So why do you have slow tests? Are they testing too much? Are they hitting external services (which they shouldn’t be)?

1 like
CLab's avatar
Level 3

@martinbean Thanks Martin. Yes I understand. I will inspect them and fix them too. Splitting it up actually increased the speed 4 fold as the setup was very heavy when everything was in one. By splitting it up the setup could be optimized for each class which made it faster.

Please or to participate in this conversation.