Python&Selenium 数据驱动【unittest+ddt+json+HTMLTestRunner】
一、摘要
本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用json文件作为数据文件作为测试输入,最后借助著名的HTMLTestRunner.py生成html测试报告
二、测试代码
# encoding = utf-8
"""
__title__ = ''
__author__ = 'davieyang'
__mtime__ = '2018/4/21'
"""
from selenium import webdriver
import unittest
import time
import logging
import traceback
import ddt
from selenium.common.exceptions import NoSuchElementException # 初始化日志对象
logging.basicConfig(
# 日志级别
level=logging.INFO,
# 时间、代码所在文件名、代码行号、日志级别名字、日志信息
format='%(asctime)s %(filename)s[line: %(lineno)d] %(levelname)s %(message)s',
# 打印日志的时间
datefmt='%a, %d %b %Y %H:%M:%S',
# 日志文件存放的目录及日志文件名
filename='F:\\DataDriven\\TestResults\TestResults.TestResults',
# 打开日志的方式
filemode='w'
) @ddt.ddt
class DataDrivenTestByDDTHTR(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="F:\\automation\\webdriver\\chromedriver.exe") @ddt.file_data("F:\\DataDriven\\testData\\test_data_list.json")
def test_dataDrivenHTRByFile(self, value):
url = "http://www.baidu.com"
self.driver.get(url)
self.driver.maximize_window()
print(value)
# 将从.json文件中读取出的数据用“||”分割成测试数据和期望的数据
testdata, execptdata = tuple(value.strip().split("||"))
# 设置隐式等待时间
self.driver.implicitly_wait(10)
try:
self.driver.find_element_by_id("kw").send_keys(testdata)
self.driver.find_element_by_id("su").click()
time.sleep(3)
# 断言期望结果是否出现在页面中
self.assertTrue(execptdata in self.driver.page_source)
except NoSuchElementException as e:
logging.error(u"查找的页面元素不存在,异常堆栈信息为:" + str(traceback.format_exc()))
except AssertionError as e:
logging.info(u"搜索 '%s',期望 '%s' ,失败" % (testdata, execptdata))
except Exception as e:
logging.error(u"未知错误,错误信息:" + str(traceback.format_exc()))
else:
logging.info(u"搜索 '%s',期望 '%s' ,通过" % (testdata, execptdata)) def tearDown(self):
self.driver.quit() if __name__ == '__main__':
unittest.main()
三、执行测试
import unittest
from Run.HTMLTestRunner import HTMLTestRunner
from DataDrivenTest.DataDrivenTestByDDTHTR import DataDrivenTestByDDTHTR
import time suit = unittest.TestSuite()
suit.addTest(unittest.makeSuite(DataDrivenTestByDDTHTR))
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
filename = "F:\\DataDriven\\TestResults\\Results-" + now + "result.html"
print(filename)
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp, title='Result', description='TestReport')
runner.run(suit)
print("test_DrivenByFile finished")
Python&Selenium 数据驱动【unittest+ddt+json+HTMLTestRunner】的更多相关文章
- python selenium 使用unittest 示例
python selenium 使用unittest 示例 并等待某个元素示例 from selenium.webdriver.support.ui import WebDriverWait from ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告
1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)
可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)
前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请 ...
- Python 数据驱动 unittest + ddt
一数据驱动测试的含义: 在百度百科上的解释是: 数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子.利用黑盒测试法进行动态测试时,需要测试软件产 ...
- python+selenium九:ddt数据驱动
第一种,测试数据放在Excel里面 test_Login: import unittestimport timeimport ddtimport osfrom selenium import webd ...
- Python&Selenium 数据驱动【unittest+ddt+json】
一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用json文件作为数据文件作为测试输入,最后生成html测试报告 二.json文件 [ ...
- Python&Selenium 数据驱动【unittest+ddt】
一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt实现数据驱动 二.测试代码 # encoding = utf-8 ""& ...
- Python&Selenium 数据驱动测试【unittest+ddt+xml】
一.摘要 本博文将介绍Python和Selenium做自动化测试时,基于unittest框架,借助ddt模块,使用xml文件作为测试输入. 二.xml文件 保存路径:D:\\Programs\\Pyt ...
随机推荐
- 转换函数conversion function
类转换分为两个角度 转换自身为其他类型 把其他类型转换为自身 Example: 这里我们可以将b转换为class xxx 的类型(方式2),也可以将me转换为double,然后再讲结果转换为doubl ...
- 小结Fragment与FragmentPagerAdapter的生命周期及其关系
本博客部分内容是来自http://blog.csdn.net/dreamzml/article/details/9951577 FragmentPagerAdapter FragmentPagerAd ...
- android 自动化测试 ---python wrapper(python 包装)
关于有道云笔记复制的东西不能直接copy到博客园,可以选择使用txt文件做个媒介 1.appium 2.monkeyrunner 3.uiautomator2 前面两种种方式都要加载androidsd ...
- Sumitomo Mitsui Trust Bank Programming Contest 2019 Task F. Interval Running
Link. There is a nice approach to this problem that involves some physical insight. In the following ...
- 关于typescript中的枚举你需要知道这些
数字枚举 数字枚举,即枚举里所有属性的值都是数字类型,先看这段代码: enum Colors { Red, Blue, Yellow } console.log(Colors.Red) console ...
- 坦克大战--Java类型
写在前面 Java编译器下载教程(真的良心):https://blog.csdn.net/Haidaiya/article/details/81230636 本项目为本人独自制作,请各位尊 ...
- 汉诺塔递推HDU2064
题意: 移动木头盘不能a到c,必须a到b到c. 问你移动次数. 假设将n层塔从A经B挪到C需要f[n]步.那么具体的移动过程可以这样看:将上面n-1层从A经B挪到C需要f[n-1]步,再将第n层从A挪 ...
- [游戏复刻] Super Mario Brothers(1985. Famicom)
10/20 第一版,导入了地图,设置了碰撞块
- S03_CH03_AXI_DMA_OV7725摄像头采集系统
S03_CH03_AXI_DMA_OV7725摄像头采集系统 3.1概述 本课程讲解如何搭建基于DMA的图形系统,方案原理如下. 摄像头采样图像数据后通过DMA送入到DDR,在PS部分产生DMA接收中 ...
- python 定时爬取内容并发送报告到指定邮箱
import requests import smtplib import schedule import time from bs4 import BeautifulSoup from email. ...