码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记(接口自动化框架)

时间:2016-04-12 12:23:30      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

之前是利用python自带的unittest测试框架

这次自己设计一个 之后再一点点往里面加功能

(ps:当然这个框架真的是很简单。。很简单。。。很简单。。。)

excel文件格式:

技术分享

 1 #!/usr/bin/env python
 2 # -*- coding: utf_8 -*-
 3 
 4 import xlrd
 5 import json
 6 
 7 
 8 class CreateExcel:
 9     def __init__(self):
10         pass
11 
12     @classmethod
13     def open_excel(cls):
14         path = "testcase.xls"
15         workbook = xlrd.open_workbook(path)
16         table = workbook.sheets()[0]
17         return table
18     # 获取sheet
19 
20     @classmethod
21     def get_nrows(cls, table):
22         nrows = table.nrows
23         return nrows
24     # 获取行号
25 
26     @classmethod
27     def get_name(cls, table, nrows):
28         testname = []
29         for i in range(1, nrows):
30             testname.append(table.cell(i, 0).value)
31         return testname
32     # 获取用例name
33 
34     @classmethod
35     def get_data(cls, table, nrows):
36         testdata = []
37         for i in range(1, nrows):
38             data = json.loads(table.cell(i, 1).value)
39             testdata.append(data)
40         return testdata
41     # 获取data接口参数
42 
43     @classmethod
44     def get_url(cls, table, nrows):
45         testurl = []
46         for i in range(1, nrows):
47             testurl.append(table.cell(i, 2).value)
48         return testurl
49     # 获取接口测试url
50 
51     @classmethod
52     def get_method(cls, table, nrows):
53         testmethod = []
54         for i in range(1, nrows):
55             testmethod.append(table.cell(i, 3).value)
56         return testmethod
57     # 获取接口测试method
58 
59     @classmethod
60     def get_pattern(cls, table, nrows):
61         testpattern = []
62         for i in range(1, nrows):
63             testpattern.append(table.cell(i, 4).value)
64         return testpattern
65     # 获取接口期望响应结果
66 
67     @classmethod
68     def get_report(cls, table, nrows):
69         testreport = []
70         for i in range(1, nrows):
71             testreport.append(table.cell(i, 5).value)
72         return testreport
73         # 获取用例期望的运行结果
74 
75 
76 if __name__ == "__main__":
77     CreateExcel()

上面代码是处理excel文档的

下面代码是测试平台

 1 #!/usr/bin/env python
 2 # -*- coding: utf_8 -*-
 3 
 4 
 5 import requests
 6 import re
 7 from createexcel import CreateExcel
 8 
 9 
10 class CreateTest:
11     def __init__(self):
12         pass
13 
14     @classmethod
15     def test_api(cls, method, url, data):
16         global results
17         if method == "post":
18             results = requests.post(url, data)
19         if method == "get":
20             results = requests.get(url, data)
21         return results
22 
23     @classmethod
24     def test_on(cls):
25         print "用例执行开始"
26 
27     @classmethod
28     def test_close(cls):
29         print "用例执行结束"
30 
31     @classmethod
32     def test_result(cls, ra, rb):
33         if ra == rb:
34             print "测试结果: 测试通过"
35         else:
36             print "测试结果: 测试失败"
37 
38     @classmethod
39     def test_http(cls, code):
40         if code == 200:
41             print "测试请求: 请求通过"
42 
43     @classmethod
44     def test_main(cls):
45         global report
46         table = CreateExcel.open_excel()
47         nrows = CreateExcel.get_nrows(table)
48         for i in range(0, nrows - 1):
49             testname = CreateExcel.get_name(table, nrows)[i]
50             testdata = CreateExcel.get_data(table, nrows)[i]
51             testurl = CreateExcel.get_url(table, nrows)[i]
52             testmethod = CreateExcel.get_method(table, nrows)[i]
53             testpattern = CreateExcel.get_pattern(table, nrows)[i]
54             testreport = CreateExcel.get_report(table, nrows)[i]
55             CreateTest.test_on()
56             print "测试用例:", testname
57             try:
58                 testresults = CreateTest.test_api(testmethod, testurl, testdata)
59                 CreateTest.test_http(testresults.status_code)
60                 pattern = re.compile(testpattern)
61                 match = pattern.search(testresults.url)
62                 if match.group() == testpattern:
63                     report = "pass"
64                 CreateTest.test_result(testreport, report)
65             except AttributeError:
66                 report = "no"
67                 CreateTest.test_result(testreport, report)
68             except Exception.__base__:
69                 print "测试请求: 请求失败"
70                 report = None
71                 CreateTest.test_result(testreport, report)
72             CreateTest.test_close()
73 
74 
75 if __name__ == __main__:
76     CreateTest()

 

python学习笔记(接口自动化框架)

标签:

原文地址:http://www.cnblogs.com/cllovewxq/p/5381814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!