pythonpytestplaywright

Print playwright browser in pytest terminal header


I have pytest set up with playwright and can run it with a docker compose command:

docker compose run test pytest /tests --browser-channel chrome

Which outputs:

============================ test session starts =============================
platform linux -- Python 3.12.6, pytest-8.3.3, pluggy-1.5.0
rootdir: /tests
plugins: base-url-2.1.0, playwright-0.5.2
collected 1 item                                                             

tests/test_example.py .                                                   [100%]

============================= 1 passed in 0.02s ==============================

I want to include the browser names in each session. I've tried using the browser_name fixture in the pytest_report_header hook, but fixtures don't work in the report hooks and it errors out.


Solution

  • I was able to add this by checking the config.option Namespace and returning the browser value if found.

    Added to my conftest.py file:

    def pytest_report_header(config):
        if config.get_verbosity() == 0:
            browser = None
            if config.option.browser_channel is not None:
                browser = config.option.browser_channel
            elif len(config.option.browser) > 0:
                browser = ", ".join(config.option.browser)
    
            if browser is not None:
                return f"browser: {browser}"
    
    ============================ test session starts =============================
    platform linux -- Python 3.12.6, pytest-8.3.3, pluggy-1.5.0
    browser: chrome
    rootdir: /tests
    plugins: base-url-2.1.0, playwright-0.5.2
    collected 1 item                                                             
    
    tests/test_example.py .                                                [100%]
    
    ============================= 1 passed in 0.02s ==============================
    ============================ test session starts =============================
    platform linux -- Python 3.12.6, pytest-8.3.3, pluggy-1.5.0
    browser: firefox
    rootdir: /tests
    plugins: base-url-2.1.0, playwright-0.5.2
    collected 1 item                                                             
    
    tests/test_example.py .                                                [100%]
    
    ============================= 1 passed in 0.02s ==============================
    

    This is still a little buggy: