mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00

This patch: - makes environment a simple class with optional methods `beforeEach`, `afterEach`, `beforeAll`, `afterAll`, `globalSetup` and `globalTeardown` - removes capability to have multiple hooks of the same name inside suite - removes default environment for test. (`dit` now adds a `TraceTestEnvironment` to the test) - extracts all environments that we use in our tests in `//test/environments.js` Downsides: - we no longer know hook locations for the environments. This, however, should not be a big deal since stack traces (if any) will still point into it. - this also regresses hook locations for suites for simplicity. We can get them back, but it shouldn't be pressing since we now have only one hook of each kind in every suite.
TestRunner
This test runner is used internally by Playwright to test Playwright itself.
- testrunner is a library: tests are
node.js
scripts - parallel wrt IO operations
- supports async/await
- modular
- well-isolated state per execution thread
Example
Save the following as test.js
and run using node
:
node test.js
const {TestRunner, Reporter, Matchers} = require('.');
// Runner holds and runs all the tests
const runner = new TestRunner({
parallel: 2, // run 2 parallel threads
timeout: 1000, // setup timeout of 1 second per test
});
// Simple expect-like matchers
const {expect} = new Matchers();
// Extract jasmine-like DSL into the global namespace
const {describe, xdescribe, fdescribe} = runner;
const {it, fit, xit} = runner;
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
// Test hooks can be async.
beforeAll(async state => {
state.parallelIndex; // either 0 or 1 in this example, depending on the executing thread
state.foo = 'bar'; // set state for every test
});
describe('math', () => {
it('to be sane', async (state, test) => {
state.parallelIndex; // Very first test will always be ran by the 0's thread
state.foo; // this will be 'bar'
expect(2 + 2).toBe(4);
});
});
// Reporter subscribes to TestRunner events and displays information in terminal
const reporter = new Reporter(runner);
// Run all tests.
runner.run();