I would like to create server before tests start and then terminate it after all tests run. Description of behaviour below.
First time I run tests:
1. Returns error (below)
2. Tests fail
3. Server is invoked (now server is running)
Second time I run tests:
1. Returns the same error (below)
2. Tests pass
3. Server is shutdown
Error: Exception has occurred: RuntimeError Could not start process server
@pytest.fixture(autouse=True, scope="session")
def start_server(xprocess):
port = 8000
server_path = py.path.local(__file__).dirpath("service.py")
xprocess.ensure("server", lambda cwd: ("started", [sys.executable, server_path, port]))
yield
xprocess.getinfo("server").terminate()
For future reference, this is the exact application of pytest-xprocess plugin. You could write a simple fixture like the following to start an instance of your server and make it available in your tests:
# content of conftest.py
import pytest
from xprocess import ProcessStarter
@pytest.fixture
def myserver(xprocess):
class Starter(ProcessStarter):
# startup pattern
pattern = "PATTERN"
# command to start process
args = ['command', 'arg1', 'arg2']
# ensure process is running and return its logfile
logfile = xprocess.ensure("myserver", Starter)
conn = # create a connection or url/port info to the server
yield conn
# clean up whole process tree afterwards
xprocess.getinfo("myserver").terminate()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With