初探自动化测试

Posted huchaopeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初探自动化测试相关的知识,希望对你有一定的参考价值。

python版本为3.x
http_request请求工具编写

 1 import requests
 2 class DemoHttpRequest:
 3 
 4     def demo_http_request(self, method, url, data = None, cookie = None):
 5 
 6         if method.lower() == get:
 7             res = requests.get(url, data, verify=False)
 8         elif method.lower == post:
 9             res = requests.post(url, data, cookies = cookie,verify=False)
10         return res

 

test_httprequest(注释的为代码的演进过程) 

key为聚合数据中老黄了的数据(https://www.juhe.cn/

  1 # -*- coding: utf-8 -*-
  2 # @Time    : 2019/12/31 6:14
  3 # @File    : test_http_request.py
  4 
  5 # 方法一
  6 # import unittest
  7 # from python11.Demo1.http_request import DemoHttpRequest
  8 # class TestHttpRequest(unittest.TestCase):
  9 #     def setUp(self):
 10 #         self.login_url = "http://v.juhe.cn/laohuangli/d"
 11 #         self.login_data = {"date":"2020-01-07","key":"f"}
 12 #         # 充值,没有充值的接口,这里仅仅代表示例
 13 #         # self.recharge_url = "http://v.juhe.cn/laohuangli/d"
 14 #         # self.recharge_data = {"date":"2020-01-07","key":"f"}
 15 #         # self.cookies = DemoHttpRequest().demo_http_request(‘get‘, self.url, self.data).cookies
 16 #     def test_calendar(self):
 17 #         res = DemoHttpRequest().demo_http_request("get", self.login_url, self.login_data)
 18 #         try:
 19 #             self.assertEqual(‘successed‘,res.json()[‘reason‘])
 20 #         except AssertionError as e:
 21 #             print("错误是{}".format(e))
 22 #             raise e
 23 #     # 需要传cookies, 示例
 24 #     # def test_recharge(self):
 25 #     #     res = DemoHttpRequest().demo_http_request("get", self.recharge_url, self.recharge_data,self.cookies )
 26 #     #     self.assertEqual(‘successed‘, res.json()[‘reason‘])
 27 #     def tearDown(self):
 28 #         pass
 29 ‘‘‘
 30 # 方法二 关于cookies  全局变量方法
 31 import unittest
 32 from python11.Demo1.http_request import DemoHttpRequest
 33 
 34 COOKIE = None     # 全局变量
 35 class TestHttpRequest(unittest.TestCase):
 36     def setUp(self):
 37         self.login_url = "http://v.juhe.cn/laohuangli/d"
 38 
 39     def test_calendar(self):
 40         global COOKIE   # 声明全局变量
 41         login_data = {"date": "2020-01-07", "key": "f"}
 42         res = DemoHttpRequest().demo_http_request("get", self.login_url, login_data)
 43         if res.cookies:     # 如果cookie存在
 44             COOKIE = res.cookies
 45         try:
 46             self.assertEqual(‘successed‘,res.json()[‘reason‘])
 47         except AssertionError as e:
 48             print("错误是{}".format(e))
 49             raise e
 50     # 需要传cookies, 示例
 51     # def test_recharge(self):
 52     #     global COOKIE
 53     #     res = DemoHttpRequest().demo_http_request("get", self.recharge_url, self.recharge_data, COOKIE )
 54     #     self.assertEqual(‘successed‘, res.json()[‘reason‘])
 55     def tearDown(self):
 56         pass
 57 ‘‘‘
 58 ‘‘‘
 59 # 方法三
 60 import unittest
 61 from python11.Demo1.http_request import DemoHttpRequest
 62 from python11.Demo1.get_data import GetData
 63 
 64 class TestHttpRequest(unittest.TestCase):
 65     def setUp(self):
 66         self.login_url = "http://v.juhe.cn/laohuangli/d"
 67 
 68     def test_calendar(self):
 69         login_data = {"date": "2020-01-07", "key": "f"}
 70         res = DemoHttpRequest().demo_http_request("get", self.login_url, login_data)
 71         if res.cookies:     # 如果cookie存在
 72             setattr(GetData, ‘Cookie‘, res.cookies)     # 将res.cookies值赋值给cookie
 73         try:
 74             self.assertEqual(‘successed‘,res.json()[‘reason‘])
 75         except AssertionError as e:
 76             print("错误是{}".format(e))
 77             raise e
 78     # 需要传cookies, 示例
 79     # def test_recharge(self):
 80     #     res = DemoHttpRequest().demo_http_request("get", self.recharge_url, self.recharge_data, getattr(GetData,"Cookie") )  # getattr(GetData,"Cookie")取值
 81     #     self.assertEqual(‘successed‘, res.json()[‘reason‘])
 82     def tearDown(self):
 83         pass
 84 ‘‘‘
 85 ‘‘‘
 86 # 参数化
 87 import unittest
 88 from python11.Demo1.http_request import DemoHttpRequest
 89 from python11.Demo1.get_data import GetData
 90 
 91 
 92 class TestHttpRequest(unittest.TestCase):
 93     def setUp(self):
 94         pass
 95     # def __init__(self, url, data, method = ‘get‘ , excepted = ‘successed‘, methodName = ‘test_api‘):
 96     #     super(TestHttpRequest, self).__init__(methodName) # 继承并保留父类的方法
 97     #     self.url = url
 98     #     self.data = data
 99     #     self.method = method
100     #     self.excepted = excepted
101     def test_api(self):
102         method = ‘get‘
103         url = ‘http://v.juhe.cn/laohuangli/d‘
104         data = {"date": "2020-01-07", "key": "f"}
105         # res = DemoHttpRequest().demo_http_request(self.method, self.url, self.data)
106         res = DemoHttpRequest().demo_http_request(method, url, data)
107         if res.cookies:     # 如果cookie存在
108             setattr(GetData, ‘Cookie‘, res.cookies)     # 将res.cookies值赋值给cookie
109         try:
110             print(res.json()[‘reason‘])
111             # self.assertEqual(self.excepted, res.json()[‘reason‘])
112             self.assertEqual(‘successed‘, res.json()[‘reason‘])
113 
114         except AssertionError as e:
115             print("错误是{}".format(e))
116             raise e
117     # 需要传cookies, 示例
118     # def test_recharge(self):
119     #     res = DemoHttpRequest().demo_http_request("get", self.recharge_url, self.recharge_data, getattr(GetData,"Cookie") )  # getattr(GetData,"Cookie")取值
120     #     self.assertEqual(‘successed‘, res.json()[‘reason‘])
121     def tearDown(self):
122         pass
123 ‘‘‘
124 ‘‘‘
125 # 参数化
126 import unittest
127 from python11.Demo1.http_request import DemoHttpRequest
128 from python11.Demo1.get_data import GetData
129 
130 
131 class TestHttpRequest(unittest.TestCase):
132     def __init__(self):
133         super().__init__()
134         self.url = ‘http://v.juhe.cn/laohuangli/d‘
135         self.data = {‘date‘ : ‘2020-01-07‘, ‘key‘: ‘ff‘}
136         self.method = ‘get‘
137         self.excepted = ‘successed‘
138 
139     def test_api(self):
140         res = DemoHttpRequest().demo_http_request(self.method, self.url, self.data)
141         try:
142             print(res.json()[‘reason‘])
143             self.assertEqual(‘successed‘, res.json()[‘reason‘])
144         except AssertionError as e:
145             print("错误是{}".format(e))
146             raise e
147 if __name__ == ‘__main__‘:
148     TestHttpRequest().test_api()
149 ‘‘‘
150 # 参数化
151 import unittest
152 from python11.Demo1.http_request import DemoHttpRequest
153 from python11.Demo1.get_data import GetData
154 
155 class TestHttpRequest(unittest.TestCase):
156     def __init__(self, method, url, data, excepted, methodName):
157         super().__init__(methodName)
158         self.url = url
159         self.data = data
160         self.method = method
161         self.excepted = excepted
162 
163 
164     def test_api(self):
165         res = DemoHttpRequest().demo_http_request(self.method, self.url, self.data)
166         try:
167             print(res.json()[reason])
168             self.assertEqual(successed, res.json()[reason])
169         except AssertionError as e:
170             print("错误是{}".format(e))
171             raise e
test_suite添加组件,生成测试报告
# -*- coding: utf-8 -*-
# @Time    : 2019/12/31 6:14
# @File    : test_suite.py

‘‘‘
import unittest
import htmlTestReportCN
from python11.Demo1 import test_http_request
# 加载用例,示例1
# 创建容器
suite = unittest.TestSuite()

# 加载用例
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromModule(test_http_request))

