阿里软件测试工程师呕心沥血熬夜三天三夜制定出这套pytest自动化测试框架学习路线

目录

一 简述

二 默认规则

三 pytest需要安装的插件

四 执行方式

五 分组执行

六 跳过用例

七 生成报告

八 生成allure测试报告

九 参数化

十 YAML文件


一 简述

首先我们要了解什么是pytest框架:

pytest是一个非常成熟的全功能的python测试框架,主要有以下几个特点:

1.简单灵活,容易上手

2.支持参数化

3.能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests)

4.pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等

5.测试用例的skip和xfail处理

6.可以很好的和jenkins集成

7.report框架----allure 也支持了pytest

二 默认规则

(1)模块名必须以test_开头或者_test结尾

(2)测试类必须以Test开头,并且不能有init方法

(3)测试方法必须以test开头

三 pytest需要安装的插件

pytest-html:生成html格式的自动化测试报告

pytest-xdist:测试用例分布式执行,多CPU分发

pytest-ordering:用于改变测试用例的执行顺序

pytest-rerunfailures:用于失败后重跑

allure-pytest:用于生成美观的测试报告

四 执行方式

(1)主函数模式:

        # (1)运行全部
        pytest.main()
        # (2)运行指定模块
        pytest.main(["-vs","test_case_run02.py"])
        # (3)运行指定文件夹(目录)
        pytest.main(["-vs","../pytest"])
        # (4)通过nodeid指定测试用例运行,nodeid = 模块名,分隔符,类名,方法名,函数名组成
        pytest.main(["-vs", "../pytest/test_case_run02.py::test_04"])
        # (5)多线程或分布式运行测试用例
        pytest.main(["-vs", "../pytest/test_case_run02.py","-n=2"])
        # (6)失败用例重跑
        pytest.main(["-vs", "../pytest/test_case_run02.py","--reruns=1"])
        # (7)只要有一个用例错了,测试停止
        pytest.main(["-vs", "../pytest/test_case_run02.py","-x"])   #用的较少
        # (8)出现2个用例失败就停止
        pytest.main(["-vs", "../pytest/test_case_run02.py", "--maxfail=2"])   #用的较少
        # (9)根据测试用例的部分字符串指定测试用例
        pytest.main(["-vs", "../pytest/test_case_run02.py", "-k=AA",])

(2)命令行模式:

    (1)运行全部:pytest
    (2)运行指定模块:pytest -vs test_case_run02.py
    (3)运行指定文件夹(目录):pytest -vs ../pytest
    (4)运行指定文件夹(目录):pytest -vs ../pytest/test_case_run02.py::TestLogin02::test_02
    (5)分2个线程运行:pytest -vs ../pytest/test_case_run02.py -n 2
    (6)失败用例重跑:pytest -vs ../pytest/test_case_run02.py --reruns 2
    (7)只要有一个用例错了,测试停止:pytest -vs ../pytest/test_case_run02.py -x
    (8)出现2个用例失败就停止:pytest -vs ../pytest/test_case_run02.py --maxfail 2
    (9)根据测试用例的部分字符串指定测试用例::pytest -vs ../pytest/test_case_run02.py -k "AA"

说明:

-s:表示输出调试信息,包括print的打印内容
-v:显示更详细的信息
-vs:两个参数一起用
-n:支持多线程或分布式运行测试用例
如:pytest -vs ../pytest/test_case_run02.py -n 2 (分2个线程运行)
--reruns NUM :失败用例重跑多少次
-x:只要有一个用例错了,测试停止
--maxfail=2:出现2个用例失败就停止
-k:根据测试用例的部分字符串指定测试用例

(3) 通过读取pytest.ini配置文件运行 

pytest.ini是pytest的主配置文件,可以改变pytest的默认行为,必须放在项目根目录下 ,不可乱放、乱命名

# pytest 配置文件可以改变 pytest 的运行方式,它是一个固定的文件 pytest.ini 文件,读取配置信息,按指定的方式去运行。

(定位到根目录pythonProject,点击下面的【Terminal】输入pytest -vs ./pytest即可)

pytest.ini配置文件

        [pytest]
        # 添加命令行参数
        addopts = -s --html ./cases/report0001.html
        # 文件搜索路径
        testpaths=./pytest
        # 文件名称
        python_files = test_*.py
        # 类名称
        python_classes = Test*
        # 方法名称
        python_functions = test_*
        # 标记用例分模块执行测试用例
        markers =
                smoke:冒烟用例
                hanshu:某个函数

五 分组执行

给用例:@pytest.mark.somke

根节点-【Terminal】执行:pytest -m “smoke or hanshu” #几个模块加几个or即可

六 跳过用例

无条件:
@pytest.mark.skip(reason="测试跳过")   # 无条件跳过
    def test_BB(self):
        print("被跳过!")
有条件:
    @pytest.mark.skipif(num=10,reason="测试跳过")  #有条件跳过
    def test_CC(self):
        print("被跳过!")

七 生成报告

pytest.ini配置文件中

# 添加命令行参数

        addopts = -s --html ./cases/report0001.html

八 生成allure测试报告

1.下载、解压、配置path路径(将bin文件所在路径加入到path中)

2.验证:(1)cmd后,allure --version ;(2)重启pycharm allure --version

3.pytest.ini配置文件中

# 添加命令行参数

        addopts = -s --alluredir ./temp

4.生成allure报告:

    os.system('allure generate ./temp -o ./allure-report --clean')

说明:

