当我运行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/