pytest使用

主要技术内容

1.pytest设计 接口测试 框架设想

common—公共的东西封装

1.request请求

2.Session

3.断言

4.Log

5.全局变量

6.shell命令

❖ config---配置文件及读取

❖ Log—

❖ payload—请求参数—*.yaml及读取

❖ testcases—conftest.py; testcase1.py…….可以有包,最好按模块就近划分

❖ result/report— 运行数据和报告

❖ 测试 策略—组织 运行all_test.py; p0_test.py; ……

2.框架执行简化流程

1.初始化配置

2.查找测试 用例套件
3.运行套件
4.初始化测试 数据pytest.fixture()

5.执行测试用例

6.读取yaml等测试数据

7.request运行

8.assert验证

9.allure记录报告

10.可与ienkins集成发邮件

3.测试 套件设计 模块化思想

4.测试 用例和套件落地思路

搜索功能—五个接口请求—返回股票stock信息;返回讨 论信息,组合,用户,群组;综合

❖ 搜索是个套件,五个接口分别是测试 用例,鉴于pytest 套件的灵活性,可以设计为 一个文件一个类下-【搜索】 下5个不同测试 方法【接口】(或可一个文件一个方法一 个接口设计 也行,灵活性强。这需要根据实际权 衡,如 果需要灵活多选择 后者,如果相对稳 定建议前者)

❖ 如果是通用的功能,可以放在共享的文件中-conftest.py

5.pytest的相关回顾

1. pytest是一个非常成熟的全功能的Python测试 框架

2. 简单 灵活,容易上手;支持参数化;

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

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

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

6. 可以很好的和jenkins集成;

7. https://docs.pytest.org/en/latest/contents.html#toc 

pytest安装,导入相关依赖库

Pip install –U pytest U表示升级 ➢ Pip intall –U pytest-html ➢ pip install -U pytest-rerunfailures ➢ Pip install pytest-assume ➢ Pip install pytest-allure ➢ Pip install pytest-xdist ➢ Pip list查看 ➢ Pytest –h 帮助 兹测试学

Pytest框架结构

Import pytest

类似的setup,teardown同样更灵活,

模块级 (setup_module/teardown_module)模块始末,全局的

函数级(setup_function/teardown_function)不在类中的函数有用

类级 (setup_class/teardown_class)只在类中前后运行一次

方法级(setup_method/teardown_methond)运行在类中方法始末 

session() 


#这是一个模块model
import pytest


@pytest.fixture(scope="module", autouse=True)
def setup_model():
    print("setup_model")
    yield
    print("teardown_model")

#类class在模块中定义,它们可以包含多个方法和数据成员。
class TestClassA:

    def setup_class(self):
        print('\nsetup class')


    def teardown_class(self):
        print('\nteardown_class')


    #方法method是与类关联的函数
    def setup_method(self):
        print("\nsetup_method")


    def teardown_method(self):
        print("\nteardown_method")

    def test_method_1(self):
        print("TestClassA: test_method_1")

    def test_method_2(self):
        print("TestClassA: test_method_2")


class TestClassB:
    def test_method_1(self):
        print("TestClassB: test_method_1")

    def test_method_2(self):
        print("TestClassB: test_method_2")


#全局函数function,class内的函数function是方法method


def setup_function():
    print("setup_function")


def teardown_function():
    print("teardown_function")


def test_function_1():
    print("test_function_1")


def test_function_2():
    print("test_function_2")



if __name__ == "__main__":
    pytest.main()

module

import pytest

# 功能函数
def multiply(a, b):
    return a * b

# ====== fixture ======
def setup_module():
    print("setup_module():在整个模块之前执行")

def teardown_module():
    print("teardown_module():在整个模块之后执行")

def setup_function(function):
    print("setup_function():在每个方法之前执行")

def teardown_function(function):
    print("teardown_function():在每个方法之后执行")

def test_multiply_1():
    print("正在执行test1")
    assert multiply(3, 4) == 12

def test_multiply_2():
    print("正在执行test2")
    assert multiply(3, 'a') == 'aaa'


 

