前言pytest 运行用例的时候,一般用命令行去执行,有些小伙伴不太习惯命令行运行用例,可能是之前深受 unittest 框架的影响,习惯在项目的根目录下写一个 run_all.py 的文件。 pytest.main()先看看 pytest.main() 的源码, main 函数的内容
def main(args=None, plugins=None):
""" return exit code, after performing an in-process test run.
:arg args: list of command line arguments.
:arg plugins: list of plugin objects to be auto-registered during
initialization.
"""
from _pytest.main import EXIT_USAGEERROR
try:
try:
config = _prepareconfig(args, plugins)
except ConftestImportFailure as e:
exc_info = ExceptionInfo(e.excinfo)
tw = py.io.TerminalWriter(sys.stderr)
tw.line(
"ImportError while loading conftest '{e.path}'.".format(e=e), red=True
)
exc_info.traceback = exc_info.traceback.filter(filter_traceback)
exc_repr = (
exc_info.getrepr(style="short", chain=False)
if exc_info.traceback
else exc_info.exconly()
)
formatted_tb = safe_str(exc_repr)
for line in formatted_tb.splitlines():
tw.line(line.rstrip(), red=True)
return 4
else:
try:
return config.hook.pytest_cmdline_main(config=config)
finally:
config._ensure_unconfigure()
except UsageError as e:
tw = py.io.TerminalWriter(sys.stderr)
for msg in e.args:
tw.line("ERROR: {}\n".format(msg), red=True)
return EXIT_USAGEERROR 如果不带任何参数,那么执行的效果跟我们在 cmd 直接运行 pytest 命令一样,默认运行的是当前目录及子目录的所有文件夹的测试用例 > pytest run_all.py在项目的根目录,新建一个 run_all.py 的文件 只需写简单的2行代码 import pytest
# 默认运行的是当前目录及子目录的所有文件夹的测试用例
pytest.main() 这样就能在 pycharm 里面右键运行,不带参数默认运行当前目录及子目录的所有文件夹的测试用例 带参数运行在运行的时候,也可以指定参数运行 -s:显示程序中的 print/logging 输出 在命令行运行带上 -s 参数 > pytest -s 那么在 pytest.main() 里面等价于 import pytest
# 带上-s参数
pytest.main(["-s"]) 在命令行运行带上多个参数时 > pytest -s -x 那么在 pytest.main() 里面等价于 import pytest
# 带上-s参数
pytest.main(["-s", "-x"]) 指定运行某个用例指定运行 cases/module1 文件夹下的全部用例, 在命令行运行时, 先 cd 到项目的根目录 >pytest cases/module1 那么在 pytest.main() 里面等价于 import pytest
# 带上-s参数
pytest.main(["cases/module1"]) 运行指定的 cases/module1/test_x1.py 下的全部用例,在命令行运行时, 先cd到项目的根目录
import pytest
# 运行指定py文件
pytest.main(["cases/module1/test_x1.py"]) 运行指定的 cases/module1/test_x1.py 下的某一个用例 test_x, 在命令行运行时, 先cd到项目的根目录
import pytest
# 运行py文件下指定的用例
pytest.main(["cases/module1/test_x1.py::test_x"]) 通过上面跟命令行运行的对比,对 pytest.main() 的使用也就基本掌握了 plugins参数的使用一般我们写插件的代码放到 conftest.py 会被pytest查找到,如果不是写到 conftest.py 的插件内容,可以通过 plugins 参数指定加载 # run_all.py
import pytest
# 在run_all.py下自定义插件
class MyPlugin(object):
def pytest_sessionstart(self):
print("*** test run start blog地址 https://www.cnblogs.com/yoyoketang/")
# plugins指定加载插件
pytest.main(["cases/module1"], plugins=[MyPlugin()]) 运行后会看到在参数用例开始之前答应上面的内容 *** test run start blog地址 https://www.cnblogs.com/yoyoketang/
============================= test session starts =============================
platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
rootdir: D:\wangyiyun\web
plugins: repeat-0.8.0, rerunfailures-9.1, xdist-2.1.0
collected 5 items
cases\module1\test_x1.py . [ 20%]
cases\module1\test_x2.py .... [100%]
========================== 5 passed in 0.05 seconds =========================== plugins参数的作用就是指定需加载的插件,也可以指定多个。 2020年第五期《python接口自动化+测试开发》课程,10月11号开学(火热报名中!) |
|