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

embedded/2024/9/24 12:17:51/

简介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/embedded/19268.html

相关文章

C 练习实例25

C 练习实例25 题目&#xff1a; 求12!3!...20!的和。 程序分析&#xff1a; 此程序只是把累加变成了累乘。 实例 #include <stdio.h>int main() {int i;long double sum,mix;sum0,mix1;for(i1;i<20;i){mixmix*i;sumsummix;} printf("%Lf\n",sum); }以…

数据中台、数据仓库、数据治理与主数据的定位与差异

在数字化时代&#xff0c;大数据已经成为企业运营和决策的重要资产。为了更好地管理和利用这些数据&#xff0c;数据中台、数据仓库、数据治理和主数据管理等概念应运而生。这些概念听起来可能有些抽象&#xff0c;但实际上它们与我们的日常生活息息相关。下面&#xff0c;我将…

神经网络进阶学习文章(一)

1.讲解YOLO有关知识 深入浅出Yolo系列之Yolov5核心基础知识完整讲解 - 知乎 (zhihu.com) 2.目标检测算法综述 目标检测算法综述 - 知乎 (zhihu.com) 3.TensorFlow详解&#xff0c;当然现在用的最多的是Pytorch框架了 谷歌大神带你十分钟看懂TensorFlow - 知乎 (zhihu.co…

【RocketMQ知识点总结-1】

文章目录 RocketMQ介绍RocketMQ架构&#xff1a;NameServer:BrokerProducerTopic&#xff08;主题&#xff09;&#xff1a;Queue&#xff08;队列&#xff09;&#xff1a;Message&#xff08;消息&#xff09;&#xff1a; RocketMQ的工作流程RocketMQ的使用场景异步消息传递…

【003_音频开发_基础篇_Linux进程通信(20种你了解几种?)】

003_音频开发_基础篇_Linux进程通信&#xff08;20种你了解几种&#xff1f;) 文章目录 003_音频开发_基础篇_Linux进程通信&#xff08;20种你了解几种&#xff1f;)创作背景Linux 进程通信类型fork() 函数fork() 输出 2 次fork() 输出 8 次fork() 返回值fork() 创建子进程 方…

我们自己的芯片指令集架构——龙芯架构简介

CPU指令集架构&#xff08;ISA, Instruction Set Architecture&#xff09; CPU指令集架构是处理器硬件与软件之间的接口规范&#xff0c;它定义了一组基本指令&#xff0c;以及这些指令的操作格式、编码方式、寻址模式、寄存器组织、中断机制、异常处理等各个方面。ISA是计算…

阿里云物联网平台 | 透传与ICA标准数据格式(Alink JSON)| 定值SDK非动态注册与动态注册 | SOC+4G模组移植方案

文章目录 一、透传与ICA标准数据格式&#xff08;Alink JSON&#xff09;二、定值SDK选非动态注册还是动态注册三、SOC4G模组移植方案 一、透传与ICA标准数据格式&#xff08;Alink JSON&#xff09; 透传和ICA标准数据格式&#xff08;Alink JSON&#xff09;是物联网设备与阿…

朴素贝叶斯算法分类

def loadDataSet():postingList[[my, dog, has, flea, problems, help, please], #切分的词条[maybe, not, take, him, to, dog, park, stupid],[my, dalmation, is, so, cute, I, love, him],[stop, posting, stupid, worthless, garbage],[mr, licks, ate, my, steak, …