Skip to main content
https://github.com/australian-imaging-service/xnat4tests/actions/workflows/test.yml/badge.svg https://codecov.io/gh/australian-imaging-service/xnat4tests/branch/main/graph/badge.svg?token=UIS0OGPST7 https://img.shields.io/pypi/v/xnat4tests.svg 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

Installation

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.

Usage

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
start_xnat(config)

# Connect to the XNAT instance using XnatPy and run some tests
with connect(config) as login:
    PROJECT = 'MY_TEST_PROJECT'
    SUBJECT = 'MYSUBJECT'
    SESSION = 'MYSESSION'

    login.put(f'/data/archive/projects/MY_TEST_PROJECT')

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

assert [p.name 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
stop_xnat(config)

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

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

@pytest.fixture(scope="session")
def xnat_config():
    tmp_dir = Path(tempfile.mkdtemp())
    return Config(
        xnat_root_dir=tmp_dir,
        xnat_port=9999,
        docker_image="myrepo_xnat4tests",
        docker_container="myrepo_xnat4tests",
        build_args={
            "xnat_version": "1.8.5",
            "xnat_cs_plugin_version": "3.2.0",
        },
    )

@pytest.fixture(scope="session")
def xnat_uri(xnat_config):
    xnat4tests.start_xnat(xnat_config)
    xnat4tests.add_data("dummydicom")
    yield xnat_config.xnat_uri
    xnat4tests.stop_xnat(xnat_config)

Release files for xnat4tests 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for xnat4tests 0.4.1
File Interpreter ABI Platform
xnat4tests-0.4.1-py3-none-any.whl Python 3 none any Details

Release files / xnat4tests-0.4.1-py3-none-any.whl

Download URL xnat4tests-0.4.1-py3-none-any.whl
Size 22.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da93e63b9e0ec6f9e6711238d65e64982fb1c73695898729d67742506cf2330f
BLAKE2b-256 checksum
How to use checksums
42f4bab62117d761fc8cf60e805a90ff666d8bc802826003ee19f46e09097910
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.4.1 This release

1 release file

0.4.0

1 release file

0.3.15

1 release file

0.3.14

1 release file

0.3.13

1 release file

0.3.12

1 release file

0.3.11

1 release file

0.3.10

1 release file

0.3.9

1 release file

0.3.8

1 release file

0.3.7

1 release file

0.3.6

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.2

1 release file

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page