Rayrun
← Back to Discord Forum

Which one is faster - using shards or workers to run in parallel?

Hi team, I'm checking this page: https://www.cuketest.com/playwright/docs/test-parallel/ and I just wanted feedback on which one should be used: sharding or using the workers ?

This thread is trying to answer question "Which one should be used for running tests in parallel: sharding or using the workers?"

6 replies

These serve a different purpose, if your current machine is at max capacity then adding additional workers will only slow down the tests and introduce errors so you will need to shard

But if it's not at max capacity then you can add additional workers and perhaps sharding is not neccesary

with workers you're scaling on single machine level, with sharding you're scaling across different machines; both of these options can work together it's not like you need to choose workers or shards

Can shards be used locally? Only in CI environment I thought.

you can use it locally but why would you?

Just asking

Answer

You can run your tests in parallel using either sharding or workers in @playwright/test.

Sharding splits your test suite into smaller subsets, or shards, each running on a separate machine or process. This is great for distributing the workload across multiple machines and speeding up large test suites. However, it requires additional setup and coordination.

// Example of sharding in @playwright/test
// This is a conceptual example and may not work as-is
const shard = process.env.SHARD || '0';
const totalShards = process.env.TOTAL_SHARDS || '1';

// In your test file
test.describe.serial(`Shard ${shard} of ${totalShards}`, () => {
  // Your tests here
});

Workers, on the other hand, run tests concurrently within a single machine or process. Each worker process is independent and starts its own browser instance. You can control the number of workers using the --workers command line option or workers configuration option in the playwright.config.ts file.

// Example of using workers in @playwright/test
import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  // Other config options...
  workers: 4, // Number of worker processes
};

export default config;

Workers simplify setup as there's no need for distributing tests across different machines. However, they can't share state or global variables.

So, whether you choose sharding or workers depends on your specific needs. If you have multiple machines, sharding could be a good option. If you're running tests within a single machine or process, workers might be simpler.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].