python - notests 将导入的方法标记为非测试用例

标签 python unit-testing nose

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

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

  1. 使用 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/

    相关文章:

    html - Python3 单元测试 HTML 报告

    python - 如何使用python将json存储在django mysql数据库中?

    .net - 使用 "friend"-declarations 进行单元测试。馊主意?

    python - 即使测试通过,也让 Nose 测试运行器显示日志记录

    python - 单元测试模拟 urllib.request.urlretrieve() Python 3 和内部函数

    unit-testing - 如何对调用私有(private)方法并依次调用 Web 服务方法的公共(public)方法进行单元测试

    python - 检测代码何时在 Travis CI 上运行

    java - Pydoop 作业未运行

    python - 将分类列转换为附加列

    python - 为体育联盟生成自然时间表