【问题标题】:nosetests mark imported method as non-test-case [duplicate]鼻子测试将导入的方法标记为非测试用例[重复]
【发布时间】:2019-02-14 08:19:18
【问题描述】:

nosetest 使用启发式方法来识别哪些函数是测试用例。当导入一个用于测试的名称不明确的方法时,这可能会变得很尴尬,例如:

foo/foo.py

def get_test_case(text):
    return "xyz"

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

测试/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】:

    这是一个相关问题: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"), ...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      相关资源
      最近更新 更多