数据驱动json文件的方式

test_data_list.json:

[

"邓肯||蒂姆",

"乔丹||迈克尔",

"库里||斯蒂芬",

"杜兰特||凯文",

"詹姆斯||勒布朗"

]

ReportTemplate.py:

#encoding=utf-8

def htmlTemplate(trData):

htmlStr = u'''<!DOCTYPE HTML>

<html>

<head>

<title>单元测试报告</title>

<style>

body {

width: 80%; /*整个body区域占浏览器的宽度百分比*/

margin: 40px auto; /*整个body区域相对浏览器窗口摆放位置(左右,上下)*/

font-weight: bold; /*整个body区域的字体加粗*/

font-family: 'trebuchet MS', 'Lucida sans', SimSun; /*表格中文字的字体类型*/

font-size: 18px; /*表格中文字字体大小*/

color: #000; /*整个body区域字体的颜色*/

}

table {

*border-collapse: collapse; /*合并表格边框*/

border-spacing: 0;  /*表格的边框宽度*/

width: 100%;     /*整个表格相对父元素的宽度*/

}

.tableStyle {

/*border: solid #ggg 1px;*/

border-style: outset; /*整个表格外边框样式*/

border-width: 2px; /*整个表格外边框宽度*/

/*border: 2px;*/

border-color: blue; /*整个表格外边框颜色*/

}

.tableStyle tr:hover {

background: rgb(173,216,230); /*鼠标滑过一行时,动态显示的颜色146,208,80*/

}

.tableStyle td,.tableStyle th {

border-left: solid 1px rgb(146,208,80); /*表格的竖线颜色*/

border-top: 1px solid rgb(146,208,80);  /*表格的横线颜色 */

padding: 15px;                       /*表格内边框尺寸*/

text-align: center;                   /*表格内容显示位置*/

}

.tableStyle th {

padding: 15px;        /*表格标题栏,字体的尺寸*/

background-color: rgb(146,208,80); /*表格标题栏背景颜色*/

/*表格标题栏设置渐变颜色*/

background-image: -webkit-gradient(linear, left top, left bottom, from(#92D050), to(#A2D668));

/*rgb(146,208,80)*/

}

</style>

</head>

<body>

<center><h1>测试报告</h1></center><br />

<table class="tableStyle">

<thead>

<tr>

<th>Search Words</th>

<th>Assert Words</th>

<th>Start Time</th>

<th>Waste Time(s)</th>

<th>Status</th>

</tr>

</thead>'''

endStr = u'''

</table>

</body>

</html>'''

# 拼接完整的测试报告HTML页面代码

html = htmlStr + trData + endStr

print html

# 生成.html文件

with open(u"d:\\test\\testTemplate.html", "w") as fp:

fp.write(html.encode("gbk"))

data_drivern_by_file.py:

# encoding=utf-8

from selenium import webdriver

import unittest, time

import logging, traceback

import ddt

from ReportTemplate import htmlTemplate

from selenium.common.exceptions import NoSuchElementException

#如果有no json的报错信息,请将json文件存储为utf-8,with Bom

# 初始化日志对象

logging.basicConfig(

# 日志级别

level = logging.INFO,

# 日志格式

# 时间、代码所在文件名、代码行号、日志级别名字、日志信息

format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

# 打印日志的时间

datefmt = '%a, %Y-%m-%d %H:%M:%S',

# 日志文件存放的目录(目录必须存在)及日志文件名

filename = 'd:/report.log',#’d:\\report.log’也可以

# 打开日志文件的方式

filemode = 'w'

)

@ddt.ddt

class TestDemo(unittest.TestCase):

@classmethod

def setUpClass(cls):

# 整个测试过程只被调用一次

TestDemo.trStr = ""

def setUp(self):

self.driver=webdriver.Firefox(executable_path="c:\\geckodriver")

status = None # 用于存放测试结果状态,失败'fail',成功'pass'

flag = 0 # 数据驱动测试结果的标志,失败置0,成功置1

@ddt.file_data("test_data_list.json")

def test_dataDrivenByFile(self, value):

# 决定测试报告中状态单元格中内容的颜色

flagDict = {0: 'red', 1: '#00AC4E'}

url = "http://www.baidu.com"

# 访问百度首页

self.driver.get(url)

# 将浏览器窗口最大化

self.driver.maximize_window()

