pytest-asyncio:协程异步测试案例

ops/2024/10/11 13:25:30/

简介pytest-asyncio是一个pytest插件。它便于测试使用异步库的代码。具体来说,pytest-asyncio提供了对作为测试函数的协同程序的支持。这允许用户在测试中等待代码。

历史攻略:

asyncio并发访问websocket

Python:协程 - 快速创建三步骤

Python:获取协程返回值的四种方式

Python:多进程,多线程,协程基础案例

Python:aiomultiprocess实现协程与多进程的强强联合

安装:

pip install pytest-asyncio

使用案例:aysncio_demo.py

# -*- coding: utf-8 -*-
# time: 2024/4/5 12:06
# file: asyncio_demo.py
# 公众号: 玩转测试开发import asyncio
import datetimeasync def do_somethings(user):# 1、定义协程函数print(f"{user} is do_somethings. the time is {datetime.datetime.now()}")await asyncio.sleep(0.1)return user + " is finish do_somethings."async def do_another(user):print(f"{user} is do_another. the time is {datetime.datetime.now()}")await asyncio.sleep(0.2)return user + " is finish do_another."async def do_others(user):print(f"{user} is do_others. the time is {datetime.datetime.now()}")return user + " is finish do_others."if __name__ == '__main__':# 3、加入事件循环。tasks = []for i in range(3):tasks.append(do_somethings("tom"))tasks.append(do_another("ken"))tasks.append(do_others("lily"))asyncio.run(asyncio.wait(tasks))

test_demo.py

# -*- coding: utf-8 -*-
# time: 2024/3/31 10:34
# file: test_demo.py
# 公众号: 玩转测试开发
import sys
import pytest
from lib.asyncio_demo import do_somethings, do_another, do_others
from logger import log@pytest.mark.asyncio
async def test_do_somethings():user = "tom"res = await do_somethings(user)log.info(f"{res}")pytest.assume(user + " is finish do_somethings." in res)@pytest.mark.asyncio
async def test_do_another():user = "ken"res = await do_another(user)log.info(f"{res}")pytest.assume(user + " is finish do_another." in res)@pytest.mark.asyncio
async def test_do_others():user = "lily"res = await do_others(user)log.info(f"{res}")# 此次故意放一个错误试试。pytest.assume(user + " is finish do_others." not in res)

运行结果:

图片

图片


http://www.ppmy.cn/ops/18624.html

相关文章

机器学习-保险花销预测笔记+代码

读取数据 import numpy as np import pandas as pddatapd.read_csv(rD:\人工智能\python视频\机器学习\5--机器学习-线性回归\5--Lasso回归_Ridge回归_多项式回归\insurance.csv,sep,) data.head(n6) EDA 数据探索 import matplotlib.pyplot as plt %matplotlib inlineplt.hi…

django忽略migrate

django migrate迁移时会依次执行四件事: 1、迁移判定,将你的项目中所有未迁移的变动文件进行迁移(django会去查询django_migrations表判断你是否有新的迁移文件变动,若有新的迁移文件,则将变动加到django_migrations表…

Visual studio2022+QT的创建

Visual studio2022QT的创建 1.首先安装Visual studio 2.可以直接在visual studio中安装qt插件,如下所示: 扩展->管理扩展->搜索qt Vistal Studio Tools 3.接下来的就是重点,安装完了这个插件之后,也是需要安装qt的程序的…

Go语言 Interface(接口)

基本介绍 Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。接口可以让我们将不同的类型绑定到一组公共的方法上,从而实现多态和灵活的设计。Go 语言中的接口是…

Feign负载均衡

Feign负载均衡 概念总结 工程构建Feign通过接口的方法调用Rest服务(之前是Ribbon——RestTemplate) 概念 官网解释: http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign Feign是一个声明式WebService客户端。使用Feign能让…

C# WinForm —— 09 标签、文本框、按钮控件

标签 Label 一般显示不能编辑的文本或图像 常用属性、事件: 属性用途(Name)标签对象的ID,在代码里引用标签的时候会用到,一般以 lbl 开头Text设置或获取 界面上显示的 文本信息Image显示图像ImageList图像集,通常和 ListView ToolStrip Tre…

ITMS-90426: Invalid Swift Support

原文 Please correct the following issues and upload a new binary to App Store Connect. ITMS-90426: Invalid Swift Support - The SwiftSupport folder is missing. Rebuild your app using the current public (GM) version of Xcode and resubmit it. 解决方式 ITMS-…

git提交常用

git config --global user.name "你的名字或昵称" git config --global user.email "你的邮箱" 第一次上传到码云 1.找到要提交到码云的文件夹 右击打开Git Bash Here 2.用命令行创建本地仓库 git init 3.将待全部文件放入缓冲区 git add . 4.提交缓…