python - 检索 Nose 测试用例的结果并在拆解中使用

标签 python nose

在nose中,无论安装是否成功完成或测试运行的状态如何,拆卸都会运行。

我想在拆卸中执行一项任务,该任务仅在刚刚运行的测试失败时才执行。有没有一种简单的方法来检索每个单独测试用例的结果并将其传递给拆卸方法进行解释?

class TestMyProgram:
    def setup(self):
        # setup code here

    def teardown(self):
        # teardown code here

        # run this code if test failed
        if test_result == 'FAIL':
            # do something    

    def test_one(self):
        # example test placeholder 
        pass

    def test_two(self):
        # example test placeholder 
        pass

最佳答案

您必须捕获测试的状态,并将其传递给您的拆卸方法。测试的状态在nose代码中:如果不编写nose插件就无法访问。但即使使用插件,您也必须编写一个自定义装备来将状态传递给拆卸方法。但是,如果您愿意稍微打破代码的结构来满足您的请求,您也许可以执行以下操作:

def special_trardown(self, state):
    # only state specific logic goes here
    print state

def test_one_with_passing_state(self):
    try:
        test_one(self)
    except AssertionError as err:
        test_result = "FAIL"
        self.special_teardown(test_result)
        raise

它并不完美,但它使事件的流程对于其他查看你的测试的人来说是显而易见的。您还可以将其包装为装饰器/上下文管理器以获得更多语法糖。

关于python - 检索 Nose 测试用例的结果并在拆解中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35702142/

相关文章:

python - 在 Nose 测试中打印不同的长描述以及测试名称 python

python - Nosetests 缺少命令行选项

python - 在抛出 subprocess.TimeoutExpired 后杀死 Python 子进程的子进程

python - 如何使用 python imaplib.IMAP4.search() 搜索特定的电子邮件

python - 如何在 Tensorflow 2.0 中复制网络

python - 如何从 Python 函数中提取函数名称?

python - pypy 解释器无法识别 nosetests 而 pypy3 可以

python - 使用最大日期字段和收入字段按类别进行新列计算

python - 现在为什么不在测试中使用 python 的 assert 语句呢?

python - 更改 nosetests 的详细报告格式