print value

# 将从.json文件中读取出的数据用“||”进行分隔成测试数据

# 和期望数据

testdata, expectdata = tuple(value.strip().split("||"))

# 设置隐式等待时间为10秒

self.driver.implicitly_wait(10)

try:

# 获取当前的时间戳,用于后面计算查询耗时用

start = time.time()

# 获取当前时间的字符串,表示测试开始时间

startTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

# 找到搜索输入框,并输入测试数据

self.driver.find_element_by_id("kw").send_keys(testdata)

# 找到搜索按钮,并点击

self.driver.find_element_by_id("su").click()

time.sleep(3)

# 断言期望结果是否出现在页面源代码中

self.assertTrue(expectdata in self.driver.page_source)

except NoSuchElementException, e:

logging.error(u"查找的页面元素不存在,异常堆栈信息:" \

+ str(traceback.format_exc()))

status = 'fail'

flag = 0

except AssertionError, e:

logging.info(u"搜索“%s”,期望“%s”,失败" %(testdata, expectdata))

status = 'fail'

flag = 0

except Exception, e:

logging.error(u"未知错误,错误信息:" + str(traceback.format_exc()))

status = 'fail'

flag = 0

else:

logging.info(u"搜索“%s”,期望“%s”通过" %(testdata, expectdata))

status = 'pass'

flag = 1

# 计算耗时,从将测试数据输入到输入框中到断言期望结果之间所耗时

wasteTime = time.time() - start - 3 # 减去强制等待的3秒

# 每一组数据测试结束后,都将其测试结果信息插入表格行

# 的HTML代码中,并将这些行HTML代码拼接到变量trStr变量中,

# 等所有测试数据都被测试结束后,传入htmlTemplate()函数中

# 生成完整测试报告的HTML代码

TestDemo.trStr += u'''

#这段儿会被多次拼接,每搜索一次就会把模板字符串后边的字符拼接上

<tr>

<td>%s</td>

<td>%s</td>

<td>%s</td>

<td>%.2f</td>

<td style="color:%s">%s</td>

</tr><br />''' % (testdata, expectdata,startTime,  wasteTime, flagDict[flag], status)

def tearDown(self):

self.driver.quit()

@classmethod

def tearDownClass(cls):

# 写自定义的html测试报告

# 整个测试过程只被调用一次

htmlTemplate(TestDemo.trStr)

if __name__ == '__main__':

unittest.main()

结果:

D:\test>python test.py
邓肯||蒂姆
testdata,expectdata: 邓肯 蒂姆
.乔丹||迈克尔
testdata,expectdata: 乔丹 迈克尔
.库里||斯蒂芬
testdata,expectdata: 库里 斯蒂芬
.杜兰特||凯文
testdata,expectdata: 杜兰特 凯文
.詹姆斯||勒布朗
testdata,expectdata: 詹姆斯 勒布朗
.<!DOCTYPE HTML>
   <html>
   <head>
   <title>单元测试报告</title>
   <style>
   body{
        width:80%;/*整个body区域占浏览器的宽度百分比*/
        margin:40px auto;/*整个body区域相对浏览器窗口摆放位置(左右,上下)*/
        font-weight:bold;/*整个body区域的字体加粗*/
        font-family:'trebuchet MS','Lucida sans',SimSun;/*表格中文字的字体类型*/
        font-size:18px;/*表格中文字字体大小*/
        color:#000;/*整个body区域字体的颜色*/
   }
   table{
        *border-collapse:collapse;/*合并表格边框*/
        border-spacing:0;/*表格的边框宽度*/
        width:100%;
   }
   .tableStyle{
        /*border:solid #ggg 1px;*/
        border-style:outset;/*整个表格外边框样式*/
        border-width:2px;/*整个表格外边框宽度*/
        /*border:2px*/
        border-color:blue;/*整个表格外边框颜色*/
   }
   .tableStyle tr:hover{
        background:rgb(173,216,230);/*鼠标滑过一行时,动态显示的颜色*/
   }

