【用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

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法