为API测试创建基本的XNAT实例
项目描述
Xnat4Tests在单个Docker中运行基本的XNAT仓库实例,用于在您的工作站上进行快速演示或集成到使用XNAT REST API的工具的测试套件中。
默认情况下安装了XNAT容器服务插件,并配置为使用与XNAT实例相同的Docker主机。
默认情况下,从主机系统中的$HOME/.xnat4tests/xnat_root/default目录挂载home/logs、home/work、build、archive、prearchive目录。这可以用于调试,并且可以用来在XNAT的容器服务中复制容器运行的 环境。
除了控制XNAT实例生命周期的start_xnat、stop_xnat和restart_xnat函数外,还有一个connect函数,它返回测试实例的XnatPy连接对象
安装
您的系统需要安装Docker,有关详细信息,请参阅获取Docker。
Xnat4Tests可在PyPI上使用,因此可以使用以下命令进行安装
$ pip3 install xnat4tests
或将它包含在您的包的test_requires中,如果您正在编写Python测试。
使用方法
命令行界面
可以使用CLI启动一个测试XNAT实例
$ xnat4tests start
这将启动一个空的XNAT实例,可以通过默认管理员用户账户访问,用户名=’admin’/密码=’admin’。为了添加一些示例数据来试验,可以使用< cite>add-data cite>命令
$ xnat4tests start
$ xnat4tests add-data 'dummydicom'
或单行
$ xnat4tests start --with-data 'dummydicom'
默认情况下,xnat4tests将在< cite>$HOME/.xnat4tests/configs/default.yaml cite>创建一个配置文件。可以通过修改配置文件来更改使用的Docker镜像/容器名称、容器运行的端口以及哪些目录被挂载到容器中。可以通过将配置文件保存到新位置并将它传递给基础命令来同时使用多个配置,例如:
$ xnat4tests --config /path/to/my/repo/xnat4tests-config.yaml start
要停止或重新启动正在运行的容器,可以使用< span class="docutils literal">xnat4tests stop span>和< span class="docutils literal">xnat4tests restart span>命令。
Python API
如果您正在开发Python应用程序,您通常希望使用API通过< cite>xnat4tests.start_xnat cite>函数启动XNAT实例。可以使用< cite>xnat4tests.connect cite>访问XnatPy连接会话对象,之后可以使用< cite>stop_xnat cite>停止实例。
# 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)
或者,如果您使用Pytest,您可以在您的< span class="docutils literal">conftest.py span>中设置连接作为.fixture,例如:
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)
项目详情
下载文件
下载您平台上的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。