Skip to content

Pytest coverage not working correctly when using Playwright #1012

@Mr-Pepe

Description

@Mr-Pepe

Context:

  • Playwright Version: 1.16.0
  • Operating System: Linux
  • Node.js version: 12.21.0
  • Browser: All

Code Snippet
Take the example from the Getting Started docs and put it inside a function inside tmp.py.

from playwright.sync_api import sync_playwright


def my_func():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page()
        page.goto("http://playwright.dev")
        print(page.title())
        browser.close()

Write a pytest unit test for the function inside test_tmp.py.

from tmp import my_func

def test_tmp():
    my_func()

Describe the bug

After running coverage run -m pytest test_tmp.py, the lines inside the with-block are not counted as covered. This really messes with the reported coverage in some of my projects because lines after a call to page.click or page.query_selector are not counted as covered. Is this a problem with Coverage.py or Playwright?

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions