本文主要是介绍python测试与数据分离,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数据获取
json:
{"test": [{"case": "normal","data": {"score": 0,"tag": "qiniu","templateId": 126},"expected": true},{"case": "err","data": {"score": 10,"tag": "qiniuqiniuqiniuqiniuqiniuqiniuqiniuqiniuqiniuqiniuqiniuqiniuqiniuqiniu","templateId": 126},"expected": false},{"case": "err2","data": {"score": 10,"tag": "sdfsf","templateId": 0},"expected": false}]
}
获取数据
import os,json
def get_data_path(case_path):file_name=os.path.dirname(case_path)test_data=os.sep.join([file_name,'data',os.path.basename(case_path).replace('.py','.json')])return test_datadef get_test_data(test_data_path,field='test'):case=[]data=[]expected=[]with open(test_data_path,encoding='utf-8') as f:dat=json.loads(f.read())test=dat[field]for td in test:case.append(td['case'])data.append(td['data'])expected.append(td['expected'])list_param=list(zip(case,data,expected))return case,list_param
测试方法:
# !/usr/bin/python3
# -*-coding:utf-8-*-
# Author: ppx
# Github:
# CreatDate: 2019/12/31 下午6:22
# Description:import requests, json, time
import urls.TemplateServiceImpl as tmpl
import urls.HsfTemplateServiceImpl as hsftmpl
import urls.CardServiceImpl as card
import rest, util
from HTMLTestRunner import HTMLTestRunner
import urls.HsfCouponServiceImpl as cardhsf
import urls.HsfTemplateServiceImpl as hsftmp
import urls.HsfTagServiceImpl as hsftag
import urls.HsfCouponServiceImpl as couponimport logging, sys, pytest
from tools.get_data import get_data_path
from tools.get_data import get_test_datacase,params = get_test_data(get_data_path(__file__))class TestClass:type = 1templateId = 127@pytest.mark.parametrize("case,data,expected", params, ids=case)def test_tag(self, case, data, expected):templateid = 126post = rest.post(hsftag.TagServiceImpl.TAGSERVICEIMPL_ADDTAG,data, self.type)assert post['success'] == expected
这篇关于python测试与数据分离的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!