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

前言

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

pytest.main()

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

  • args 传一个list对象,list 里面是多个命令行的参数
  • plugins 传一个list对象,list 里面是初始化的时候需注册的插件
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: {}
".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 输出
-v: 丰富信息模式, 输出更详细的用例执行信息
-k: 运行包含某个字符串的测试用例。如:pytest -k add XX.py 表示运行 XX.py 中包含 add 的测试用例。
-q: 简单输出模式, 不输出环境信息
-x: 出现一条测试用例失败就退出测试。在调试阶段非常有用,当测试用例失败时,应该先调试通过,而不是继续执行测试用例。

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

> pytest -s

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

import pytest

# 带上-s参数
pytest.main(["-s"])

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

> pytest -s -x

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

import pytest

# 带上-s -x参数
pytest.main(["-s", "-x"])

指定运行某个用例

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

>pytest cases/module1

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

import pytest

# 运行指定文件夹目录
pytest.main(["cases/module1"])

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

>pytest cases/module1/test_x1.py

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

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() 里面等价于

import pytest

# 运行指定py文件下的test_x
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:wangyiyunweb
plugins: repeat-0.8.0, rerunfailures-9.1, xdist-2.1.0
collected 5 items

casesmodule1	est_x1.py .                                               [ 20%]
casesmodule1	est_x2.py ....                                            [100%]

========================== 5 passed in 0.05 seconds ===========================

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

原文地址:https://www.cnblogs.com/yoyoketang/p/14032069.html