Skip to main content

Creates basic XNAT instance for API tests

Project description Supported Python versions

Xnat4Tests runs a basic XNAT repository instance in a single Docker to be used for quick demonstrations on your workstation or integrated within test suites for tools that use XNAT’s REST API.

The XNAT container service plugin is installed by default and is configured to use the same Docker host as the XNAT instance.

The home/logs, home/work, build, archive, prearchive directories are mounted in from the host system from the $HOME/.xnat4tests/xnat_root/default directory by default. This can be useful for debugging and can be used to replicate the environment under which containers run in within XNAT’s container service.

In addition to the start_xnat, stop_xnat and restart_xnat functions, which control the life-cycle of the XNAT instance, there is also a connect function that returns an XnatPy connection object to the test instance


Docker needs to be installed on your system, see Get Docker for details.

Xnat4Tests is available on PyPI so can be installed with

$ pip3 install xnat4tests

or include in your package’s test_requires if you are writing Python tests.


Command line interface

A test XNAT instance can be launched using the CLI with

$ xnat4tests start

This will spin up an empty XNAT instance that can be accessed using the default admin user account user=’admin’/password=’admin’. To add some sample data to play with you can use the add-data command

$ xnat4tests start
$ xnat4tests add-data 'dummydicom'

or in a single line

$ xnat4tests start --with-data 'dummydicom'

By default, xnat4tests will create a configuration file at $HOME/.xnat4tests/configs/default.yaml. The config file can be adapted to modify the names of the Docker images/containers used, the ports the containers run on, and which directories are mounted into the container. Multiple configurations can be used concurrently by saving the config file to a new location and passing it to the base command, i.e.

$ xnat4tests --config /path/to/my/repo/xnat4tests-config.yaml start

To stop or restart the running container you can use xnat4tests stop and xnat4tests restart commands, respectively.

Python API

If you are developing Python applications you will typically want to use the API to launch the XNAT instance using the xnat4tests.start_xnat function. An XnatPy connection session object can be accessed using xnat4tests.connect and the instanced stopped afterwards using stop_xnat.

# Import xnat4tests functions
from xnat4tests import start_xnat, stop_xnat, connect, Config

config = Config.load("default")

# Launch the instance (NB: it takes quite while for an XNAT instance to start). If an existing
# container with the reserved name is already running it is returned instead

# Connect to the XNAT instance using XnatPy and run some tests
with connect(config) as login:


    # Create subject
    xsubject = login.classes.SubjectData(label=SUBJECT,
    # Create session
    login.classes.MrSessionData(label=SESSION, parent=xsubject)

assert [ for p in (config.xnat_root_dir / "archive").iterdir()] == [PROJECT]

# Remove the container after you are done (not strictly necessary). To avoid
# having to wait for XNAT to restart each time before you run your tests, you can
# skip this line and start_xnat will attempt to use the instance that is already
# running

Alternatively, if you are using Pytest then you can set up the connection as a fixture in your, e.g.

import tempfile
from pathlib import Path
from xnat4tests import start_xnat, stop_xnat, connect, Config

def xnat_config():
    tmp_dir = Path(tempfile.mkdtemp())
    return Config(
            "xnat_version": "1.8.5",
            "xnat_cs_plugin_version": "3.2.0",

def xnat_uri(xnat_config):
    yield xnat_config.xnat_uri

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

xnat4tests-0.3.11-py3-none-any.whl (20.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page