前言

pytest 运行用例的时候,一般用命令行去执行,可能是之前深受 unittest 框架的影响,习惯在项目的根目录下写一个 run_all.py 的文件。【使用pytest测试框架一般使用pytest.ini主文件指定运行测试用例;
运行的时候,使用 python 运行 run_all.py 来执行测试用例。

pytest.main()

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

  • args 传一个list对象,list 里面是多个命令行的参数。【包括运行的测试用例(例如:test_a.py);运行测试用例的命令行参数(例如:-vs)】
  • plugins 传一个list对象,list 里面是初始化的时候需注册的插件。

pytest 获取最后结果 pytest执行_pytest 获取最后结果


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


pytest 获取最后结果 pytest执行_用例_02

 main() 函数如果不带任何参数,那么执行的效果跟我们在 cmd 直接运行 pytest 命令一样,默认运行的是当前目录及子目录的所有文件夹的测试用例。

run_all.py/main.py

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

pytest 获取最后结果 pytest执行_用例_03

只需写简单的2行代码:


import pytest # 默认运行的是当前目录及子目录的所有文件夹的测试用例 pytest.main()


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

例如:

pytest 获取最后结果 pytest执行_python_04

带参数运行

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


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


1、在命令行运行带上 -s 参数: pytest -s 

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


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


2、在命令行运行带上多个参数时: pytest -s -x 

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


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


指定运行某个用例

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

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


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


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

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


import pytest # 运行指定py文件 pytest.main(["cases/module1/test_x1.py"])


3、运行指定的 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"])


plugins参数的使用

一般我们写插件的代码放到 conftest.py 会被pytest查找到;

如果不是写到 conftest.py 的插件内容,可以通过 plugins 参数指定加载:【默认级别相当于fixture函数中的session级别:在所有目录以及子目录的用例执行之前首先执行一次仅执行一次plugins指定的函数】

pytest 获取最后结果 pytest执行_测试用例_05


# run_all.py 
import pytest


# 在run_all.py下自定义插件
class MyPlugin(object):
    def pytest_sessionstart(self):
        print("*** test run start blog地址 ")


# plugins指定加载插件

pytest.main(["cases/module1"], plugins=[MyPlugin()])


pytest 获取最后结果 pytest执行_用例_06

运行后会看到在测试用例开始执行之前打印上面的内容:

pytest 获取最后结果 pytest执行_命令行_07


*** test run start blog地址 
============================= 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 ===========================


pytest 获取最后结果 pytest执行_python_08

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