【问题标题】:What is the protocol for loading test suites for both nose and unittest?为鼻子和单元测试加载测试套件的协议是什么?
【发布时间】:2019-05-07 02:21:42
【问题描述】:

我使用了unittestload_tests() 协议,因为我希望我的自动化测试包含doctest,但仅限于一个模块。这在由 unittest 发现和运行时工作正常,但在由 nose 运行时会失败。

import doctest
import unittest

import my.module

suite = doctest.DocTestSuite(my.module)

def load_tests(loader, std, pat): # invoked by unittest discovery process
    return suite

错误是:

Traceback (most recent call last):
  File "/g/data/w85/brl654/conda/envs/haz/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
TypeError: load_tests() takes exactly 3 arguments (0 given)

如果我将这些参数设为可选,nose 会运行测试吗?有没有更好的方法来集成这个测试套件,以便它可以由unittestnose 运行?

【问题讨论】:

    标签: python unit-testing nose doctest


    【解决方案1】:

    困难在于nosetests 通常会搜索unittest.TestCase 子类和具有类似测试名称的函数的组合。 unittest.TestSuite 既不是TestCase 的子类也不是函数,所以自然不能被鼻子捡到。 (此外,nose 错误地选择了 load_tests 并尝试将其作为测试函数运行,这与 unittest 的测试加载协议相反。)

    粗略的解决方案是:

    load_tests.__test__ = False # instruct nose to skip load_tests()
    def test_with_nose():
        assert suite.run(unittest.TestResult()).wasSuccessful()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 2010-10-27
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多