test.only('should only run this test', async ({ page }) => { // 測試代碼 }); });
2-1 test.skip
test.skip 是一個用來跳過某個測試用例的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 跳過特定瀏覽器的測試 test('should work on all browsers except WebKit', async ({ browserName, page }) => { test.skip(browserName === 'webkit', 'This test is not applicable for WebKit'); await page.goto('https://example.com'); const title = await page.title(); expect(title).toBe('Example Domain'); });
// 跳過特定條件下的測試 test('should skip this test if condition is met', async ({ page }) => { const condition = true; // 根據實際情況設置條件 test.skip(condition, 'Skipping this test due to condition being true'); await page.goto('https://example.com'); const title = await page.title(); expect(title).toBe('Example Domain'); });
2-2 test.only
test.only 是一個用來只運行某個測試用例的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
test.describe('Feature Tests', () => { // 只運行這個測試組中的測試 test.only('should run only this test', async ({ page }) => { await page.goto('https://example.com/feature'); const featureText = await page.innerText('.feature'); expect(featureText).toBe('Feature works!'); });
test('this test will be ignored', async ({ page }) => { await page.goto('https://example.com/feature'); const featureText = await page.innerText('.feature'); expect(featureText).toBe('Feature works!'); }); });