nosetest 使用启发式方法来识别哪些函数是测试用例。当导入名称不明确的方法进行测试时,这可能会变得很尴尬,例如:
foo/foo.py
def get_test_case(text):
return "xyz"
(注意目录 foo 被排除,这不是关于将 foo/foo.py 识别为测试用例的 Nose 测试)
测试/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"), ...)
但感觉应该有更好的方法来告诉nosetest停止get_test_case
功能。
显然我也可以重命名 get_test_case
为了向 Nose 测试隐藏它,但这不是我正在寻找的答案。
最佳答案
这是一个相关问题:Getting nose to ignore a function with 'test' in the name
问题中提出了两种解决方案
- 使用 nottest定义
get_test_case
的模块中的装饰器。
from nose.tools import nottest
@nottest
def get_test_case(text):
return "xyz"
- 使用 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"), ...)
关于python - notests 将导入的方法标记为非测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54685876/