# 执行用例
with open("test_summer.html", "wb") as file:
    runner = HTMLTestReportCN.HTMLTestRunner(stream=file, verbosity=1,title=‘第一次写‘,description=‘太难了‘,tester=‘aa‘)
    runner.run(suite)

‘‘‘
import unittest
import HTMLTestReportCN
from python11.Demo1.test_http_request import TestHttpRequest
# 列表中嵌套字典
test_data = [{url : "http://v.juhe.cn/laohuangli/d",
              data : {"date": "2020-01-07", "key": "ff"},
              excepted : successed,
              method:get,
              methodName : test_api
              }]

# 创建容器
suite = unittest.TestSuite()

# 加载用例
print(test_data[0][method])
suite.addTest(TestHttpRequest( test_data[0][method],
                               test_data[0][url],
                               test_data[0][data],
                               test_data[0][excepted],
                               test_data[0][methodName]
                               ))

# 执行用例
with open("test_summer.html", "wb") as file:
    runner = HTMLTestReportCN.HTMLTestRunner(stream=file, verbosity=1,title=第一次写,description=太难了,tester=胡朝鹏)
    runner.run(suite)

美化后的报告下载网址:https://github.com/findyou/HTMLTestRunnerCN

以上是关于初探自动化测试的主要内容,如果未能解决你的问题,请参考以下文章

puptter自动化测试初探

iOS 自动化测试初探

python behave自动化测试框架- 初探BDD

初探自动化测试

初探接口测试框架--python系列1

postman自动化测试初探