Skip to content

Commit

Permalink
chore: catch leaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rwoll committed Jul 7, 2022
1 parent 8820f30 commit 0310e11
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/async/conftest.py
Expand Up @@ -57,13 +57,21 @@ async def launch(**kwargs):

yield launch
for browser in browsers:
if browser.contexts:
raise Exception(
"Test failed to close a context it created via browser_factory fixture. Explicitly close context, or use context_factory."
)
await browser.close()


@pytest.fixture(scope="session")
async def browser(browser_factory):
browser = await browser_factory()
yield browser
if browser.contexts:
raise Exception(
"Test failed to close a context it created via browser fixture. Explicitly close context, or use context_factory."
)
await browser.close()


Expand Down
7 changes: 7 additions & 0 deletions tests/async/test_har.py
Expand Up @@ -503,6 +503,7 @@ async def test_should_round_trip_har_zip(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_round_trip_har_with_post_data(
Expand Down Expand Up @@ -536,6 +537,7 @@ async def test_should_round_trip_har_with_post_data(
assert await page_2.evaluate(fetch_function, "3") == "3"
with pytest.raises(Exception):
await page_2.evaluate(fetch_function, "4")
await context_2.close()


async def test_should_disambiguate_by_header(
Expand Down Expand Up @@ -578,6 +580,7 @@ async def test_should_disambiguate_by_header(
assert await page_2.evaluate(fetch_function, "baz2") == "baz2"
assert await page_2.evaluate(fetch_function, "baz3") == "baz3"
assert await page_2.evaluate(fetch_function, "baz4") == "baz1"
await context_2.close()


async def test_should_produce_extracted_zip(
Expand Down Expand Up @@ -605,6 +608,7 @@ async def test_should_produce_extracted_zip(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_update_har_zip_for_context(
Expand All @@ -627,6 +631,7 @@ async def test_should_update_har_zip_for_context(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_update_har_zip_for_page(
Expand All @@ -649,6 +654,7 @@ async def test_should_update_har_zip_for_page(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_update_extracted_har_zip_for_page(
Expand All @@ -675,3 +681,4 @@ async def test_should_update_extracted_har_zip_for_page(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()
4 changes: 4 additions & 0 deletions tests/sync/conftest.py
Expand Up @@ -63,6 +63,10 @@ def browser(
) -> Generator[Browser, None, None]:
browser = browser_type.launch(**launch_arguments)
yield browser
if browser.contexts:
raise Exception(
"Test failed to close a context it created via browser fixture. Explicitly close context, or use context fixture"
)
browser.close()


Expand Down
7 changes: 7 additions & 0 deletions tests/sync/test_har.py
Expand Up @@ -438,6 +438,7 @@ def test_should_round_trip_har_zip(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_round_trip_har_with_post_data(
Expand Down Expand Up @@ -471,6 +472,7 @@ def test_should_round_trip_har_with_post_data(
assert page_2.evaluate(fetch_function, "3") == "3"
with pytest.raises(Exception):
page_2.evaluate(fetch_function, "4")
context_2.close()


def test_should_disambiguate_by_header(
Expand Down Expand Up @@ -512,6 +514,7 @@ def test_should_disambiguate_by_header(
assert page_2.evaluate(fetch_function, "baz2") == "baz2"
assert page_2.evaluate(fetch_function, "baz3") == "baz3"
assert page_2.evaluate(fetch_function, "baz4") == "baz1"
context_2.close()


def test_should_produce_extracted_zip(
Expand All @@ -537,6 +540,7 @@ def test_should_produce_extracted_zip(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_update_har_zip_for_context(
Expand All @@ -557,6 +561,7 @@ def test_should_update_har_zip_for_context(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_update_har_zip_for_page(
Expand All @@ -577,6 +582,7 @@ def test_should_update_har_zip_for_page(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_update_extracted_har_zip_for_page(
Expand All @@ -601,3 +607,4 @@ def test_should_update_extracted_har_zip_for_page(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()

0 comments on commit 0310e11

Please sign in to comment.