You could use pytest-xvfb instead of messing with this… It would be easier.
It is actually fairly simple. Create a file called conftest.py
in your project root which contains this:
import pytest
import os
import subprocess
import tempfile
@pytest.fixture(scope="session", autouse=True)
def start_xvfb_server (request):
tempdir = tempfile.mkdtemp()
xvfb_cmd = ['Xvfb',
':1022',
'-screen', '0', '800x600x24',
'-fbdir', tempdir,
'-noreset'
]
xvfb_proc = subprocess.Popen(xvfb_cmd,
stdout=open(os.devnull),
stderr=open(os.devnull),
shell=False
)
request.addfinalizer(xvfb_proc.kill)
Now, all you have to do is to set up each tests to set the DISPLAY to 1022 which is trivial to do.