What is the difference between sharding and using workers in @playwright/test?


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.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

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 luc@ray.run.