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

相关文章

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测

pytorch自动求梯度autograd的实现

《pytorch自动求梯度autograd的实现》autograd是一个自动微分引擎,它可以自动计算张量的梯度,本文主要介绍了pytorch自动求梯度autograd的实现,具有一定的参考价值,感兴趣... autograd是pytorch构建神经网络的核心。在 PyTorch 中,结合以下代码例子,当你

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的