【用ddt思想重构项目】Selenium使用xlrd模块读取excel文件、使用pytest参数化实现ddt

本文主要是介绍【用ddt思想重构项目】Selenium使用xlrd模块读取excel文件、使用pytest参数化实现ddt,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

一直想学习自动化测试,但是都没行动,业余时间学习零零碎碎并记录20210421。

8、用ddt思想重构项目

  • Selenium读取CSV文件
  • Selenium读取XML文件
  • Selenium读取json文件
  • Selenium 读取excel文件
  • Selenium读取ini配置文件
  • Selenium读取数据库数据
  • Selenium参数化测试
  • Selenium ddt
  • 使用ddt思想重构项目

Selenium使用xlrd模块读取ecel文件

1、调用xlrd:import xlrd
2、使用xlrd模块调用excel表格
3、结合pytest参数化格式处理方式来实现DDT

实例

1、首先创建个excel表格:test.xlsx

2、安装xlrd模块(MAC):pip3 install xlrd

或者pycharm里选中xlrd,再选择option+O然后选择安装,授权即可

3、openpyxl包的安装,也是用快捷键装

4、代码test_excel.py

import pytest
import xlrd
from openpyxl.workbook import Workbookdef get_data():filename = 'test.xlsx'wb = xlrd.open_workbook(filename)sheet = wb.sheet_by_index(0)rows = sheet.nrowscols = sheet.ncolslst = []for row in range(rows):for col in range(cols):cell_data = sheet.cell_value(row, col)lst.append(cell_data)print(lst)return lst@pytest.mark.parametrize('name', get_data())
def test1(name):print(name)if __name__ == '__main__':pytest.main(['-sv', 'test_excel.py'])

5、运行结果:

========================================================= ERRORS ==========================================================
_____________________________________________ ERROR collecting test_excel.py ______________________________________________
test_excel.py:19: in <module>@pytest.mark.parametrize('name', get_data())
test_excel.py:7: in get_datawb = xlrd.open_workbook(filename)
/Library/Python/3.7/site-packages/xlrd/__init__.py:170: in open_workbookraise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
E   xlrd.biffh.XLRDError: Excel xlsx file; not supported
================================================= short test summary info =================================================
ERROR test_excel.py - xlrd.biffh.XLRDError: Excel xlsx file; not supported
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
==================================================== 1 error in 0.42s =====================================================
zhengxiaofang@zhengxiangdeMBP ddt % 

报错,需要再安装一个:pip3 install pyexcel-xls

~ % pip3 install pyexcel-xls
Defaulting to user installation because normal site-packages is not writeable
Collecting pyexcel-xlsDownloading pyexcel_xls-0.6.2-py2.py3-none-any.whl (11 kB)
Collecting pyexcel-io>=0.6.2Downloading pyexcel_io-0.6.4-py2.py3-none-any.whl (44 kB)|████████████████████████████████| 44 kB 5.9 kB/s
Collecting xlrd<2Downloading xlrd-1.2.0-py2.py3-none-any.whl (103 kB)|████████████████████████████████| 103 kB 3.7 kB/s
Collecting xlwtDownloading xlwt-1.3.0-py2.py3-none-any.whl (99 kB)|████████████████████████████████| 99 kB 6.3 kB/s
Collecting lml>=0.0.4Downloading lml-0.1.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: lml, xlwt, xlrd, pyexcel-io, pyexcel-xls
Successfully installed lml-0.1.0 pyexcel-io-0.6.4 pyexcel-xls-0.6.2 xlrd-1.2.0 xlwt-1.3.0

再运行看下,正常了

 ddt % pytest -sv test_excel.py
=================================================== test session starts ===================================================
platform darwin -- Python 3.7.3, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- /Library/Developer/CommandLineTools/usr/bin/python3
cachedir: .pytest_cache
rootdir: /Users/ff/PycharmProjects_py3/Selenium_project/testcases/ddt
plugins: dependency-0.5.1, allure-pytest-2.8.40
collecting ... ['test1']
['test1', 'test10']
['test1', 'test10', 'test20']
['test1', 'test10', 'test20', 'test2']
['test1', 'test10', 'test20', 'test2', 'test11']
['test1', 'test10', 'test20', 'test2', 'test11', 'test21']
['test1', 'test10', 'test20', 'test2', 'test11', 'test21', 'test3']
['test1', 'test10', 'test20', 'test2', 'test11', 'test21', 'test3', 'test12']
['test1', 'test10', 'test20', 'test2', 'test11', 'test21', 'test3', 'test12', 'test22']
['test1', 'test10', 'test20', 'test2', 'test11', 'test21', 'test3', 'test12', 'test22', 'test4']
['test1', 'test10', 'test20', 'test2', 'test11', 'test21', 'test3', 'test12', 'test22', 'test4', 'test13']
['test1', 'test10', 'test20', 'test2', 'test11', 'test21', 'test3', 'test12', 'test22', 'test4', 'test13', 'test23']
collected 12 items                                                                                                        test_excel.py::test1[test1] test1
PASSED
test_excel.py::test1[test10] test10
PASSED
test_excel.py::test1[test20] test20
PASSED
test_excel.py::test1[test2] test2
PASSED
test_excel.py::test1[test11] test11
PASSED
test_excel.py::test1[test21] test21
PASSED
test_excel.py::test1[test3] test3
PASSED
test_excel.py::test1[test12] test12
PASSED
test_excel.py::test1[test22] test22
PASSED
test_excel.py::test1[test4] test4
PASSED
test_excel.py::test1[test13] test13
PASSED
test_excel.py::test1[test23] test23
PASSED=================================================== 12 passed in 0.32s ====================================================ddt % 

“永不放弃,总有希望在前面等待!”送给自己,也送给正在阅读文章的博友们~

这篇关于【用ddt思想重构项目】Selenium使用xlrd模块读取excel文件、使用pytest参数化实现ddt的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3