【问题标题】:How can I include additional tests if a test passes?如果测试通过,我如何包含额外的测试?
【发布时间】:2013-07-23 14:15:43
【问题描述】:

我正在使用nose 运行一些系统测试,其中之一是测试(配置)文件是否存在。如果此文件存在,我想对其进行一些额外的测试。如果没有,我想跳过一堆测试。

如果主要测试通过,使nose 包含附加测试的最佳方法是什么?

【问题讨论】:

  • 检查断言语句的返回值并设置条件是最简单的方法
  • 你可以创建一个装饰器,如果配置文件不存在调用 unittest .skip

标签: python unit-testing nose system-testing


【解决方案1】:

您可以在特定的 TestCase 中使用 setUp 方法中的 skipTest,例如:

import os
from unittest import TestCase

class MyTest(TestCase):
    def setUp(self):
        if not os.path.exists('configfile'):
            return self.skipTest('config file not found')

    def test01(self):
        # Do something with the file
        with open('configfile') as fd:
            self.assertEqual(fd.readlines().__len__(), 0)

如果配置文件不存在,test test01 将不会运行。

【讨论】:

  • 我相信SkipTest 是一个应该引发而不是返回的异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 1970-01-01
相关资源
最近更新 更多