一、摘要

本博文将介绍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】的更多相关文章

  1. python selenium 使用unittest 示例

    python selenium 使用unittest 示例 并等待某个元素示例 from selenium.webdriver.support.ui import WebDriverWait from ...

  2. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

    1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...

  3. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

  4. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)

    前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请 ...

  5. Python 数据驱动 unittest + ddt

    一数据驱动测试的含义: 在百度百科上的解释是: 数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子.利用黑盒测试法进行动态测试时,需要测试软件产 ...

  6. python+selenium九:ddt数据驱动

    第一种,测试数据放在Excel里面 test_Login: import unittestimport timeimport ddtimport osfrom selenium import webd ...

  7. Python&Selenium 数据驱动【unittest+ddt+json】

    一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用json文件作为数据文件作为测试输入,最后生成html测试报告 二.json文件 [ ...

  8. Python&Selenium 数据驱动【unittest+ddt】

    一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt实现数据驱动 二.测试代码 # encoding = utf-8 ""& ...

  9. Python&Selenium 数据驱动测试【unittest+ddt+xml】

    一.摘要 本博文将介绍Python和Selenium做自动化测试时,基于unittest框架,借助ddt模块,使用xml文件作为测试输入. 二.xml文件 保存路径:D:\\Programs\\Pyt ...

随机推荐

  1. 解决Linux:Too many levels of symbolic links

    Too many levels of symbolic links 解决:创建链接时使用绝对路径

  2. python之pandas学习笔记-初识pandas

    初识pandas python最擅长的就是数据处理,而pandas则是python用于数据分析的最常用工具之一,所以学python一定要学pandas库的使用. pandas为python提供了高性能 ...

  3. C#操作Memcached帮助类

    在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装: 服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl 接下来开始写程序, ...

  4. Docker下mysql容器开启binlog日志(保留7天)

    现有需求开启用Docker容器启动的mysql数据库的binlog,以作为 日志记录 和 数据恢复,我们了解了MySQL的binlog日志的开启方式以及binlog日志的一些原理和常用操作,我们知道, ...

  5. [转帖]Linux cpufreq 机制了解

    Linux cpufreq 机制了解 https://www.cnblogs.com/armlinux/archive/2011/11/12/2396780.html 引用文章链接: http://w ...

  6. Ckeditor5显示css样式

    Ckeditor5在编辑模式是通过js加载样式的,但是在显示时没有提供,官方提供了两种方式来实现. https://ckeditor.com/docs/ckeditor5/latest/builds/ ...

  7. python基础(十)--函数进阶

    嵌套函数 >>> graphic = '三角形' >>> def chang(): graphic = '正方形' def chang1(): #内部嵌套的函数命名 ...

  8. Nginx安装启动过程报错libpcre.so.1 cannot open shared object file: No such file or directory

    具体报错信息如下: nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: ...

  9. hdu 1671 复习字典树

    #include<cstdio> #include<iostream> #include<string> #include<cstdlib> #defi ...

  10. Redis 的订阅发布(PUB/SUB)示例

    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1"); ISubscriber sub ...