allure generate:命令,固定的
./temp:临时的json格式报告的路径
-o:输出output
./allure-report:生成的allure报告的路径
--clean:清空./allure-report路径下原来的报告

完整代码 all.py:

import pytest
import os

if __name__ == '__main__':
    pytest.main()
    os.system("allure generate ./temp -o ./allure-report --clean")

或者

import pytest
import os

if __name__ == '__main__':
    pytest.main(["-vs", "../pytest/test_case_run02.py","--alluredir","./temp"])
    os.system("allure generate ./temp -o ./allure-report --clean")

九 参数化

@pytest.mark.parameterize(argnames,argvalues) 
  • argnames表示:参数名称,是一串字符,多个桉树之间由都好隔开"username,password"
  • argvalues表示:参数化的数据,用列表或元组来表示,[("13825110000‘,”123456“),(13900000000”,“111111”)

用法1:

    @pytest.mark.parametrize('args', {'周一', '周二', '周三'})
    def test_para(self, args):
        print(args)

用法2(完整代码):

import pytest

list1 = [(1, 1, 1), (3, 3, 9), (5, 6, 30)]
list2 = [(1, 3, 3), (2, 3, 6), (3, 6, 18)]

def multi(a, b):
    return a * b

class TestParameterized:
    @pytest.mark.parametrize('args', {'周一', '周二', '周三'})  # 第一种参数化方法
    def test_para(self, args):
        print(args)

    @pytest.mark.parametrize("a,b,c", list1)    #第二种参数化方法
    def test_1_multi(self, a, b, c):
        print("测试函数1的数据a是{},b是{},c是{}".format(a, b, c))
        assert multi(a, b) == c

    @pytest.mark.parametrize("a,b,c", list2)
    def test_2_multi(self, a, b, c):
        print("测试函数2的数据a是{},b是{},c是{}".format(a, b, c))
        assert multi(a, b) == c

if __name__ == '__main__':
    pytest.main(["-vs","test_parameterized.py"])

十 YAML文件

1.yaml文件格式校验网站:

YAML、YML在线编辑器(格式化校验)-BeJSON.com​www.bejson.com/validators/yaml_editor/正在上传…重新上传取消

2.YAML 是一种可读性非常高,与程序语言数据结构非常接近。同时具备丰富的表达能力和可扩展性,并且易于使用的数据标记语言。

基本语法规则(很接近 Python 的语法规则):

1.大小写敏感
2.使用缩进表示层级关系
3.不允许使用 TAB 键来缩进,只允许使用空格键来缩进
4.缩进的空格数量不重要
5.使用"#"来表示注释

支持的数据格式:

1.对象:键值对的集合,又称映射 (mapping) / 哈希(hashes)/ 字典 (dictionary)
2.数组: 一组按次序排列的值,又称序列 (sequence) / 列表 (list)
3.纯量 (scalars) :单个的,不可再分的值

1、字典格式,Key: value,(value前必须加空格!!)

    webserver:
      ip: 192.168.1.1
      port: 8000
      create_date: 2019-06-09
      create_time:1:01:01:01

# 结果:

    { webserver:
       { ip: '192.168.1.1',
         port: 8000,
         create_date: Sun Jun 09 2019 08:00:00 GMT+0800 (中国标准时间),
         create_time: 219661 } }

2、列表格式,- value,-value前必须加空格

    ip:
      - 192.168.1.1
      - 192.168.1.2
      - 192.168.1.3
      - 192.168.1.4

# 结果:

    { ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] }

3、列表和字典互相嵌套

    webserver:
      - ip:
        - 192.168.1.1
        - 192.168.1.2
        - 192.168.1.3
        - 192.168.1.4
      - port: 8000

# 列表和字典嵌套

# 结果:

    { webserver:
       [ { ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] },
         { port: 8000 } ] }

4、强制数据类型转换

    port: !!str 8000
    num: !!int '1999'
    boolean: !!bool 'true'
    second: !!float '18.362'

# yaml中,强制转换时,只是给字符串加引号,去引号,如果去引号后的值和要求的类型不符,转换报错。

# 结果

{ port: '8000', num: 1999, boolean: true, second: 18.362 }

【相关完整代码】

分组执行、该顺序、跳过

  test_pytest.py

import pytest

@pytest.mark.hanshu   # 标记测试用例为函数,用于分组执行
def test_AA():
    print("函数函数函数函数")


class TestLogin02:
    @pytest.mark.run(order=3)    #用于改变测试用例的执行顺序
    def test_05(self):
        print("pytest学习!222222222222")

    @pytest.mark.run(order=1)   # 失败的测试用例
    def test_03(self):
            print("pytest学习!33333333")
            assert 1 == 2

    @pytest.mark.run(order=2)
    def test_04(self):
            print("pytest学习!44444444444")

    @pytest.mark.skip(reason="测试跳过")   # 无条件跳过
    def test_BB(self):
        print("被跳过!")

    @pytest.mark.skipif(num=10,reason="测试跳过")  #有条件跳过
    def test_CC(self):
        print("被跳过!")

  pytest.ini:配置文件(根目录)

[pytest]
addopts=-vs --alluredir ./temp
testpaths=./pytest
python_files=test_*.py
python_classes=TestParameterized*
python_functions=test
markers=
    smoke:冒烟用例
    hanshu:函数

  all.py:allure报告运行

import pytest
import os

if __name__ == '__main__':
    pytest.main()
    os.system("allure generate ./temp -o ./allure-report --clean")

  allure报告:

B站最牛的Python自动化测试框架全栈测试开发实战项目入门到精通,涨薪必备教程!!!

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值