A pytest plugin for managing processes across test runs.
Project description
pytest plugin for managing processes across test runs.
Setting Up
To use pytest-xprocess you just need to install it via:
pip install pytest-xprocess
and that’s all!
This will provide a xprocess fixture which can be used to ensure that external processes on which your application depends are up and running during testing. You can also use it to start and pre-configure test-specific databases (i.e. Postgres, Couchdb).
Additionally, there are two new command line options:
--xkill # terminates all external processes --xshow # shows currently running processes and log files
xprocess fixture usage
You typically define a project-specific fixture which uses xprocess internally:
# 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']
# max startup waiting time
# optional, defaults to 120 seconds
timeout = 45
# max lines read from stdout when matching pattern
# optional, defaults to 100 lines
max_read_lines = 100
# 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 process and its children afterwards
xprocess.getinfo("myserver").terminate()
The xprocess.ensure method takes the name of an external process and will make sure it is running during your testing phase. Also, you are not restricted to having a single external process at a time, xprocess can be used to handle multiple diferent processes or several instances of the same process.
The Starter is a subclass that gets initialized with the working directory created for this process. If the server has not yet been started:
pattern is waited for in the logfile before returning. It should thus match a state of your server where it is ready to answer queries.
args is a list of arguments, used to invoke a new subprocess.
timeout may be used to specify the maximum time in seconds to wait for process startup.
max_read_lines may be be used to extend the number of lines searched for pattern prior to considering the external process dead. By default, the first 50 lines of stdout are redirected to a logfile, which is returned pointing to the line right after the pattern match.
Adicionally, env may be defined to customize the environment in which the new subprocess is invoked. To inherit the main test process environment, leave env set to the default (None).
If the server is already running, simply the logfile is returned.
Overriding Wait Behavior
To override the wait behavior, override ProcessStarter.wait. See the xprocess.ProcessStarter interface for more details.
Note that the plugin uses a subdirectory in .pytest_cache to persist the process ID and logfile information.
An Important Note Regarding Stream Buffering
There have been reports of issues with test suites hanging when users attempt to start external python processes with xprocess.ensure method. The reason for this is that pytest-xprocess relies on matching predefined string patterns written to your environment standard output streams to detect when processes start and python’s sys.stdout/sys.stderr buffering ends up getting in the way of that. A possible solution for this problem is making both streams unbuffered by passing the -u command-line option to your process start command or setting the PYTHONUNBUFFERED environment variable.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-xprocess-0.16.0.tar.gz
.
File metadata
- Download URL: pytest-xprocess-0.16.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f499e4db3d2c829bc5a4a6363464e6c4cc62f61c2a5bf7a105d5d6b12593704e |
|
MD5 | 523fef439f3fb2762806a08c3a279835 |
|
BLAKE2b-256 | bab6984c396fac5d002adbd873b1720dadad02ec9d0bcda3984995fd7008ab4d |
File details
Details for the file pytest_xprocess-0.16.0-py2.py3-none-any.whl
.
File metadata
- Download URL: pytest_xprocess-0.16.0-py2.py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8ba0b22aa0d6d262cb6cd896c4403b9b59c5ddfc60fa3c803c2fa1d00095e68 |
|
MD5 | 753a50668917ad192d0588a3ffc1bb91 |
|
BLAKE2b-256 | cf4ca39af543582344546d8f0ba92c6018ed8a88088f9540af69955052d295f1 |