前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest文档60-pytest.main()的使用

pytest文档60-pytest.main()的使用

作者头像
上海-悠悠
发布2020-12-03 10:55:50
4.2K0
发布2020-12-03 10:55:50
举报
文章被收录于专栏:从零开始学自动化测试

前言

pytest 运行用例的时候,一般用命令行去执行,有些小伙伴不太习惯命令行运行用例,可能是之前深受 unittest 框架的影响,习惯在项目的根目录下写一个 run_all.py 的文件。 运行的时候,使用 python 运行 run_all.py 来执行测试用例。

pytest.main()

先看看 pytest.main() 的源码, main 函数的内容

  • args 传一个list对象,list 里面是多个命令行的参数
  • plugins 传一个list对象,list 里面是初始化的时候需注册的插件
代码语言:javascript
复制
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_USAGEERRORtry:
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 命令一样,默认运行的是当前目录及子目录的所有文件夹的测试用例

代码语言:javascript
复制
> pytest

run_all.py

在项目的根目录,新建一个 run_all.py 的文件

只需写简单的2行代码

代码语言:javascript
复制
import pytest# 默认运行的是当前目录及子目录的所有文件夹的测试用例
pytest.main()

这样就能在 pycharm 里面右键运行,不带参数默认运行当前目录及子目录的所有文件夹的测试用例

带参数运行

在运行的时候,也可以指定参数运行

-s:显示程序中的 print/logging 输出 -v: 丰富信息模式, 输出更详细的用例执行信息 -k:运行包含某个字符串的测试用例。如:pytest -k add XX.py 表示运行 XX.py 中包含 add 的测试用例。 -q: 简单输出模式, 不输出环境信息 -x: 出现一条测试用例失败就退出测试。在调试阶段非常有用,当测试用例失败时,应该先调试通过,而不是继续执行测试用例。

在命令行运行带上 -s 参数

代码语言:javascript
复制
> pytest -s

那么在 pytest.main() 里面等价于

代码语言:javascript
复制
import pytest# 带上-s参数
pytest.main(["-s"])

在命令行运行带上多个参数时

代码语言:javascript
复制
> pytest -s -x

那么在 pytest.main() 里面等价于

代码语言:javascript
复制
import pytest# 带上-s参数
pytest.main(["-s", "-x"])

指定运行某个用例

指定运行 cases/module1 文件夹下的全部用例, 在命令行运行时, 先 cd 到项目的根目录

代码语言:javascript
复制
>pytest cases/module1

那么在 pytest.main() 里面等价于

代码语言:javascript
复制
import pytest# 带上-s参数
pytest.main(["cases/module1"])

运行指定的 cases/module1/test_x1.py 下的全部用例,在命令行运行时, 先cd到项目的根目录 ‘’’

pytest cases/module1/test_x1.py ‘’’ 那么在 pytest.main() 里面等价于

代码语言:javascript
复制
import pytest# 运行指定py文件
pytest.main(["cases/module1/test_x1.py"])

运行指定的 cases/module1/test_x1.py 下的某一个用例 test_x, 在命令行运行时, 先cd到项目的根目录 ‘’’

pytest cases/module1/test_x1.py::test_x ‘’’ 那么在 pytest.main() 里面等价于

代码语言:javascript
复制
import pytest# 运行py文件下指定的用例
pytest.main(["cases/module1/test_x1.py::test_x"])

通过上面跟命令行运行的对比,对 pytest.main() 的使用也就基本掌握了

plugins参数的使用

一般我们写插件的代码放到 conftest.py 会被pytest查找到,如果不是写到 conftest.py 的插件内容,可以通过 plugins 参数指定加载

代码语言:javascript
复制
# 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()])

运行后会看到在参数用例开始之前答应上面的内容

代码语言:javascript
复制
*** 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 itemscases\module1\test_x1.py .                                               [ 20%]
cases\module1\test_x2.py ....                                            [100%]========================== 5 passed in 0.05 seconds ===========================

plugins参数的作用就是指定需加载的插件,也可以指定多个。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • pytest.main()
  • run_all.py
  • 带参数运行
  • 指定运行某个用例
  • plugins参数的使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档