如何防止 snakemake 在测试套件失败后停止?

How to prevent snakemake from stopping after failed test suite?

我正在尝试使用 snakemake 自动进行集成测试(我需要一些文件的输出,所以这似乎是一个很好的工具)。但是,当我需要 运行 pytest 中的两个测试套件时,如果任一套件中的单个测试失败,工作流将停止。例如我有:

rule run_tests:
    run:
        commands = [
            "pytest test_that_should_fail",
            "pytest test_that_should_succeed"
        ]
        for c in commands:
            shell(c)

我需要后一个测试应该失败的测试输出。有没有办法防止 snakemake 在 运行ning "pytest test_that_should_fail" 之后停止?此外,snakemake 停止时没有任何类型的错误消息只是一个通用的:“规则错误 run_tests:jobid:0”

你可以这样写:

commands = [
   "pytest test_that_should_fail || true",
   "pytest test_that_should_succeed || true",
]

然后shell命令的退出代码为0,表示成功,从而继续执行其他命令。

编辑:

这是解决问题的简单方法,不是@bli 指出的最干净也不是最惯用的方法。

我建议你让 Snakemake 处理不同的测试,方法是 运行每个规则实例只执行一个测试。

这可以按如下方式完成(未测试):

TESTS = ["test_that_should_fail", "test_that_should_succeed"]

rule all:
    input:
        logs = expand("{test}.log", test=TESTS),
        errs = expand("{test}.err", test=TESTS),

rule run_test:
    output:
        log = "{test}.log",
        err = "{test}.err",
    shell:
        """
        pytest {wildcards.test} 1> {output.log} 2> {output.err}
        """

然后,您可以使用 snakemake 的选项 -k 来确保所有测试都是 运行。 但是,如果需要非零 return 代码,则使用 || true(如 中所建议的)可能会更干净。