Mengoptimalkan test suite: parallel execution, sharding, caching, dan strategi untuk menjalankan ribuan tests dalam hitungan menit

Setelah di episode 15 kita menguji cross-browser, pada episode ini kita memecahkan masalah utama test automation: waktu. Test suite yang lambat mengurangi velocity development.
// playwright.config.ts
export default defineConfig({
fullyParallel: true, // Jalankan tests dalam parallel
workers: process.env.CI ? 4 : undefined, // Limit workers
retries: process.env.CI ? 2 : 0,
});# Default: jumlah CPU cores
npx playwright test
# Custom workers
npx playwright test --workers=4
# Serial (debugging)
npx playwright test --workers=1# .github/workflows/test.yml
jobs:
test:
strategy:
matrix:
shard: [1, 2, 3, 4]
steps:
- run: npx playwright test --shard=${{ matrix.shard }}/4# Split test suite
npx playwright test --shard=1/4
npx playwright test --shard=2/4
npx playwright test --shard=3/4
npx playwright test --shard=4/4Speed Optimizations:
├── parallel execution (fullyParallel: true)
├── reduce browser launches (reuse contexts)
├── cache dependencies (npm, browsers)
├── skip slow tests di CI
├── use API tests instead of E2E untuk coverage
├── mock external services
└── optimize test data setup// Reuse browser context
test.describe('Feature Group', () => {
let context: BrowserContext;
test.beforeAll(async ({ browser }) => {
context = await browser.newContext();
});
test.afterAll(async () => {
await context.close();
});
test('test 1', async () => {
const page = await context.newPage();
// ...
});
});# Profile test execution
npx playwright test --reporter=json > profile.json
# Analyze slow tests
node analyze-profile.js profile.jsonTarget Performance:
├── Full suite: < 10 minutes
├── Smoke tests: < 2 minutes
├── Per-test: < 30 seconds average
├── CI pipeline: < 15 minutes total
└── Feedback loop: < 5 minutesTip
Mulai dengan parallel execution — ini memberikan boost terbesar dengan effort minimal. Setelah itu, optimasi berdasarkan profiling results.
// playwright.config.ts
export default defineConfig({
fullyParallel: true,
workers: process.env.CI ? 4 : undefined,
retries: process.env.CI ? 2 : 0,
timeout: 30000, // 30 seconds per test
expect: {
timeout: 5000, // 5 seconds per assertion
},
reporter: [
['html', { open: 'never' }],
['json', { outputFile: 'results.json' }],
],
});fullyParallel: true + worker limit.Di episode 17 selanjutnya kita akan membahas test reporting & dashboards — Allure reports, metrics, dan observability untuk test suite. Sampai jumpa di episode 17!