pytest-xvfb

3.1.1 · active · verified Thu Apr 16

pytest-xvfb is an active pytest plugin, currently at version 3.1.1, that automatically runs tests within a virtual X server like Xvfb, Xephyr, or Xvnc. This enables running graphical user interface (GUI) tests in headless environments, such as continuous integration (CI) servers, without the need for a physical display, preventing windows from unexpectedly popping up during testing.

Common errors

Warnings

Install

Imports

Quickstart

To use `pytest-xvfb`, simply install it and `pytest`. For most use cases, tests that require a display will automatically run in a virtual framebuffer when `pytest-xvfb` is installed and a suitable backend (like `xvfb` or `xephyr`) is available on the system. You can access the `xvfb` fixture in your tests to get details about the virtual display, or use `@pytest.mark.no_xvfb` to explicitly disable the plugin for a specific test.

import pytest
import os

def test_gui_app_runs_headless(xvfb):
    # The test automatically runs with Xvfb started by pytest-xvfb
    # The 'xvfb' fixture provides details about the virtual display.
    print(f"\nRunning on virtual display: {xvfb.display}")
    print(f"Screen resolution: {xvfb.width}x{xvfb.height}x{xvfb.colordepth}")
    # Example: Run a simple command that needs a display
    # In a real scenario, you'd launch your GUI application here
    # and perform assertions.
    # For demonstration, we'll just check DISPLAY environment variable
    assert os.environ.get('DISPLAY') == f':{xvfb.display}', 'DISPLAY environment variable not set correctly.'

@pytest.mark.no_xvfb
def test_this_runs_without_xvfb():
    # This test will explicitly not use Xvfb, even if enabled.
    assert 'DISPLAY' not in os.environ

view raw JSON →