pip install pytest-services
To use the memcached_client fixture, install the memcached extra, which pulls in pylibmc:
pip install 'pytest-services[memcached]'
The plugin provides a set of fixtures and utility functions to start service processes for your tests with pytest.
The service fixtures start real processes, so the corresponding programs must be installed and available on PATH:
- memcached for the
memcachedfixture. - mysql-server (
mysqld,mysql,my_print_defaultsandmysql_tzinfo_to_sql) for themysqlfixture. - xvfb (
Xvfb) for thexvfbfixture.
The services rely on POSIX facilities (unix sockets, /tmp, /dev/shm), so they are meant to be run on
Linux or other POSIX systems.
run_services- Determines whether services should be run or not.
Falseby default if not in a distributed environment (without pytest-xdist). Can be manually set toTrueby overriding this fixture in your test config or just by using the--run-servicescommand line argument (see below).
worker_id- Id of the worker if tests are run using pytest-xdist. It is set to
localif tests are not run using pytest-xdist. Has a deprecated aliasslave_idwhich will be removed in a future version.
session_id- Test session id. Globally unique, and of course also guaranteed to be different for potentially multiple test sessions running on same test node via pytest-xdist.
watcher_getter- Function to instantiate test service watcher (popen object). Includes automatic finalization (exiting) of the service process, and testing the service before returning the watcher from the function. Example of usage for memcached service:
@pytest.fixture(scope='session')
def memcached(request, run_services, memcached_socket, watcher_getter):
"""The memcached instance which is ready to be used by the tests."""
if run_services:
return watcher_getter(
name='memcached',
arguments=['-s', memcached_socket],
checker=lambda: os.path.exists(memcached_socket),
# Needed for the correct execution order of finalizers
request=request,
)services_log- Logger used for debug logging when managing test services.
root_dir- Parent directory for test service artifacts (disk based). Set to
/tmpby default.
base_dir- Base directory for test service artifacts (disk based), unique subdirectory of
root_dir. Automatically removed recursively at the end of the test session.
temp_dir- Temporary directory (disk based), subfolder of
base_dir. Used for strictly temporary artifacts (for example - folder where files are uploaded from the user input).
memory_root_dir- Parent directory for test service artifacts (memory based). Main idea of having memory base directory is to
store performance-critical files there. For example - mysql service will use it to store database file, it speeds up
mysql server a lot, especially database management operations.
Set to
/dev/shmby default, if it exists and has enough free space (about 640 MB), with a fallback toroot_dir. Note that if AppArmor is running on your system, most likely it will prevent your test service from using it (for example - mysql has its own AppArmor profile). You'll need to disable such profile in the AppArmor configuration. Example of disabling AppArmor for mysqld:
sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/
sudo /etc/init.d/apparmor restartmemory_base_dir- Base directory for test service artifacts (memory based), unique subdirectory of
memory_root_dir. Automatically removed recursively at the end of the test session.
memory_temp_dir- Temporary directory (memory based), subfolder of
memory_base_dir.
lock_dir- Lock files directory for storing locks created for resource assignment (ports, display, etc). Subfolder of
memory_root_dir.
run_dir- Process id and socket files directory (like system-wide
/var/runbut local for test session). Subfolder ofmemory_temp_dir.
port_getter- Function to get an unallocated port. Automatically ensures locking and un-locking of it on application level via lock files.
display_getter- Function to get an unallocated display. Automatically ensures locking and un-locking of it on application level via lock files.
lock_resource_timeout- Maximum number of seconds to wait for the lock on a resource (port, display) used by
port_getteranddisplay_getter. Defaults to20.
memcached- Start memcached instance.
memcached_socket- Memcached unix socket file name to be used for connection.
memcached_connection- Memcached connection string.
memcached_client- A
pylibmc.Clientinstance bound to the service. Requirespylibmcinstalled (pip install 'pytest-services[memcached]'); tests using it are skipped otherwise.
memcached_clean- Flushes all memcached data before the test if
do_memcached_cleanisTrue. Requirespylibmc, throughmemcached_client.
do_memcached_clean- Determine if memcached should be cleared before every test run by
memcached_clean. Equals torun_servicesfixture by default.
mysql- Start mysql-server instance and create the test database.
mysql_database_name- MySQL database name to be created after initialization of the mysql service
systemdatabase. Defaults topytest_services_test.
mysql_database_getter- Function with single parameter - database name. To create additional database(s) for tests.
Used in
mysql_databasefixture which is used bymysqlone.
mysql_database- Creates the
mysql_database_namedatabase.
mysql_connection- MySQL connection string.
mysql_socket- MySQL unix socket file name to be used for connection. The server runs with
--skip-networking, so connections must go through this socket.
mysql_pid- MySQL server pid file.
mysql_watcher- The
mysqldprocess watcher.
mysql_data_dir- MySQL data directory, created inside
memory_temp_dir.
mysql_defaults_file- MySQL defaults file (
--defaults-file) used by the server.
mysql_base_dir- MySQL installation base directory, detected from the location of
my_print_defaults.
mysql_system_database- Initializes the MySQL
systemdatabase.
xvfb- Start xvfb instance.
xvfb_display- Xvfb display to use for connection.
xvfb_resolution- Xvfb display resolution to use. Tuple in form
(1366, 768, 8).
Warning
pytest_services.django_settings is not compatible with current Python and Django versions: it imports
imp (removed in Python 3.12) and django.core.urlresolvers (removed in Django 2.0).
In some cases, there's a need of switching django settings during test run, because several django projects are tested
within the single test suite.
pytest_services.django_settings simplifies switching of django settings to a single function call:
setup_django_settings- Override the environment variable and call the
_setupmethod of the settings object to reload them.
clean_django_settings- Remove the
DJANGO_SETTINGS_MODULEenvironment variable and reset the settings object. The environment variable must be set when calling it.
Example of usage:
conftest.py:
from pytest_services import django_settings
django_settings.clean_django_settings()
django_settings.setup_django_settings('your.project.settings')Note that the nice project pytest-django doesn't help with the situation, as it's single django project oriented, as well as standard django testing technique. Single project approach works fine, as long as there are no fixtures to share between them, but when there are fixtures to share, then you can get benefit of joining several django projects tests into a single test run, because all session-scoped fixtures will be instantiated only once for all projects tests. The benefit is only visible if you have big enough test suite and your fixtures are heavy enough.
--run-services- Force services to be run even if tests are executed in a non-distributed way (without pytest-xdist).
--xvfb-display- Skip xvfb service to run and use provided display. Useful when you need to run all services except the xvfb to debug your browser tests, if, for example you use pytest-splinter with or without pytest-bdd.
test_your_test.py (requires mysqlclient):
import MySQLdb
def test_some_mysql_stuff(mysql, mysql_socket, mysql_database_name):
"""Test using mysql server."""
conn = MySQLdb.connect(user='root', unix_socket=mysql_socket, database=mysql_database_name)If you have questions, bug reports, suggestions, etc. please create an issue on the GitHub project page.
This software is licensed under the MIT license
See License file
© 2014 Anatoly Bubenkov, Paylogic International and others.