nosetests标记导入的方法为非测试用例[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

nosetest使用启发式方法来识别哪些功能是测试用例。导入具有不明确名称的方法进行测试时,这可能会变得很尴尬,例如:

富/ foo.py

def get_test_case(text):
    return "xyz"

(注意目录foo被排除在外,这不是关于将foo / foo.py识别为测试用例的nosetests)

测试/ 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来隐藏它的鼻子测试,但这不是我想要的答案。

python unit-testing nose
1个回答
1
投票

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

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

  1. 在定义nottest的模块中使用get_test_case装饰器。
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"), ...)
© www.soinside.com 2019 - 2024. All rights reserved.