class

def multiply(a, b):
    """Return the product of two numbers."""
    return a * b


# Test fixture for the multiply function
class TestMultiply:
    def setup_class(cls):
        print("setup_class(): Executed before any method in this class")

    def teardown_class(cls):
        print("teardown_class(): Executed after all methods in this class")

    def setup_method(self):
        print("setup_method(): Executed before each test method")

    def teardown_method(self):
        print("teardown_method(): Executed after each test method")

    def test_multiply_3(self):
        """Test multiply with two integers."""
        print("Executing test3")
        assert multiply(3, 4) == 12

    def test_multiply_4(self):
        """Test multiply with an integer and a string (should fail)."""
        print("Executing test4")
        # Note: This test will raise a TypeError due to incompatible types.
        assert multiply(3, 'a') == 'aaa'

Pytest框架assert断言使用 断言:支持显示最常见的子表达式的值,包括调用,属性,比较以及二元和一元运算符。 包含,相等,不等,大于 小于运算,assertnot 假

assert “h” in “hello”(判断h在hello中)

assert 3==4(判断3=4)

assert 3!=4(判断3!=4)

assert f() ==4 (判断f()方法返回值是否=4)

assert 5>6 (判断5>6为真)

assert not xx (判断xx不为真)

assert {'0', '1', '3', '8'} == {'0', '3', '5', '8'} 断言列表或者字典

import pytest

def intest():
    assert 'h' in "hello"
    assert 3==4
    # (判断3=4)
    assert 3!=4
    # (判断3!=4)
    assert f() ==4
    # (判断f()方法返回值是否=4)
    assert 5>6
    # (判断5>6为真)
    assert not xx
    # (判断xx不为真)
    assert {'0', '1', '3', '8'} == {'0', '3', '5', '8'}
    # 断言列表或者字典


def add(a, b):
    return a+b
# 测试相等

def test_add1():
    assert add(3, 4) == 7


# 测试不相等
def test_add_2():
    assert add(17, 22) != 50


# 测试大于或等于
def test_add_3():
    assert add(17, 22) <= 50


# 测试小于或等于
def test_add_4():
    assert add(17, 22) >= 38

# 功能:用于判断素数
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

# 判断是否为True
def test_true_1():
    assert is_prime(13)

# 判断是否为True
def test_true_2():
    assert is_prime(7) is True
# 判断是否不为True
def test_true_3():
     assert not is_prime(4)
# 判断是否不为True
def test_true_4():
    assert is_prime(6) is not True
# 判断是否为False
def test_false_1():
    assert is_prime(8) is False

# 测试包含
def test_in():
 a = "hello"
 b = "he"
 assert b in a

# 测试不包含
def test_not_in():
    a = "hello"
    b = "hi"
    assert b not in a


if __name__ == "__main__":
    pytest.main()

pytest-fixture的灵活

Fixture优势 :

1、对于setup,teardown,可以不起这两个名字,所以命名方式 灵活。(可独立命名,声明后激活)前置后置

import pytest

@pytest.fixture()
def login():
    print('这是个登陆模块!')


def test_soso(login):
    print('case1:登际后执行搜索')


def test_cakan():
    print('case2:不登陆就看')


def test_cart(login):
    print('case3:登陆,加购物车')

2、数据共享。在conftest.py配置里写方法可以实现 数据共享 ,不需要import导入。可以跨文件共享

创建conftest文件

import pytest

@pytest.fixture()
def login():
    print('From_conftest - 这是个登陆模块!')
import pytest


def test_soso(login):
    print('case1:登际后执行搜索')


def test_cakan():
    print('case2:不登陆就看')


def test_cart(login):
    print('case3:登陆,加购物车')

3、 神奇的yield,提供返回值,不是return;;相当于setup 和 teardown 

import pytest


@pytest.fixture(scope="module", autouse=True)
def setup_model():
    print("setup_model")
    yield
    print("teardown_model")


def test_soso(setup_model):
    print('case1:登际后执行搜索')


def test_cakan():
    print('case2:不登陆就看')


def test_cart(setup_model):
    print('case3:登陆,加购物车')



if __name__ == "__main__":
    pytest.main()

pytest-fixture

pytest-fixture-CSDN博客

1.Fixture(scope=“function”,params=None, autouse=False, ids=None, name=None):

2.可以使用此装饰器定义fixture

1. scope:=module,session,class,function;

function:每个test都运行,默认是function的scope ,

class:每个 class的所有test只运行一次;

module:每个module的所有test只运行一次;

session:每个session只运行一次


#这是一个模块model
import pytest


@pytest.fixture(scope="class", autouse=True)
def setup_model():
    print("setup_model_class")
    yield
    print("teardown_model_class")

#类class在模块中定义,它们可以包含多个方法和数据成员。
class TestClassA:

    def setup_class(self):
        print('\nsetup class')


    def teardown_class(self):
        print('\nteardown_class')


    #方法method是与类关联的函数
    def setup_method(self):
        print("\nsetup_method")


    def teardown_method(self):
        print("\nteardown_method")

    def test_method_1(self):
        print("TestClassA: test_method_1")

    def test_method_2(self):
        print("TestClassA: test_method_2")


class TestClassB:
    def test_method_1(self):
        print("TestClassB: test_method_1")

    def test_method_2(self):
        print("TestClassB: test_method_2")


#全局函数function,class内的函数function是方法method


def setup_function():
    print("setup_function")


def teardown_function():
    print("teardown_function")


def test_function_1():
    print("test_function_1")


def test_function_2():
    print("test_function_2")



if __name__ == "__main__":
    pytest.main()

2. params:可选多个参数调用fixture

3. Autouse:如果是True,则为 所有测试 激活fixture,如果是False,则需要显式参考来激活。


#这是一个模块model
import pytest


@pytest.fixture(scope="class")
def setup_model():
    print("setup_model_class")
    yield
    print("teardown_model_class")

#类class在模块中定义,它们可以包含多个方法和数据成员。
class TestClassA:

    def setup_class(self):
        print('\nsetup class')


    def teardown_class(self):
        print('\nteardown_class')


    #方法method是与类关联的函数
    def setup_method(self):
        print("\nsetup_method")


    def teardown_method(self):
        print("\nteardown_method")

    def test_method_1(self,setup_model):
        print("TestClassA: test_method_1")

    def test_method_2(self):
        print("TestClassA: test_method_2")


class TestClassB:
    def test_method_1(self):
        print("TestClassB: test_method_1")

    def test_method_2(self):
        print("TestClassB: test_method_2")


#全局函数function,class内的函数function是方法method


def setup_function():
    print("setup_function")


def teardown_function():
    print("teardown_function")


def test_function_1():
    print("test_function_1")


def test_function_2():
    print("test_function_2")



if __name__ == "__main__":
    pytest.main()

4. ids:每个字符串ID的列表,每个字符串对应 于param,这是测试 ID的一部分。  

import pytest


@pytest.fixture(params=['www.baidu.com','www.bingying.com'],
                ids=['case1','case2'])
def urls(request):
    return request.param


def test_url(urls):
    url = urls
    print("url:",url)

5. Name:fixture的名称。

3.fixture带参数传递 -通过固定参数request传递 ;传二个以上参数(通过字典,其实算一个),可 以传多个组合测试 。

import pytest


@pytest.fixture(params=['www.baidu.com','www.bingying.com'],
                ids=['case1','case2'])
def urls(request):
    return request.param


def test_url(urls):
    url = urls
    print("url:",url)

传一个字典

import pytest


@pytest.fixture(params=[{'name':'baidu','url':'www.baidu.com'},{'name':'biying','url':'www.biying'}])
def urls_name(request):
    url = request.param['url']
    name = request.param['name']
    return url,name


def test_name_url(urls_name):
    url,name = urls_name
    print(name,url)

4.参数化:@pytest.mark.parametrize(“login_r”, test_user_data, indirect=True) indeirect=True 是把 login_r当作函数去执行

fixture传二个参数与数据驱动结合

@pytest.mark.parametrize 装饰器接受两个主要参数:

  1. 第一个参数是一个字符串,表示测试函数中需要参数化的参数名称,多个参数之间用逗号分隔。
  2. 第二个参数是一个列表或元组,其中每个元素都是一个参数组合,每个组合也是一个元组或列表,其元素顺序必须与第一个参数中列出的参数顺序相匹配。

直接在测试函数上使用

import pytest

# 假设我们有一个函数,它接受两个参数并返回它们的和
def add(a, b):
    return a + b

# 使用 parametrize 装饰器,指定测试函数的参数和对应的值
@pytest.mark.parametrize("a, b, expected", [
    (1, 2, 3),   # 第一组参数
    (2, 3, 5),   # 第二组参数
    (-1, 1, 0),  # 第三组参数
    (0, 0, 0),   # 第四组参数
])
def test_add(a, b, expected):
    assert add(a, b) == expected

类级别使用它,对整个类的所有测试方法进行参数化

两组数据组合

两组数据参数化,从下往上执行

pytest-fixture

1.Fixture 显示调用

2.Fixture 应用usefixtures

3.autouse=True自动激活

4.可以使用多个fixture

5.可以嵌套fixture


 

55:00

fixture结合parametrize+yaml进行搜索接口 测试

读取url

读取yaml文件,请求access_token,在test里create_tag

import pytest
import requests
import os
import yaml


def _base_data(file_name):
    cur_path = os.path.dirname(os.path.realpath(__file__))
    yaml1 = os.path.join(cur_path, file_name)
    f1 = open(yaml1)  # 打开yaml文件
    data = yaml.load(f1, Loader=yaml.FullLoader)  # 使用load方法加载
    return data

@pytest.fixture(autouse=True)
def get_base_data():
    base_data = _base_data('tag.yml')
    for v in base_data.values():  #读值
        print(v)
        return v


test_user_data2 = [{"tag": {"name": 'mou1moumou'}},{"tag": {"name": 'maimai2'}},{"tag": {"name": 'sl2ience'}}]


@pytest.fixture(autouse=True)
def query_param(request):
    return request.param


@pytest.mark.parametrize("query_param", test_user_data2, indirect=True)
def test_tag(get_base_data, query_param):
    method = get_base_data.get('method')
    url = get_base_data.get('url')
    params = get_base_data.get('param')
    res = requests.request(method=method, url=url, params=params)
    data = res.json()
    access_token = data['access_token']
    tag_params = query_param
    response = requests.post('https://api.weixin.qq.com/cgi-bin/tags/create?access_token='+access_token, json=tag_params)
    assert 'tag' in response.json()

读取测试数据

读取多个测试数据,待解决!
import yaml
import os
import pytest

def _base_data(file_name):
    cur_path = os.path.dirname(os.path.realpath(__file__))
    yaml1 = os.path.join(cur_path, file_name)
    f1 = open(yaml1)  # 打开yaml文件
    data = yaml.load(f1, Loader=yaml.FullLoader)  # 使用load方法加载
    return data


@pytest.fixture(scope="module")
def get_test_data():
    test_data = _base_data('tag_data.yml')
    test_user_data2 = test_data.get('test_user_data2')
    return test_user_data2


@pytest.mark.parametrize("tag_data", get_test_data(), indirect=True)
def test_functionality(tag_data):
    # test_case 是一个字典,包含 'name', 'input', 'expected_output' 等键
    response= requests.get(url='https://api.weixin.qq.com/cgi-bin/token', param={'grant_type': 'client_credential',
                               'appid': 'wx2e193b26f5edc5cf',
                               'secret': '99fe9aa25e42f30fa81c5d83816e4fc7'})
    access_token = response['access_token']
    response_tag = requests.post('https://api.weixin.qq.com/cgi-bin/tags/create?access_token=' + access_token, json= tag_data)
    print(response_tag)

附:fixture工厂模式

私有方法

html测试报告

安装 

pip install pytest-html

 执行

pytest --html=report.html -vs test_yml_func.py

在当前目录生成report.html

Allure 见其他文章

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/869888.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Android RSA 加解密

文章目录 一、RSA简介二、RSA 原理介绍三、RSA 秘钥对生成1. 密钥对生成2. 获取公钥3. 获取私钥 四、PublicKey 和PrivateKey 的保存1. 获取公钥十六进制字符串1. 获取私钥十六进制字符串 五、PublicKey 和 PrivateKey 加载1. 加载公钥2. 加载私钥 六、 RSA加解密1. RSA 支持三…

【STM32】IIC学习笔记

学习IIC 前言一、基础知识GPIO_WriteBit 写入高低电平 二、放代码三、逐行细读总结 前言 最近沉迷手写笔记~ 尝试解读江科大的IIC程序&#xff0c;结合笔记更理解IIC 一、基础知识 GPIO_WriteBit 写入高低电平 二、放代码 这个是江科大的软件IIC的设置部分 #include "s…

{Spring Boot 原理篇} Spring Boot自动装配原理

SpringBootApplication 1&#xff0c;Spring Boot 应用启动&#xff0c;SpringBootApplication标注的类就是启动类&#xff0c;它去实现配置类中的Bean的自动装配 SpringBootApplication public class SpringbootRedis01Application {public static void main(String[] args)…

快速入门了解Ajax

博客主页&#xff1a;音符犹如代码系列专栏&#xff1a;JavaWeb关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ Ajax的初识 意义&#xff1a;AJAX&#xff08;Asynchronous JavaScript and…

计算机实验室排课查询小程序的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;学生管理&#xff0c;教师管理&#xff0c;实验室信息管理&#xff0c;实验室预约管理&#xff0c;取消预约管理&#xff0c;实验课程管理&#xff0c;实验报告管理&#xff0c;报修信息管理&#xff0…

DAY15

数组 冒泡排序 冒泡排序无疑是最为出名的排序算法之一&#xff0c;总共有八大排序 冒泡的代码还是相当简单的&#xff0c;两层循环&#xff0c;外层冒泡轮数&#xff0c;里层依次比较&#xff0c;江湖中人人尽皆知 我们看到嵌套循环&#xff0c;应该马上就可以得到这个算法的…

【微信小程序实战教程】之微信小程序 WXML 语法详解

WXML语法基础 从本章开始&#xff0c;我们就正式进入到了小程序项目开发学习的初级阶段&#xff0c;本章将介绍小程序的界面构成。有过网页开发学习经历的同学都知道&#xff0c;网页开发所使用的技术是HTML、CSS和JS&#xff0c;其中HTML用于描述整个网页的结构&#xff0c;也…

【RaspberryPi】树莓派Matlab/Simulink支持包安装与使用

官网支持与兼容性 Raspberry Pi Support from MATLAB - Hardware Support - MATLAB & Simulink Raspberry Pi Support from Simulink - Hardware Support - MATLAB & Simulink Matlab与树莓派兼容性 Simulink与树莓派兼容性 树莓派Matlab&Simulink RaspberryPi支…

【React】通过实际示例详解评论列表渲染和删除

文章目录 一、引言二、初始状态与状态更新1. 使用useState钩子管理状态2. 评论列表的初始数据 三、列表渲染的实现1. list.map(item > { ... })2. return 语句3. JSX 语法4. 为什么这样设计5. 完整解读 四、列表项的唯一标识1. key 的作用2. key 的用法3. 可以没有 key 吗&a…

【React】详解样式控制:从基础到进阶应用的全面指南

文章目录 一、内联样式1. 什么是内联样式&#xff1f;2. 内联样式的定义3. 基本示例4. 动态内联样式 二、CSS模块1. 什么是CSS模块&#xff1f;2. CSS模块的定义3. 基本示例4. 动态应用样式 三、CSS-in-JS1. 什么是CSS-in-JS&#xff1f;2. styled-components的定义3. 基本示例…

github的Codespaces是什么

