我的项目结构如下:
/root
/tests
common_test_case.py
test_case_1.py
test_case_2.py
...
project_file.py
...
每个测试test_case_...
都继承自^{common_test_case.CommonTestCase
。类CommonTestCase
包含应该由所有测试执行的测试方法(尽管使用每个测试唯一的数据,存储和访问在测试的self.something
中)。如果一个确切的测试用例需要一些特定的测试,则直接将它们添加到特定的类中。你知道吗
目前,我正在努力添加日志到我的测试。除此之外,我还想记录从中运行方法的类(因为上面的方法对不同的类意味着相同的测试方法名称)。我想使用内置的logging模块来实现这一点。你知道吗
我试过以下LogRecord attributes:%(filename)s
,%(module)s
,%(pathname)s
。不过,对于common_test_case.py
中定义的方法,它们都将路径/名称返回给common_test_case.py
,而不是实际运行它们的测试模块。你知道吗
我的问题是:
logging
模块?你知道吗
您的问题与此one类似,解决方法如下:
请参阅函数定义here,它为实例化的
TestCase
类的实例调用self.__class__
。如果您使用的是多重继承,multiple inheritance rules from Python将应用:这意味着
common_test_case.CommonTestCase
将被搜索,然后unittest.TestCase
。如果在common_test_case.CommonTestCase
中没有id函数,则应该像它只从unittest.TestCase
派生一样工作。如果您觉得有必要将id
函数添加到CommonTestCase
,可以这样做(如果确实需要):解决方案I've found(到目前为止,这是关键): 你知道吗
不过,我仍然在想,是否有一个“更清晰”的方法,或者是否可以使用logging模块来实现。你知道吗
配方,基于West's answer(在python3.6.1上测试): 你知道吗
相关问题 更多 >
编程相关推荐