python - Nose 测试和类方法

标签 python unit-testing python-3.5 nose

当我运行nosetests时,我遇到了一个奇怪的错误:

======================================================================
ERROR: Extract test data from tarball.
----------------------------------------------------------------------
TypeError: extract_test_data() missing 1 required positional argument: 'calling_file'

相关代码分为两个文件:

测试/core.py

class CoreTestCase(unittest.TestCase):
    @classmethod
    def extract_test_data(cls, calling_file, base='data', name_only=False):
        """Extract test data from tarball.
           ...
        """
        ...

测试/.../test_this.py

class TestThis(core.CoreTestCase):
    """Run some tests."""

    @classmethod
    def setUpClass(cls):
        cls.TESTDAT_DIR = cls.extract_test_data(__file__)

导入等工作正常,并且 unittest 没有任何问题。但由于某种原因,nose 正在破坏调用。

我已尝试以下所有方法:

cls.TESTDAT_DIR = cls.extract_test_data(calling_file=__file__)
cls.TESTDAT_DIR = cls.extract_test_data(cls,__file__)
cls.TESTDAT_DIR = cls.extract_test_data(cls, calling_file=__file__)

但是我仍然遇到奇怪的分类错误:

TypeError: extract_test_data() got multiple values for argument 'calling_file'
AttributeError: type object 'TestThis' has no attribute 'TESTDAT_DIR'

最佳答案

nose正在尝试运行extract_test_data就像这是一个单元测试。将其重命名以排除 token test或将其添加到 extract_test_data :

from nose.tools import nottest

class CoreTestCase(unittest.TestCase):

@nottest
@classmethod
def extract_test_data(cls, calling_file, base='data', name_only=False):
    """Extract test data from tarball.
       ...
    """
    ...

编辑:link文档中解释说,默认情况下,testMatch正则表达式将运行 has test or Test at a word boundary or following a - or _ 的任何函数

关于python - Nose 测试和类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41746861/

相关文章:

python - BeautifulSoup 解析返回空集

python - Python中是使用http.client还是Socket包?

Python Beautiful Soup——通过 Steam 的年龄检查

python:带有交互式 python shell 的扭曲应用程序

python - 每次查询后重新打开sqlite数据库的效率

java - Junit mockito when(..).thenReturn() 抛出 NullPointerException

unit-testing - AngularJS : Need help to unit test a factory with promise

node.js - 将 "node test"作为 Visual Studio Team Services 生成任务的一部分运行,结果在 "tests"选项卡中

python - 异步 + 多处理 + unix

python - 使用单独文件中的自定义代码时出现问题