【发布时间】:2023-01-12 23:57:35
【问题描述】:
我在不同的目录中有几个测试文件。
\tests
\subtestdir1
-__init__.py
-test1.py
\subtestdir2
-__init__.py
-test2.py
-__init__.py
-test3.py
在所有测试文件中的所有测试之前,我只需要进行一次设置。
根据https://stackoverflow.com/a/66252981,顶级__init__.py看起来像这样:
import unittest
OLD_TEST_RUN = unittest.result.TestResult.startTestRun
def startTestRun(self):
print('once before all tests')
OLD_TEST_RUN(self)
unittest.result.TestResult.startTestRun = startTestRun
我也试过这个:https://stackoverflow.com/a/64892396/3337597
import unittest
def startTestRun(self):
print('once before all tests')
setattr(unittest.TestResult, 'startTestRun', startTestRun)
在这两种情况下,所有测试都成功运行,但 startTestRun 没有执行。我不知道为什么。我感谢任何澄清。
(我使用 unittest.TestCase 并通过右键单击测试目录并单击运行“测试中的 Python 测试...”来运行我的测试)
【问题讨论】:
标签: python python-3.x unit-testing python-unittest