ptyhon - 接口自动化测试实战case1

work_20181203_httprequest.py:

import requests
class http_request:

def http_get(url,params):
res = requests.get(url,params)
return res

def http_post(url,params,cookies = None):
res = requests.post(url,params,cookies = cookies)
return res

work_20181203_testcase.py:


#-*- coding:utf-8 -*-
# author:jiaxy
# datetime:2018/12/3 14:03
# file: work_20181203_testcase.py

'''
登录

login='http://47.107.168.87:8080/futureloan/mvc/api/member/login'
login_data={'mobilephone':18688773467,'pwd':'123456'}

充值

recharge='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'
recharge_data={'mobilephone':18688773467,'amount':'1000'}
'''

import unittest
from jiaxy.work_20181203_httprequest import http_request

class Test(unittest.TestCase):

login = 'http://47.107.168.87:8080/futureloan/mvc/api/member/login'
login_data={'mobilephone':18688773467,'pwd':'123456'}
recharge = 'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'
recharge_data={'mobilephone':18688773467,'amount':'1000'}

def setUp(self):
print('开始测试...')

def tearDown(self):
print('测试完成...')

def test_001_login_nomobie(self):
login_url = self.login
login_data = {'mobilephone':'','pwd':'123456'}
login_msg = http_request.http_get(login_url,login_data)
try:
self.assertEquals('手机号不能为空',login_msg.json()['msg'])
print('test_001_login_nomobie 测试通过')
except Exception as e:
print('test_001_login_nomobie 测试不通过')
raise e

def test_002_login_nopwd(self):
login_url = self.login
login_data = {'mobilephone':18688773467,'pwd':''}
login_msg = http_request.http_get(login_url,login_data)
try:
self.assertEquals('密码不能为空',login_msg.json()['msg'])
print('test_002_login_nopwd 测试通过')
except Exception as e:
print('test_002_login_nopwd 测试不通过')
raise e


def test_003_login_errorpwd(self):
login_url = self.login
login_data = {'mobilephone':18688773467,'pwd':'12345'}
login_msg = http_request.http_get(login_url,login_data)
try:
self.assertEquals('用户名或密码错误',login_msg.json()['msg'])
print('test_003_login_errorpwd 测试通过')
except Exception as e:
print('test_003_login_errorpwd 测试不通过')
raise e

def test_004_login_success(self):
login_url = self.login
login_data = self.login_data
login_msg = http_request.http_get(login_url,login_data)
try:
self.assertEquals('登录成功',login_msg.json()['msg'])
print('test_004_login_success 测试通过')
cookies = login_msg.cookies
except Exception as e:
print('test_004_login_success 测试不通过')
raise e
cookies = None
return cookies

def test_005_recharge_success(self):
recharge_url = self.recharge
recharge_data = self.recharge_data
cookies = self.test_004_login_success()
recharge_msg = http_request.http_post(recharge_url,recharge_data,cookies = cookies)
try:
self.assertEquals('充值成功',recharge_msg.json()['msg'])
print('test_005_recharge_success 测试通过')
except Exception as e:
print('test_005_recharge_success 测试不通过')
raise e

def test_006_recharge_nomobile(self):
recharge_url = self.recharge
recharge_data = {'mobilephone':'','amount':'1000'}
cookies = self.test_004_login_success()
recharge_msg = http_request.http_post(recharge_url,recharge_data,cookies = cookies)
try:
self.assertEquals('手机号不能为空',recharge_msg.json()['msg'])
print('test_006_recharge_nomobile 测试通过')
except Exception as e:
print('test_006_recharge_nomobile 测试不通过')
raise e

def test_007_recharge_errmobile(self):
recharge_url = self.recharge
recharge_data = {'mobilephone':1868877346,'amount':'1000'}
cookies = self.test_004_login_success()
recharge_msg = http_request.http_post(recharge_url,recharge_data,cookies = cookies)
try:
self.assertEquals('手机号码格式不正确',recharge_msg.json()['msg'])
print('test_007_recharge_errmobile 测试通过')
except Exception as e:
print('test_007_recharge_errmobile 测试不通过')
raise e

def test_008_recharge_nocharge(self):
recharge_url = self.recharge
recharge_data = {'mobilephone':18688773467,'amount':''}
cookies = self.test_004_login_success()
recharge_msg = http_request.http_post(recharge_url,recharge_data,cookies = cookies)
try:
self.assertEquals('请输入金额',recharge_msg.json()['msg'])
print('test_008_recharge_nocharge 测试通过')
except Exception as e:
print('test_008_recharge_nocharge 测试不通过')
raise e

def test_009_recharge_toolargecharge(self):
recharge_url = self.recharge
recharge_data = {'mobilephone':18688773467,'amount':'10000000000000000000000'}
cookies = self.test_004_login_success()
recharge_msg = http_request.http_post(recharge_url,recharge_data,cookies = cookies)
try:
self.assertEquals('请输入范围在0到50万之间的正数金额',recharge_msg.json()['msg'])
print('test_009_recharge_toolargecharge 测试通过')
except Exception as e:
print('test_009_recharge_toolargecharge 测试不通过')
raise e

work_20181203_testrun.py:


#-*- coding:utf-8 -*-
# author:jiaxy
# datetime:2018/12/3 14:04
# file: work_20181203_testrun.py

from jiaxy import work_20181203_testcase
import HTMLTestRunner
import unittest

def runTest():
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromModule(work_20181203_testcase))
with open('test_result.html','wb+') as file:
runner = HTMLTestRunner.HTMLTestRunner(
stream = file,
verbosity = 2
)
runner.run(suite)

if __name__ == '__main__':
runTest()


测试报告:

 

 
posted @ 2018-12-03 15:41  JiaxyGogogo  阅读(695)  评论(0编辑  收藏  举报