microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.18k stars 3.69k forks source link

feat(test): allow fixtures to be specified for single tests #33645

Closed dalyIsaac closed 5 days ago

dalyIsaac commented 1 week ago

The current way to specify fixture inputs for a single test is to do the following:

import { test as base, expect } from '@playwright/test';

const test = base.extend({
  input: 'defaultInput',
  foo: async ({ input }, use) => {
    await use(input);
  }
});

test.describe(() => {
  test.use({ input: 'asd' });

  test('test1', ({ foo }) => {
    expect(foo).toBe('asd');
  });
});

This PR is an attempt to address #27138 by adding fixtures to the TestDetails for a test, enabling the following:

// ...

test('test2', { fixtures: { input: 'asd' } }, ({ foo }) => {
  expect(foo).toBe('asd');
});

test('test3', ({ foo }) => {
  expect(foo).toBe('defaultInput');
});

This appeared to be the most straightforward way to specify fixtures for a single test. However, it has the downside of creating an implicit anonymous test.describe suite around each test that specifies fixtures, which may be confusing in Playwright UIs - also this is related to #30476.

If this approach is acceptable, I will update the documentation accordingly. Otherwise, I am happy to try a different approach or close this PR entirely if it's not desired.

dalyIsaac commented 1 week ago

@microsoft-github-policy-service agree

github-actions[bot] commented 1 week ago

Test results for "tests 1"

36923 passed, 650 skipped :heavy_check_mark::heavy_check_mark::heavy_check_mark:

Merge workflow run.

dgozman commented 5 days ago

@dalyIsaac Thank you for the PR! We've discussed this and decided that we are not ready to commit to such an API yet. Meanwhile we recommend manually wrapping with an async describe - I've updated #27138 to mention that.