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

2024-04-27 21:12

本文主要是介绍pytest-asyncio:协程异步测试案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介: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)

运行结果:

图片

图片

这篇关于pytest-asyncio:协程异步测试案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/941545

相关文章

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Java 中实现异步的多种方式

《Java中实现异步的多种方式》文章介绍了Java中实现异步处理的几种常见方式,每种方式都有其特点和适用场景,通过选择合适的异步处理方式,可以提高程序的性能和可维护性,感兴趣的朋友一起看看吧... 目录1. 线程池(ExecutorService)2. CompletableFuture3. ForkJoi

Python异步编程中asyncio.gather的并发控制详解

《Python异步编程中asyncio.gather的并发控制详解》在Python异步编程生态中,asyncio.gather是并发任务调度的核心工具,本文将通过实际场景和代码示例,展示如何结合信号量... 目录一、asyncio.gather的原始行为解析二、信号量控制法:给并发装上"节流阀"三、进阶控制

MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固 通俗易懂版)

《MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固通俗易懂版)》本文主要讲解了MySQL中的多表查询,包括子查询、笛卡尔积、自连接、多表查询的实现方法以及多列子查询等,通过实际例子和操... 目录复合查询1. 回顾查询基本操作group by 分组having1. 显示部门号为10的部门名,员

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring