Package: xnat4tests
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.Thehome/logs,home/work,build,archive,prearchivedirectories are
mounted in from the host system from the$HOME/.xnat4tests/xnat_root/defaultdirectory
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 thestart_xnat,stop_xnatandrestart_xnatfunctions, which control the life-cycle of
the XNAT instance, there is also aconnectfunction that returns an XnatPy connection object to the test instanceInstallationDocker needs to be installed on your system, seeGet Dockerfor details.Xnat4Tests is available on PyPI so can be installed with$pip3installxnat4testsor include in your package’stest_requiresif you are writing Python tests.UsageCommand line interfaceA test XNAT instance can be launched using the CLI with$xnat4testsstartThis 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 theadd-datacommand$xnat4testsstart$xnat4testsadd-data'dummydicom'or in a single line$xnat4testsstart--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.yamlstartTo stop or restart the running container you can usexnat4tests stopandxnat4tests restartcommands, respectively.Python APIIf you are developing Python applications you will typically want to use the API to
launch the XNAT instance using thexnat4tests.start_xnatfunction. An XnatPy connection
session object can be accessed usingxnat4tests.connectand the instanced stopped
afterwards usingstop_xnat.# Import xnat4tests functionsfromxnat4testsimportstart_xnat,stop_xnat,connect,Configconfig=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 insteadstart_xnat(config)# Connect to the XNAT instance using XnatPy and run some testswithconnect(config)aslogin:PROJECT='MY_TEST_PROJECT'SUBJECT='MYSUBJECT'SESSION='MYSESSION'login.put(f'/data/archive/projects/MY_TEST_PROJECT')# Create subjectxsubject=login.classes.SubjectData(label=SUBJECT,parent=login.projects[PROJECT])# Create sessionlogin.classes.MrSessionData(label=SESSION,parent=xsubject)assert[p.nameforpin(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# runningstop_xnat(config)Alternatively, if you are using Pytest then you can set up the connection as
a fixture in yourconftest.py, e.g.importtempfilefrompathlibimportPathfromxnat4testsimportstart_xnat,stop_xnat,connect,Config@pytest.fixture(scope="session")defxnat_config():tmp_dir=Path(tempfile.mkdtemp())returnConfig(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")defxnat_uri(xnat_config):xnat4tests.start_xnat(xnat_config)xnat4tests.add_data("dummydicom")yieldxnat_config.xnat_urixnat4tests.stop_xnat(xnat_config)