github 地址: https://github.com/jerrylizilong/api_test_demo
整理如何用 unittest 编写接口测试用例,和使用 pytest+allure 生成报表。
每个接口只需要写上接口地址、加密参数列表,即可快速编写对应的测试用例。
api_demo.api_manage.api_base 通用方法: 如何为登录接口进行签名、拼接参数和 url,及发送。
def getOsign(user,osign_list): # 签名方法。 这里举例是进行 MD5 加密, 具体需要替换为实际的签名方法。
user['osign'] = util.getOsign(user, osign_list)
return user
def getOsign(user,osignList):
paraPand = ''
for para in osignList:
paraPand += str(user[para])
return md5(paraPand)
def api_send(user,osign_list,url,need_osign = True,isMock=False): # 具体的接口url 拼接、参数生成和发送方法
if need_osign:
getOsign(user,osign_list)
return send(user, url,isMock=isMock)
# http post 发送
def send(user,url,isMock=False,host=host,needJson=True):
body = json.dumps(user, default=lambda user: user.__dict__, sort_keys=True, skipkeys=True)
body = eval(body)
list = util.dict_2_str(body)
url = host+url+ '?' + list
print(url)
if isMock:
return mockData.mockData(url)
else:
response, content = util.sendRequest(url)
print(content)
if needJson:
content = json.loads(content)
return content
api_demo.mock 示例如何为登录接口生成 mock 数据。
def login(self,query):
data = {}
osign = query['osign'][0]
userName = query['userName'][0]
password = query['password'][0]
verifyCode = query['verifyCode'][0]
if osign != util.md5(userName +password+verifyCode) or verifyCode !='123456': # 签名不匹配,或者验证码错误
data['code']=4010
data['msg']='invalid request!'
elif userName !='correctuser' or password !='correctpassword' : # 用户名或密码错误
data['code']=500
data['msg']='username or password is wrong ,please try again!'
else: # 正常登录
data['code']=200
data['msg']='success!'
data['loginTime'] = self.loginTime
return data
api_demo.test 示例如何编写不同场景的测试用例。
isMock = True # 如果不需要使用 mock ,直接使用接口,此处改为 False
osign_list = ['userName', 'password','verifyCode'] # 定义签名参数列表,例如签名方法为 username+password+verifycode 做md5 。 具体需要替换为实际的签名参数列表。
url = '/login' # 具体的接口 url 相对路径, 测试时会拼凑为完整路径: http://host/login
def setUp(self):
import warnings
warnings.simplefilter("ignore", ResourceWarning)
print("start")
# 通用参数初始化
self.testuser = {}
self.testuser['userName']='correctuser'
self.testuser['password']='correctpassword'
self.testuser['verifyCode']='123456'
# 正常场景:login 是否成功。
def test_login(self):
result =login(self.testuser,isMock=True)
self.assertEqual(result['code'],code_success)
self.assertEqual(result['msg'],msg_success)
# 异常场景: userName错误。
def test_login_wrong_userName(self):
self.testuser['userName']='username'
result = login(self.testuser, isMock=True)
self.assertEqual(result['code'],code_login_fail)
self.assertEqual(result['msg'],msg_login_fail)
# 异常场景: 签名错误。
def test_login_osign_error(self):
from api_demo.api_manage import api_base
self.assertEqual(api_base.test_osign_error(self.testuser,osign_list,url,code_sign_error,msg_sign_error,isMock=isMock), 0)
通用的签名错误校验方法:
# 传入:签名参数列表 osign_list; 返回签名不匹配的参数数量,如果返回 0 ,表示测试通过
def test_osign_error(user,osign_list,url,code_sign_error,msg_sign_error,isMock=False):
osignFailCount = 0
for para in osign_list:
result = osign_error(user,osign_list,url,code_sign_error,msg_sign_error,para,isMock=isMock)
if not result:
osignFailCount += 1
print('osign para lenth : %d' % len(osign_list))
return osignFailCount
# 单个参数的签名校验:在生成完签名后修改某个参数值,看接口能否正确校验
def osign_error(user,osign_list,url,code_sign_error,msg_sign_error, para,isMock=False):
if para in ['']: # 过滤的加密参数,部分参数不需要加密验证
return True
else:
user = getOsign(user,osign_list)
if isinstance(user[para],int):
user[para]= user[para] + 1
else :
user[para]=str(user[para] + '1')
result = api_send(user, osign_list, url,need_osign=False,isMock=isMock)
if result['code'] == code_sign_error and result['msg'] == msg_sign_error:
return True
else:
print('osign error : %s, %s' % (para, result))
return False
run_pytest_entry.py 、 run_pytest.py
@pytest.mark.skipif(environmentFlag =='1', reason='skip') # 如果environmentFlag =='1', 跳过
# 异常场景: userName错误。
def test_login_wrong_userName(self):
print(environmentFlag =='1')
print('evironment is : ',environmentFlag)
self.testuser['userName']='username'
result = api_send(self.testuser,osign_list,url,isMock=isMock)
self.assertEqual(result['code'],code_login_fail)
self.assertEqual(result['msg'],msg_login_fail)
@pytest.mark.skipif(isMock , reason='skip') # 如果 isMock 为 true, 跳过
# 异常场景: password错误。
def test_login_wrong_password(self):
self.testuser['password']='password'
result = api_send(self.testuser,osign_list,url,isMock=isMock)
self.assertEqual(result['code'],code_login_fail)
self.assertEqual(result['msg'],msg_login_fail)
api_demo.init.py 文件中可以定义多个不同的测试环境地址,并通过 environmentFlag 进行切换:
environmentFlag='1'
if environmentFlag=='1':
host = 'http://test1.com'
elif environmentFlag=='2':
host = 'http://test2.com'
elif environmentFlag=='3':
host = 'http://test3.com'
python run_pytest_entry.py 1
其中最后的参数 1 为 environmentFlag, 如果需要切换不同的测试环境,只需传入不同的标记位。
freestyle job:
pipeline script:
pipeline {
agent {label 'apitest' } // 指定运行节点
stages {
stage('update') {
steps {
echo 'update..'
sh '/yourfolder/svn_update.sh'
}
}
stage('test') {
steps {
echo 'test..'
sh 'python3 /yourfolder/run_pytest_entry.py 1 4 ${JOB_NAME}'
}
}
stage('allure-report') {
steps {
echo 'allure-report..'
}
post {
always {
allure includeProperties: false, jdk: '', results: [[path: 'report']]
}
}
}
stage('sendding email'){
steps {
echo 'sendding email..'
}
post {
always {
script {
emailext attachLog: true, body: '${DEFAULT_CONTENT} ', recipientProviders: [[$class: 'RequesterRecipientProvider']], subject: '$DEFAULT_SUBJECT', to: 'youremail@test.com'
}
}
}
}
}
}