Reputation: 2591
The results/errors in beforeAll()
and afterAll()
are currently added to respectively the first and last test. In our case quite a lot already happens in these methods. If an error occurs in e.g. the beforeAll
method, it looks like the first test has failed, while in fact the test hasn't even started.
Is it somehow possible (using a custom reporter?) to report them separately? This would make debugging and resolving issues much easier for us.
Upvotes: 1
Views: 72
Reputation: 11
You can use custom reporter to log specific test failures. check it here.
Otherwise, You can skip the test if failed in the hooks. Just add try and catch to your before hook, and then use test.skip() within the catch block. As follows.
test.beforeAll(async ({ browser }) => {
const context = await browser.newContext();
page = await context.newPage();
try{
await page.goto('http://aaaaaaaa.cosm'); //wrong url
}
catch (error)
{
console.log('Env Setup failed: ',error.message);
test.skip();
}
});
This will skip the whole test.
Upvotes: 0
Reputation: 327
If you add the beforeAll
or afterAll
hooks to the test, it will be reported within that test. This makes sense as it is setting the state for the test, meaning, like Fixtures, it is apart of the current test.
If you wish to break down the reporting, you could use test.steps
, to break out the specifics in your reports:
test('example', async ({ page }) => {
await test.step(`do thing 1`, async () => {
const heading1 = await page.getByRole("heading", {name: "header1"});
await heading1.click();
});
await test.step(`do thing 2`, async () => {
const heading2 = await page.getByRole("heading", {name: "header2"});
await heading2.click();
});
This would break your reports up into structured reports, with a section breaking down your pre/post hooks, but would still be in the test itself.
But, in your case, I feel you are trying to use hooks in the way a Setup/Teardown would be used, where it does an action and has a separate trace/entry in the reports. If you really want this abstracted off, you should implement project or global setup/teardowns to achieve this separation.
This follows the idea that a test includes the actions before/after, as that is contained within the scope of the current test.
Setup/teardown will follow a separate scope therefore will have a separate entry.
Upvotes: 0