[英]Using Python's `unittest` module to compose tests of functions in a separate file
[英]How to run initialization code before tests when using Python's unittest module as a testrunner?
在运行库提供的测试之前,库的用户如何运行自己的初始化代码(例如设置记录器的调试级别)? Python的unittest
模块用作测试人员。
您可以尝试使用pytest来运行单元测试。 如果可行(许多基于单元测试的测试套件工作),那么你可以创建一个小模块,例如“mymod.py”,定义一个pytest配置钩子:
# content of mymod.py
def pytest_configure():
import logging
logging.getLogger().setLevel(logging.WARN)
如果您现在执行py.test,如下所示:
$ py.test -p mymmod --pyargs mylib
然后将搜索“mylib”包以进行测试,并在运行它们之前修改日志记录级别。 “-p”选项指定要加载的插件,“ - pygs”告诉pytest首先尝试导入参数,而不是将它们视为目录或文件名(默认值)。
有关详细信息,请访问: http : //pytest.org和http://pytest.org/latest/unittest.html
HTH,holger
类似问题的回答在这里更有帮助:
我用python setUpClass方法https://docs.python.org/2/library/unittest.html#unittest.TestCase.setUpClass
setUpClass()
A class method called before tests in an individual class are run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():
@classmethod
def setUpClass(cls):
...
我像这样运行我的测试:
python -m unittest discover -v
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.