pal*_*asb 5 python python-unittest
假设有以下测试套件:
# test_module.py
import unittest
class Tests(unittest.TestCase):
@unittest.skip
def test_1(self):
print("This should run only if explicitly asked to but not by default")
# assume many other test cases and methods with or without the skip marker
Run Code Online (Sandbox Code Playgroud)
当通过调用unittest库时,python -m unittest是否有任何参数可以传递给它实际运行而不是跳过Tests.test_1而不修改测试代码并运行任何其他跳过的测试?
python -m unittest test_module.Tests.test_1正确地选择此作为唯一要运行的测试,但它仍然会跳过它。
如果不修改测试代码就无法做到这一点,那么我可以做的最惯用的更改是什么来有条件地撤消@unittest.skip并运行一个特定的测试用例?
在所有情况下,我仍然希望python -m unittest discover(或任何其他未显式打开测试的调用)跳过测试。
如果您想跳过一些昂贵的测试,您可以将条件跳过与自定义环境变量一起使用:
@skipIf(int(os.getenv('TEST_LEVEL', 0)) < 1)
def expensive_test(self):
...
Run Code Online (Sandbox Code Playgroud)
然后您可以通过指定相应的环境变量来包含此测试:
TEST_LEVEL=1 python -m unittest discover
TEST_LEVEL=1 python -m unittest test_module.Tests.test_1
Run Code Online (Sandbox Code Playgroud)
如果您因为预计测试会失败而想要跳过测试,则可以使用专用expectedFailure装饰器。
顺便说一下,pytest有一个专门的装饰器用于标记慢速测试。
| 归档时间: |
|
| 查看次数: |
2251 次 |
| 最近记录: |