了解什么是自动化测试框架

学习selenium ---学习Junit-----统一你写的用例---加验证和断言---生成测试报告
已经学习了selenium和JUnit,就是不知道怎么同一自己写的用例和生成一份报告
在网上找到了一份别人使用python写的自动化测试框架的代码,在这里做一个阅读跟理解
给出网址:http://sleepycat.org/blog/33/#reply_to_user
这里一共分为9个部分
1)__init__.py;这里面只有版本信号
2)env.py:这里是环境的一个配置文件,在这里面引入了一个threading,这是一个知识点。用到threading.local() 和 threading.lock()
# -*- coding: utf-8 -*-
import threading
# driver of web browser, such as: webdriver.Firefox()
#设置浏览器的选择。先设置为None
BROWSER = None
# path where to save the result log.
#设置保存结果的log的路径
RESULT_PATH = ""
EXCEL_DATA_PATH = ""
# test case/module variables.
#测试case的变量
#===============================================================================
# CASE_START_TIME = "N/A"
# CASE_STOP_TIME = "N/A"
# CASE_NAME = "N/A"
# CASE_PASS = "N/A"
# CASE_WARNINGS = 0
#
# MODULE_NAME = ""
#===============================================================================
# multi-threading
#设置多线程并发
#threading.local()这个方法用来保存一个全局变量,只有在当前线程才能访问
#在不同的线程中都会开辟一个新的内存空间来存储这个值,而且是隔离的,别的线程无法访问
threadlocal = threading.local()
# start/stop time of the whole test
#设置总的开始/结束时间 成功和失败的case
TOTAL_START_TIME = "N/A"
TOTAL_STOP_TIME = "N/A"
TOTAL_TESTCASE_PASS = 0
TOTAL_TESTCASE_FAIL = 0
# initial URL of the testing page.
#初始化要测试的网址
BASE_URL = ""
# TESTING_BROWSER is one of TESTING_BROWSERS
#是用哪个浏览器
TESTING_BROWSER = ""
TESTING_BROWSERS = ""
# Data of test result, for generate report in excel.
#一些报告数据
EXCEL_REPORT_DATA = []
HTMLREPORT_TESTCASES = []
HTMLREPORT_SCREENSHOT_NAME = ""
FIREFOX_BINARY = ""
RESERVED_FIREFOX_BINARY = ""
DRIVER_OF_CHROME = ""
DRIVER_OF_IE = ""
FAST_FAIL = False
EXIT_STATUS = 0
BROWSER_VERSION_INFO = {}
#锁住线程
THREAD_LOCK = threading.Lock()
RESTART_BROWSER = True
SUPPORT_ANGULARJS = True3)htmlreport.py
# -*- coding: utf-8 -*-
import os, shutil, datetime
import env, common
#创建一个html头部内容用来输出报告
def html_source_header(title="Knitter Web Automation Test Result"):
return """<!DOCTYPE HTML>
<html>
<head>
<title>%s</title>
<style>
a {
color: #034CAC;
overflow: hidden;
text-decoration: none;
display: inline; /* do not show the dot of list */
}
a:hover {
text-decoration: underline;
color: #034CAC;
}
a:visited {
color: #000000;
}
button {
width: 120px;
}
body {
width: 100%%;
color: #000000;
font-size: 13px;
font-family: verdana, Courier New;
text-align: center;
}
table, td, th {
border: 1px solid black;
border-collapse: collapse;
width: 1260px;
text-align: center;
}
.tcaption {
background-color: #65ACCA;
}
.theader {
background-color: #D7EAF2;
}
.tfail{
background-color: #FF6F6F;
}
h1{
text-align: center;
}
</style>
</head>
""" % title
def html_source_body(title="Knitter Web Automation Test Result", countdown=True):
if countdown == False:
return """<body> <h1>%s</h1> <p>[<a href="..\index.html">Last Test</a>] [<a href="..\history.html">History Tests</a>]""" % title
return """
<body>
<script>
function countdown(){
var timer = 9;
setInterval(function() {
timer--;
document.getElementById(‘timer‘).innerHTML = timer;
}, 1000);
}
function autorefresh(){
window.location.reload();
}
countdown();
window.setTimeout(‘autorefresh()‘, 9000);
</script>
<h1>%s</h1>
<p style="text-align: center">
[<a href="index.html">Last Test</a>] [<a href="history.html">History Tests</a>]
</p>
<p style="text-align: center">
Pending to do auto-refresh in [<span id=‘timer‘>9</span>] seconds. <button onclick="window.location.reload();">Refresh Now</button></p>
""" % title
#html的table
def html_source_table1(args):
return """
<table>
<tr class="tcaption"><th colspan="5">Test Status</th></tr>
<tr class="theader">
<th>Time</th>
<th>Duration</th>
<th>Total Cases</th>
<th>Passed Cases</th>
<th>Failed Cases</th>
</tr>
<tr>
<td style=‘width: 35%%‘>%s => %s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
</table>
""" % tuple(args)
def html_source_table_history_header():
return """
<table>
<tr class="tcaption"><th colspan="5">History Results (Latest 100 Results)</th></tr>
<tr class="theader">
<th>Result Folder</th>
<th>Duration</th>
<th>Total Cases</th>
<th>Passed Cases</th>
<th>Failed Cases</th>
</tr>
"""
def html_source_table_history(*args):
return """
<tr>
<td style=‘width: 36%%‘><a href="%s/index.html">%s</a></td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
""" % args
def html_source_table2():
return """
<br />
<br />
<table>
<tr class="tcaption"><th colspan="5">Test Cases</th></tr>
<tr class="theader">
<th>Time</th>
<th>Case</th>
<th>Duration</th>
<th>Browser</th>
<th>Result</th>
</tr>
"""
def html_source_test_cases(test_cases):
return_src_code = ""
for test_case in test_cases:
return_src_code += """
<tr>
<td style="width: 20%%">%s</td>
<td style="width: 52%%; text-align: left;"> %s</td>
<td>%s</td>
<td>%s</td>
%s
</tr>
""" % tuple(test_case)
return return_src_code
def html_source_end_table():
return """
</table>
<br />
<br />
<br />
"""
def html_source_version_info():
return """
<p style="text-align: center">%s</p>
""" % common.get_version_info()
def html_source_foot():
return """
<hr />
<p style="text-align: center">2015 DB Schenker</p>
<br />
<body>
</html>
"""
def generate_html_report(test_status, test_cases=[], countdown=True):
common.mkdirs(os.path.join(env.RESULT_PATH, "result"))
with open(os.path.join(env.RESULT_PATH, "result", "index.html"), "w") as f:
f.write(html_source_header())
f.write(html_source_body(countdown=countdown))
f.write(html_source_table1(test_status))
f.write(html_source_table2())
f.write(html_source_test_cases(test_cases))
f.write(html_source_end_table())
f.write(html_source_version_info())
f.write(html_source_foot())
def save_current_report_to_repository():
report_dir = os.path.join(env.RESULT_PATH,
"result",
"%s__%s" % (datetime.datetime.strptime(env.TOTAL_START_TIME, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d_%H%M%S"),
datetime.datetime.strptime(env.TOTAL_STOP_TIME, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d_%H%M%S")))
common.delete_then_mkdir(report_dir)
common.copy(os.path.join(env.RESULT_PATH, "result", "testcase"), os.path.join(report_dir, "testcase"))
common.copy(os.path.join(env.RESULT_PATH, "result", "screenshots"), os.path.join(report_dir, "screenshots"))
common.copy(os.path.join(env.RESULT_PATH, "result", "index.html"), report_dir)
with open(os.path.join(report_dir, "status.ini"), "w") as f:
f.write("Duration=%s\n" % str(datetime.datetime.strptime(env.TOTAL_STOP_TIME, "%Y-%m-%d %H:%M:%S") - datetime.datetime.strptime(env.TOTAL_START_TIME, "%Y-%m-%d %H:%M:%S")))
f.write("TotalCases=%s\n" % str(env.TOTAL_TESTCASE_PASS+env.TOTAL_TESTCASE_FAIL))
f.write("PassedCases=%s\n" % str(env.TOTAL_TESTCASE_PASS))
f.write("FailedCases=%s\n" % str(env.TOTAL_TESTCASE_FAIL))
def generate_report_history():
folders = common.get_sub_folder_names(os.path.join(env.RESULT_PATH, "result"))
reports = []
for folder in folders:
if len(folder) == 36:
reports.append(folder)
with open(os.path.join(env.RESULT_PATH, "result", "history.html"), "w") as f:
f.write(html_source_header(title="Knitter Web Automation Test History"))
f.write(html_source_body(title="Knitter Web Automation Test History"))
f.write(html_source_table_history_header())
i = 0
for report in sorted(reports, reverse=True):
Duration = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "Duration")
TotalCases = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "TotalCases")
PassedCases = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "PassedCases")
FailedCases = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "FailedCases")
f.write(html_source_table_history(report, report, Duration, TotalCases, PassedCases, FailedCases))
i = i + 1
if i > 100: break
f.write(html_source_end_table())
f.write(html_source_foot())
if __name__ == "__main__":
pass
env.RESULT_PATH = r"E:\EclipseWorkspace\claims-qa-test"
generate_report_history()
#===============================================================================
# generate_html_report(["2015-03-05 09:54:06", "2015-03-05 09:59:17", "11:05:12", "1", "1", "0"],
# [
# ["2015-03-03 17:28:23", "claims", "WC2TC0047_CreateNewPreliminaryWithMandatoryFieldsSuccessfullyMandatoryFieldsSuccessfully",
# "0:07:05", "Firefox", ‘<td class="tfail"><a href="#">Fail</a></td>‘, ‘<a href="#">Check Image</a>‘],
# ["2015-03-03 17:28:23", "claims", "WC2TC0047_CreateNewPreliminaryWithMandatoryFieldsSuccessfully",
# "0:07:05", "Firefox", ‘<td class="tfail"><a href="#">Fail</a></td>‘, ‘<a href="#">Check Image</a>‘],
# ]
#
#
# )
#===============================================================================本文出自 “ehealth” 博客,谢绝转载!
原文:http://ehealth.blog.51cto.com/10942284/1729249