我知道如果我这样做,py.test可以测试一个模块:
py.test mod1.py
Run Code Online (Sandbox Code Playgroud)
或者,我可以在python中调用pytest:
import pytest
pytest.run(['mod1.py'])
Run Code Online (Sandbox Code Playgroud)
我可以在python中执行它,让它运行当前模块吗?我想我能做到:
import pytest
import os
pytest.main([os.path.basename(__file__)])
Run Code Online (Sandbox Code Playgroud)
但是想知道这是否是最"pythonic"的方式.谢谢!
Mat*_*Moy 11
您的版本不允许将额外的参数传递给pytest(例如allow ./test_file.py -v).我试过简单
import sys
if __name__ == '__main__':
pytest.main(sys.argv)
Run Code Online (Sandbox Code Playgroud)
它似乎做了伎俩.sys.argv[0]是脚本名称(即__file__,可能作为相对路径),因此它限制对脚本的调用,并sys.argv[1:]包含在命令行上传递的额外参数.
任何更好的主意赞赏!