跳转到主要内容

为API测试创建基本的XNAT实例

项目描述

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在单个Docker中运行基本的XNAT仓库实例,用于在您的工作站上进行快速演示或集成到使用XNAT REST API的工具的测试套件中。

默认情况下安装了XNAT容器服务插件,并配置为使用与XNAT实例相同的Docker主机。

默认情况下,从主机系统中的$HOME/.xnat4tests/xnat_root/default目录挂载home/logshome/workbuildarchiveprearchive目录。这可以用于调试,并且可以用来在XNAT的容器服务中复制容器运行的 环境。

除了控制XNAT实例生命周期的start_xnatstop_xnatrestart_xnat函数外,还有一个connect函数,它返回测试实例的XnatPy连接对象

安装

您的系统需要安装Docker,有关详细信息,请参阅获取Docker

Xnat4Tests可在PyPI上使用,因此可以使用以下命令进行安装

$ pip3 install xnat4tests

或将它包含在您的包的test_requires中,如果您正在编写Python测试。

使用方法

命令行界面

可以使用CLI启动一个测试XNAT实例

$ xnat4tests start

这将启动一个空的XNAT实例,可以通过默认管理员用户账户访问,用户名=’admin’/密码=’admin’。为了添加一些示例数据来试验,可以使用< cite>add-data命令

$ xnat4tests start
$ xnat4tests add-data 'dummydicom'

或单行

$ xnat4tests start --with-data 'dummydicom'

默认情况下,xnat4tests将在< cite>$HOME/.xnat4tests/configs/default.yaml创建一个配置文件。可以通过修改配置文件来更改使用的Docker镜像/容器名称、容器运行的端口以及哪些目录被挂载到容器中。可以通过将配置文件保存到新位置并将它传递给基础命令来同时使用多个配置,例如:

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

要停止或重新启动正在运行的容器,可以使用< span class="docutils literal">xnat4tests stop和< span class="docutils literal">xnat4tests restart命令。

Python API

如果您正在开发Python应用程序,您通常希望使用API通过< cite>xnat4tests.start_xnat函数启动XNAT实例。可以使用< cite>xnat4tests.connect访问XnatPy连接会话对象,之后可以使用< cite>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)

或者,如果您使用Pytest,您可以在您的< span class="docutils literal">conftest.py中设置连接作为.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)

项目详情


下载文件

下载您平台上的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源代码分发

此版本没有可用的源代码分发文件。请参阅生成分发存档的教程

构建分发

xnat4tests-0.3.11-py3-none-any.whl (20.9 kB 查看散列)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面