目录 github的Codespaces是什么 一、定义与功能 二、特点与优势 三、工作原理 四、使用场景与限制 github的Codespaces是什么 GitHub的Codespaces是一个基于云的即时开发环境,它利用容器技术为开发者提供一个完全配置好的开发环境,以便他们能够直接在浏览器或通过Visua…

【CI/CD】docker + Nginx自动化构建部署

CI/CD是什么 CI/CD 是持续集成&#xff08;Continuous Integration&#xff09;和持续部署&#xff08;Continuous Deployment&#xff09;或持续交付&#xff08;Continuous Delivery&#xff09;的缩写&#xff0c;它们是现代软件开发中用于自动化软件交付过程的实践。 1、…

Spark实时(五):InputSource数据源案例演示

文章目录 InputSource数据源案例演示 一、​​​​​​​File Source 1、读取text文件 2、读取csv文件 3、读取json文件 二、Socket Source 三、Rate Source InputSource数据源案例演示 在Spark2.0版本之后&#xff0c;DataFrame和Dataset可以表示静态有边界的数据&am…

【RT摩拳擦掌】RT600 4路音频同步输入1路TDM输出方案

【RT摩拳擦掌】RT600 4路音频同步输入1路TDM输出方案 一&#xff0c; 文章简介二&#xff0c;硬件平台构建2.1 音频源板2.2 音频收发板2.3 双板硬件连接 三&#xff0c;软件方案与软件实现3.1 方案实现3.2 软件代码实现3.2.1 4路I2S接收3.2.2 I2S DMA pingpong配置3.2.3 音频数…

Spring源码学习笔记之@Async源码

文章目录 一、简介二、异步任务Async的使用方法2.1、第一步、配置类上加EnableAsync注解2.2、第二步、自定义线程池2.2.1、方法一、不配置自定义线程池使用默认线程池2.2.2、方法二、使用AsyncConfigurer指定线程池2.2.3、方法三、使用自定义的线程池Excutor2.2.4、方法四、使用…

算法-----递归~~搜索~~回溯(宏观认识)

目录 1.什么是递归 1.1二叉树的遍历 1.2快速排序 1.3归并排序 2.为什么会用到递归 3.如何理解递归 4.如何写好一个递归 5.什么是搜索 5.1深度&#xff08;dfs&#xff09;优先遍历&优先搜索 5.2宽度&#xff08;bfs&#xff09;优先遍历&优先搜索 6.回溯 1.什…

《0基础》学习Python——第二十三讲__网络爬虫/<6>爬取哔哩哔哩视频

一、在B站上爬取一段视频&#xff08;B站视频有音频和视频两个部分&#xff09; 1、获取URL 注意&#xff1a;很多平台都有反爬取的机制&#xff0c;B站也不例外 首先按下F12找到第一条复制URL 2、UA伪装&#xff0c;下列图片中&#xff08;注意代码书写格式&#xff09; 3、Co…

redis的使用场景和持久化方式

redis的使用场景 热点数据的缓存。热点&#xff1a;频繁读取的数据。限时任务的操作&#xff1a;短信验证码。完成session共享的问题完成分布式锁。 redis的持久化方式 什么是持久化&#xff1a;把内存中的数据存储到磁盘的过程&#xff0c;同时也可以把磁盘中的数据加载到内存…

react开发-配置开发时候@指向SRC目录

这里写目录标题 配置开发时候指向SRC目录VScode编辑器给出提示总体1.配置react的 2.配置Vscode的1.配置react的2,配置VSCode的提示支持 配置开发时候指向SRC目录VScode编辑器给出提示 总体1.配置react的 2.配置Vscode的 1.配置react的 1. 我么需要下载一个webpack的插件 这样…

河南萌新联赛2024第(二)场:南阳理工学院

文章目录 链接 A. 国际旅行Ⅰ题意与思路代码 D.A*BBBB题意与思路代码 F.水灵灵的小学弟题意与思路代码 H.狼狼的备忘录题意与思路代码 I.重生之zbk要拿回属于他的一切题意与思路代码 J.这是签到题意与思路代码总结 链接 链接 A. 国际旅行Ⅰ 题意与思路 这是一个签到题&…