python学习笔记(unittest)
刚刚放假回来我想很多人都还没有缓过来吧
这次介绍一个python自带的测试框架 unitest
1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 4 import requests 5 import unittest 6 7 class Testswcw_back(unittest.TestCase): 8 def setUp(self): 9 print "接口测试开始" 10 11 def tearDown(self): 12 print "接口测试结束" 13 14 def testlogin(self): #登录测试用例 15 url = 'http://localhost:8081/swcw/back/sysLogin.action' 16 postparams = {'username':'admin','password':'123456'} 17 results = requests.post(url,postparams) 18 print "请求地址:",url 19 print "请求返回地址:",results.url 20 print "接口返回状态码:",results.status_code 21 22 def testaddproducttype(self): 23 url = 'http://localhost:8081/swcw/back/producttypeForm.action' 24 login_data = {'username':'admin','password':'123456'}#登录参数 25 postparams = { 26 "id": "", 27 "oldPid": "", 28 "pid": "24", 29 "typename": "104", 30 "code": "101", 31 "showinnav": "1", 32 "remark": "102", 33 "": "保 存", 34 }#添加商品类别参数 35 s = requests.Session() 36 37 login = s.post('http://localhost:8081/swcw/back/sysLogin.action',data=login_data) 38 #调用session方法发送post请求登录 39 results = s.post(url,data=postparams) 40 #再去调研session方法post请求看成同一用户 保留登录状态 41 print "请求地址:",url 42 print "请求返回地址:",results.url 43 print "接口返回状态码:",results.status_code 44 45 if __name__ == "__main__": 46 unittest.main()
代码很简单,没有声明通用类,直接开始写接口测试
运行结果如下:
setUp、tearDown两个函数每次执行用例都会运行一遍
博主只能运用到这里