python接口自动化 目录结构
/api
封装接口 :接口地址,接口get ,post方法,返回接口response
import requests import app class search(): def __init__(self): self.url = app.baseurl+'/admin___________' def getsonadress(self,father): data = {"filter_parent_id": father} response = requests.post(self.url,json = data ) return response
/lib
第三方库 :如生成测试报告 HTMLTestRunner_PY3.py
/log
日志
/report
测试报告
/script
测试用例 测试脚本
setUP
tearDwon
tes01_XX()
import random import unittest from api.serchadress import search import app class searchAdress(unittest.TestCase): def setUp(self) -> None: self.searchsonadress = search() def tearDown(self) -> None: return 1 def test_01(self): print(self.searchsonadress.url) father = str(int(random.uniform(1,100))) response = self.searchsonadress.getsonadress(father) app.logger.error('srm son adress{}'.format(response.json())) self.assertEqual(response.json().get("code"),0) print(response.json())
app.python :全局变量,日志
import logging from logging import handlers import os baseurl ="http://opc-staging.XXX.com" basedir = os.path.dirname(os.path.abspath(__file__)) logger = logging.getLogger() logger.setLevel(logging.INFO) sh = logging.StreamHandler() fil = basedir+os.sep+'log'+os.sep+ 'test.log' fh = logging.handlers.TimedRotatingFileHandler(fil,encoding='utf-8') # 4、设置日志格式,创建格式化器 fmate = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s' fmt = logging.Formatter(fmate) sh.setFormatter(fmt) fh.setFormatter(fmt) logger.addHandler(sh) logger.addHandler(fh)
run_suit.py
创建测试套件,并执行测试套件,生成测试报告
import os import time import unittest import app from lib.HTMLTestRunner_PY3 import HTMLTestRunner from script.serachAdress import searchAdress suit = unittest.TestSuite() suit.addTest(unittest.makeSuite(searchAdress)) # 生成测试报告 report_dir = app.basedir +os.sep+"report"+os.sep+"report{}.html".format(time.strftime("%Y%m%d-%H-%M-%S")) with open(report_dir,'wb') as f: runner = HTMLTestRunner(f,verbosity=2,title='测试') runner.run(suit)