二.pytest默认的测试用例的规则以及基础应用

2021-04-17  本文已影响0人  TheRightPath1
1. 命名规则

(1)模块名必须以test_开头或者_test结束
(2)测试类必须以Test开头,并且不能有init方法
(3)测试方法必须以test开头

2. 运行方式

(1)主函数运行方式:
① 运行所有的测试用例:直接新建一个文件,执行pytest.main()
② 运行指定模块的测试用例:pytest.main(['-vs', 'test_login.py'])
③ 运行指定目录的测试用例:pytest.main(['-vs', './test_demo'])
④ 通过nodeid运行指定的用例:nodeid由模块名,分隔符,类名,方法名,函数名组成:
pytest.main(['-vs', './test_demo/test_login,py::test_login']) # 指定运行的函数名
pytest.main(['-vs', './test_demo/test_login,py::TestLogin::test_login'])# 指定运行的类函数名
(2)命令行运行模式
① 运行所有的模块:直接在命令行中输入pytest
② 运行指定模块的测试用例:pytes -vs test_login.py
③ 运行指定目录的测试用例:pytest -vs ./test_demo
④ 通过nodeid运行指定的用例:nodeid由模块名,分隔符,类名,方法名,函数名组成:
pytest -vs /test_demo/test_login,py::test_login # 指定运行的函数名
pytest -vs ./test_demo/test_login,py::TestLogin::test_login # 指定运行的类函数名

(3)通过读取pytest.ini配置文件运行
pytest.ini这个文件是pytest单元测试框架的核心配置文件

  1. 位置:一般放在项目的根目录
  2. 编码:必须是ANSI编码
  3. 作用:改变pytest默认的行为,比如命名必须以test_开头
  4. 运行的规则:不管是主函数的模式运行或者是命令行运行模式都会读取这个文件
    文件样式:
    [pytest]
    addopts = -vs # 命令行的参数,用空格分割
    testpaths = ./testcase #测试用例的路径
    python_files = test_.py # 模块名的规则
    python_classes = Test
    # 类名的规则
    python_functions = test # 方法名的规则

常用运行参数解释:
-s:表示输出调试信息,包括print打印的信息
-v:显示更详细的信息
-vs:这两个参数一起使用
-n:支持多线程或者分布式运行测试用例
如:pytest.main(['-vs', './test_demo/test_login,py::TestLogin::test_login', '-n=2']) # 使用-n指定两个线程运行
--rerun Num:失败用例重新运行几次
如:pytest.main(['-vs', './test_demo/test_login,py::TestLogin::test_login', '--reruns=2'])
-x:表示只要一个测试用例报错,那么测试停止
--maxfail NUM:表示最多出现NUM错误就停止运行
-k:根据测试用例的部分字符串指定测试用例,如pytest.main(['-vs', './test_demo/test_login,py::TestLogin::test_login', '-k="ao"]) # 表示只指定包含ao字符的用例函数
--html 文件名:生成html的测试报告

3. 测试用例的执行顺序
默认是从上到下顺序执行的,但是可以通过pytest.mark.run装饰器修改测试用例函数的执行顺序
import pytest


class TestOrder(object):

    def test_order1(self):
        print('order1')

    def test_order2(self):
        print('order2')

    @pytest.mark.run(order=3)
    def test_order3(self):
        print('order3')

    @pytest.mark.run(order=1)
    def test_order4(self):
        print('order4')

    @pytest.mark.run(order=2)
    def test_order5(self):
        print('order5')
4. 分组执行用例

当我们有多个测试用例的class时,如果我们只想执行其中的一个或几个用例而不是全部执行,那么可以使用分组执行用例
(1)使用pytest.mark.xxx标记指定函数的组名

import pytest


class TestOrder(object):
    @pytest.mark.qqq
    def test_order1(self):
        print('order1')

    @pytest.mark.www
    def test_order2(self):
        print('order2')

    @pytest.mark.run(order=3)
    @pytest.mark.www
    def test_order3(self):
        print('order3')

    @pytest.mark.run(order=1)
    @pytest.mark.www
    def test_order4(self):
        print('order4')

    @pytest.mark.run(order=2)
    @pytest.mark.www
    def test_order5(self):
        print('order5')

(2)运行pytest中添加marker=xxx的组名配置

[pytest]
addopts = -vs
testpaths = ./testcase
python_files = test_*.py
python_classes = Test*
python_functions = test
markers =
    www
    qqq

(3)运行测试脚本时添加-m参数

import pytest


if __name__ == '__main__':
    pytest.main(['-vs', 'test_order.py', '-m=www or qqq'])
5. 跳过指定的用例

(1)无条件跳过指定的用例:使用skip装饰器,reason参数可不填

@pytest.mark.skip(reason='xxx')

(2)有条件跳过指定的用例:使用ifskip装饰器,第一个参数是条件,reason参数可不填

@pytest.mark.ifskip(age<18,reason='xxx')
上一篇下一篇

猜你喜欢

热点阅读