栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

记录Pytest自动化测试脚本中的小点滴

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记录Pytest自动化测试脚本中的小点滴

目录

1.背景

2.问题合集

1)Pytest+allure自动化框架

2)Pytest中几个好用插件

3)判断一个字典在另一个字典中

4)retrying的使用

5)yaml中的变量替换

6)随机生成中文字符

7)allure


1.背景

最近都在写Python自动化测试代码,Java暂时放一放。没办法,挣钱吃饭只能听“老板”的,打工人都懂~~很久没写Python所以,遇到好些小问题。

2.问题合集

1)Pytest+allure自动化框架

接口自动化框架(Pytest+request+Allure)_软件测试官的博客-CSDN博客_接口自动化框架

Pytest接口自动化框架 - Linazhu - 博客园

Python+Pytest+Allure+Git+Jenkins接口自动化框架 - wuwei丶 - 博客园

2)Pytest中几个好用插件

python有哪些插件_Python测试工具 | 8 个很棒的pytest插件_weixin_39846612的博客-CSDN博客

3)判断一个字典在另一个字典中
def is_contained(self,exp,act):
	expect=set(exp.items())
	actual=set(act.items())
	if expect.issubset(actual):
		return True
	return False

上面这个方法仅适用于有一层字典的情况。即,判断期望的exp是否在act中

exp = {
    "k1": v1,
    "k2": v2
}

act = {
    "k1": v1,
    "k2": v2,
    "k3": v3
}

4)retrying的使用

一个服务的Pod正常就绪以后,还没有立刻对外提供服务,所以在访问服务的RESTFul接口时,并不会立刻访问通过。直接通过requests访问接口的时候,会出现类似这样的错误:

HTTPSConnectionPool(host=‘xxx.xxx.xxx.xxx’, port=xxx): Max retries exceeded with url: / (Caused by ProxyError(‘Cannot connect to proxy.’, NewConnectionError(’: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。’)))

这时需要重试。

# 最大重试次数和最大延迟时间30s
@retry(stop_max_attempt_number=5, stop_max_delay=30000)
def _foo(url):
    response = requests.get(url)
    assert response.status_code == 200, f"访问{url}失败"
    return response

def foo(url):
    try:
        response = _foo(url)
        result = response.text
    except Exception as e:
        allure.attach(f"{e}")
    return result

Python异常处理模块——retrying - 知乎

 Python3,异常进阶写法之retrying。_Carl_奕然的博客-CSDN博客_python3 retrying

: Failed to establish a new connection: - java渣渣 - 博客园">Caused by NewConnectionError(': Failed to establish a new connection: - java渣渣 - 博客园

5)yaml中的变量替换

虽然最终没有使用这里的种种方案,但不妨提供给大家参考。

python替换yaml变量的多种方法以及踩坑_小生测试的博客-CSDN博客_python替换yaml

6)随机生成中文字符

https://www.jb51.net/article/128987.htm

7)allure

参考高手--allure清空上一次运行的记录(--clean-alluredir) - 清风吹拂啊狂风肆虐 - 博客园

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/861391.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号