python学习笔记(unittest)

时间:2024-07-23 23:03:44

刚刚放假回来我想很多人都还没有缓过来吧

这次介绍一个python自带的测试框架 unitest

 #!/usr/bin/env python
# -*- coding: utf_8 -*- import requests
import unittest class Testswcw_back(unittest.TestCase):
def setUp(self):
print "接口测试开始" def tearDown(self):
print "接口测试结束" def testlogin(self): #登录测试用例
url = 'http://localhost:8081/swcw/back/sysLogin.action'
postparams = {'username':'admin','password':''}
results = requests.post(url,postparams)
print "请求地址:",url
print "请求返回地址:",results.url
print "接口返回状态码:",results.status_code def testaddproducttype(self):
url = 'http://localhost:8081/swcw/back/producttypeForm.action'
login_data = {'username':'admin','password':''}#登录参数
postparams = {
"id": "",
"oldPid": "",
"pid": "",
"typename": "",
"code": "",
"showinnav": "",
"remark": "",
"": "保 存",
}#添加商品类别参数
s = requests.Session() login = s.post('http://localhost:8081/swcw/back/sysLogin.action',data=login_data)
#调用session方法发送post请求登录
results = s.post(url,data=postparams)
#再去调研session方法post请求看成同一用户 保留登录状态
print "请求地址:",url
print "请求返回地址:",results.url
print "接口返回状态码:",results.status_code if __name__ == "__main__":
unittest.main()

代码很简单,没有声明通用类,直接开始写接口测试

运行结果如下:

python学习笔记(unittest)

setUp、tearDown两个函数每次执行用例都会运行一遍

博主只能运用到这里