测试设置的继承

2024-06-16 14:48:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有几个测试用例具有相同的设置/拆卸代码。我认为下面的设置可以消除重复。在

在客户端设置.py在

class BaseClientTestCase(unittest.TestCase):

    def setUp(self):
        #Do setup    
    def tearDown(self):
        #Do tear down

试验_myothertestcase.py在

^{pr2}$

这允许我删除重复,然后能够在需要时添加一些特定的设置/拆卸。尽管我遇到了这个问题, 我的测试加载器停止工作了。在

unittest.TestLoader().loadTestsFromName('tests.test_myothertestcase')

返回的错误如下:

AttributeError: 'module' object has no attribute 'test_myothertestcase'

测试加载程序将从命令行获取要查找的名称,因此它必须是字符串。由于某些原因,它不再识别MyOtherTestCase,好像继承并不像我预期的那样工作。在

我该怎么做?在

扩展信息

结构

app/
  ...
manage.py
tests/
    __init__.py
    test_myothertestcase.py

在管理.py在

@manager.command
def test(coverage=False,testcase=None):
    """Run the unit tests."""

    suite = None
    if testcase:
        suite = unittest.TestLoader().loadTestsFromName("tests.%s" % testcase)
    else:
        suite = unittest.TestLoader().discover('tests')

    unittest.TextTestRunner(verbosity=2).run(suite)

堆栈跟踪:

  File "./manage.py", line 46, in <module>
    manager.run()
  File "/path/v_env/lib/python3.4/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/path/v_env/lib/python3.4/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/path/v_env/lib/python3.4/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "./manage.py", line 30, in test
    suite = unittest.TestLoader().loadTestsFromName("tests.%s" % testcase)
  File "/usr/lib/python3.4/unittest/loader.py", line 114, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test_myothertestcase'

Tags: runinpytestselfliblinetests