鼻子测试将导入的方法标记为非测试用例

时间:2019-02-14 08:19:19

标签: python unit-testing nose

nosetest使用启发式方法来识别哪些功能是测试用例。导入名称不明确的方法进行测试时,例如:

foo / foo.py

def get_test_case(text):
    return "xyz"

(注意,不包括目录foo,这与将foo / foo.py标识为测试用例的鼻子测试无关)

tests / test_foo.py

import unittest

# causes TypeError: get_test_case() missing 1 required positional argument: 'text'
from foo.foo import get_test_case

class TestTestCasesReader(unittest.TestCase):

     def test_get_test_case(self):
         self.assertEquals(get_test_case("fooBar"), ...)

我知道我可以在测试中解决此问题:

import unittest
import foo.foo

# ...
        self.assertEquals(foo.get_test_case("fooBar"), ...)

但是感觉应该有一种更好的方法告诉鼻子测试取消该get_test_case函数。

很明显,我也可以重命名get_test_case使其免受鼻子测试的影响,但这并不是我要找的答案。

1 个答案:

答案 0 :(得分:1)

这是一个相关的问题:Getting nose to ignore a function with 'test' in the name

问题中提出了两种解决方案

  1. 在定义get_test_case的模块中使用nottest装饰器。
from nose.tools import nottest

@nottest
def get_test_case(text):
    return "xyz"
  1. 在测试代码中使用nottest
import unittest
from nose.tools import nottest

from foo.foo import get_test_case
get_test_case = nottest(get_test_case)

class TestTestCasesReader(unittest.TestCase):

     def test_get_test_case(self):
         self.assertEquals(get_test_case("fooBar"), ...)