Demo.py #coding:utf-8 # import requests import json ''' data = { 'username':'jackson', 'password':'a123456' } res = requests.post(url = 'http://127.0.0.1:8000/login/', data = data) print res.json() ''' class RunMain: def __init__(self,url,method, data=None): self.res = self.run_main(url, data, method) def send_post(self,url, data): res = requests.post(url = url ,data = data).json() return json.dumps(res,indent=2,sort_keys=True) def send_get(self,url, data): res = requests.get(url = url ,data = data).json() return json.dumps(res,indent=2,sort_keys=True) def run_main(self,url, data, method): res = None if method == 'GET': res = self.send_get(url, data) else: res = self.send_post(url,data) return res if __name__ == "__main__": url = 'https://coding.imooc.com/api/pjlist' data = { 'cid':'75', 'page':'1', 'secrect':'25546c47f8560361a25ff8240b3a858e', 'timestamp':'1541404380460', 'token':'c561708e886cb11705308d76f163a9d9', 'uid':'6469051', 'uuid':'4a5207345f879ce7be25db6d3755315b' } run = RunMain(url, 'POST', data) print(run.res)
Python自带的test框架unittest
demo.py
#coding:utf-8 # import requests import json ''' data = { 'username':'jackson', 'password':'a123456' } res = requests.post(url = 'http://127.0.0.1:8000/login/', data = data) print res.json() ''' class RunMain: # def __init__(self,url,method, data=None): self.res = self.run_main(url, data, method) def send_post(self,url, data): res = requests.post(url = url ,data = data).json() print(type(res)) return json.dumps(res,indent=2,sort_keys=True) def send_get(self,url, data): res = requests.get(url = url ,data = data).json() return json.dumps(res,indent=2,sort_keys=True) def run_main(self,url, data, method): res = None if method == 'GET': res = self.send_get(url, data) else: res = self.send_post(url,data) return res if __name__ == "__main__": url = 'https://coding.imooc.com/api/pjlist' data = { 'cid':'75', 'page':'1', 'secrect':'25546c47f8560361a25ff8240b3a858e', 'timestamp':'1541404380460', 'token':'c561708e886cb11705308d76f163a9d9', 'uid':'6469051', 'uuid':'4a5207345f879ce7be25db6d3755315b' } run = RunMain(url, 'POST', data) print(run.res)
test_method.py
#coding:utf-8 import unittest class TestMethod(unittest.TestCase): @classmethod def setUpClass(cls): print("类执行之前的方法") def setUp(self): print('test-->setup') @classmethod def tearDownClass(cls): print("类执行之后的方法") def tearDown(self): print('test-->tearDown') def test_01(self): print("这是第一个测试方法") def test_02(self): print("这是第2个测试方法") if __name__ == '__main__': unittest.main()
引入unittest
Demo02.py
#coding:utf-8 # import requests import json ''' data = { 'username':'jackson', 'password':'a123456' } res = requests.post(url = 'http://127.0.0.1:8000/login/', data = data) print res.json() ''' class RunMain: # def __init__(self,url,method, data=None): # self.res = self.run_main(url, data, method) def send_post(self,url, data): res = requests.post(url = url ,data = data).json() print(type(res)) # return json.dumps(res,indent=2,sort_keys=True) return res def send_get(self,url, data): res = requests.get(url = url ,data = data).json() # return json.dumps(res,indent=2,sort_keys=True) return res def run_main(self,url, data, method): res = None if method == 'GET': res = self.send_get(url, data) else: res = self.send_post(url,data) return res if __name__ == "__main__": url = 'https://coding.imooc.com/api/pjlist' data = { 'cid':'75', 'page':'1', 'secrect':'25546c47f8560361a25ff8240b3a858e', 'timestamp':'1541404380460', 'token':'c561708e886cb11705308d76f163a9d9', 'uid':'6469051', 'uuid':'4a5207345f879ce7be25db6d3755315b' } run = RunMain(url, 'POST', data) print(run.res)
test_method02.py
#coding:utf-8 import unittest from demo02 import RunMain class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test_01(self): print("test1") def test_02(self): print("test2") url = "http://coding.imooc.com/api/cate" data = { 'cid':'0', 'page':'1', 'secrect':'25546c47f8560361a25ff8240b3a858e', 'timestamp':'1541744582639', 'token':'b8089bb9102b565dd1025d61a5290177', 'uid':'6469051', 'uuid':'4a5207345f879ce7be25db6d3755315b' } res = self.run.run_main(url, data, 'POST') # print(res['errorCode']) print(res) self.assertEqual(res['errorCode'], 1007, '测试失败') print('this second case') if __name__ == "__main__": unittest.main()
测试生成报告工具HTMLTestRunner.py
下载地址
http://tungwaiyip.info/software/HTMLTestRunner_0_8_2/HTMLTestRunner.py
将HTMLTestRunner.py拷贝到python的安装目录下
D:python37Lib
生成测试报告的示例:
#coding:utf-8 import unittest from demo02 import RunMain import HTMLTestRunner class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test_01(self): print("test1") def test_02(self): print("test2") url = "http://coding.imooc.com/api/cate" data = { 'cid':'0', 'page':'1', 'secrect':'25546c47f8560361a25ff8240b3a858e', 'timestamp':'1541744582639', 'token':'b8089bb9102b565dd1025d61a5290177', 'uid':'6469051', 'uuid':'4a5207345f879ce7be25db6d3755315b' } run = RunMain() res = run.run_main(url, data, 'POST') # print(res['errorCode']) print(res) self.assertEqual(res['errorCode'], 1007, '测试失败') print('this second case') if __name__ == "__main__": # unittest.main() filepath = "htmlreport.html" fp = file(filepath,'wb') suite = unittest.TestSuite() suite.addTest(TestMethod('test_02')) suite.addTest(TestMethod('test_01')) runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title = 'html report') runner.run(suite)