.tableStyle td,.tableStyle th{
        border-left:solid 1px rgb(146,208,80);/*表格的竖线颜色*/
        border-top:1px solid rgb(146,208,80);/*表格的横线颜色*/
        padding:15px;/*表格内边框尺寸*/
        text-align:center;/*表格内容显示位置*/
   }
   .tableStyle th{
        padding:15px;/*表格标题栏,字体的尺寸*/
        background-color:rgb(146,208,80);/*表格标题栏背景颜色*/
        /*表格标题栏设置渐变颜色*/
       background-image:-webkit-gradient(linear,left top,left bottom,from(#92D050) to(#A2D668));/*rgb(14,208,80)*/
   }
   </style>
   </head>
   <body>
        <center><h1>测试报告</h1></center><br/>
        <table class='tableStyle'>
            <thead>
            <tr>
            <th>Search Words</th>
            <th>Assert Words</th>
            <th>Start Time</th>
            <th>Waste Time(s)</th>
            <th>Status</th>
            </tr>
            </thead>

<tr>
            <td>邓肯</td>

<td>蒂姆</td>

<td>2018-06-27 21:38:14</td>

<td>0.58</td>

<td style="color:#00AC4E">pass</td>

</tr><br/>

<tr>
            <td>乔丹</td>

<td>迈克尔</td>

<td>2018-06-27 21:38:29</td>

<td>0.53</td>

<td style="color:#00AC4E">pass</td>

</tr><br/>

<tr>
            <td>库里</td>

<td>斯蒂芬</td>

<td>2018-06-27 21:38:43</td>

<td>0.53</td>

<td style="color:#00AC4E">pass</td>

</tr><br/>

<tr>
            <td>杜兰特</td>

<td>凯文</td>

<td>2018-06-27 21:38:59</td>

<td>0.51</td>

<td style="color:#00AC4E">pass</td>

</tr><br/>

<tr>
            <td>詹姆斯</td>

<td>勒布朗</td>

<td>2018-06-27 21:39:13</td>

<td>0.56</td>

<td style="color:#00AC4E">pass</td>

</tr><br/>
        </table>
   </body>
   </html>

----------------------------------------------------------------------
Ran 5 tests in 74.468s

OK

html报告:

report0627.log:

Wed,2018-06-27 21:38:18 test.py[line:110] INFO 搜索"邓肯",期望"蒂姆"通过
Wed,2018-06-27 21:38:33 test.py[line:110] INFO 搜索"乔丹",期望"迈克尔"通过
Wed,2018-06-27 21:38:47 test.py[line:110] INFO 搜索"库里",期望"斯蒂芬"通过
Wed,2018-06-27 21:39:03 test.py[line:110] INFO 搜索"杜兰特",期望"凯文"通过
Wed,2018-06-27 21:39:17 test.py[line:110] INFO 搜索"詹姆斯",期望"勒布朗"通过

 

最新文章

  1. 【Django】--Models 和ORM以及admin配置
  2. windows下的host文件在哪里?做什么用的?
  3. yii2输出sql语句
  4. centos7 安装lamp
  5. 使用JDBC对数据库进行查询的前期准备工作,以及简单的JDBC访问MySQL数据库(Mac)
  6. Android请求返回417解决办法
  7. Ext.Net学习笔记17:Ext.Net GridPanel Selection
  8. 在eclipse中部署发布web项目 和 更改eclipseweb项目发布的路径
  9. Eclipse文件覆盖问题
  10. 可以通过Action来判断是什么操作触发了事件
  11. JS调用WebService,发布到IIS,网页提示WebService未定义[已解决]
  12. url路由配置以及渲染方式
  13. Apktool(2)——使用前必须知道的apk知识
  14. 最小的K个数(python)
  15. Java中资料的上传与下载
  16. Sql Server 向上取整、向下取整、四舍五入取整
  17. 五校联考 running (欧拉函数)
  18. 八大最安全的Linux发行版,具备匿名功能,做服务器的首选,web,企业服务器等
  19. python基础开发环境Pycharm的详细使用方法
  20. Spring MapFactoryBean例子

热门文章

  1. Nginx(二)-- 配置文件之虚拟主机配置
  2. Qt选择文件对话框-中文路径-转std::string
  3. php和mySQL结合使用
  4. 【Android N 7.1.1】 ActivityManagerService 获取cpu状态
  5. Python老王视频习题答案
  6. Android - 采用 SharedPreferences 存储数据
  7. Java实现远程服务生产与消费(RPC)的4种方法-RMI,WebService,HttpClient,RestTemplate
  8. python 10分钟入门pandas
  9. react路由守卫
  10. 用 Fiddler查看 Android 网络请求