def test_book_002(self): """添加书籍""" data = self.excel.get_data(2, file_path('config', 'book.yml')) resp = self.request.post(self.excel.get_url(2), json=data) resp.close() book_id = resp.json()[0]['datas']['id'] write_content(file_path('data', 'book_id'), book_id) self.assert_result(2, resp)
def get_url(self, row: int): """获取案例url""" base_url = self.get_value(row, BookCaseEnum.URL.value) if '{book_id}' in base_url: url = base_url.replace('{book_id}', read_content(file_path('data', 'book_id'))) else: url = base_url return url
class TestBook: excel = ExcelOperator(file_path('data', 'book.xls'), 0) request = Requests() def assert_result(self, row: int, resp): assert self.excel.get_except_result(row) in json.dumps( resp.json(), ensure_ascii=False) def test_book_001(self): """获取所有书籍数据""" resp = self.request.get(self.excel.get_url(1)) resp.close() self.assert_result(1, resp) def test_book_002(self): """添加书籍""" data = self.excel.get_data(2, file_path('config', 'book.yml')) resp = self.request.post(self.excel.get_url(2), json=data) resp.close() book_id = resp.json()[0]['datas']['id'] write_content(file_path('data', 'book_id'), book_id) self.assert_result(2, resp) def test_book_003(self): """查看书籍""" resp = self.request.get(self.excel.get_url(3)) resp.close() self.assert_result(3, resp) def test_book_004(self): """更新书籍""" data = self.excel.get_data(4, file_path('config', 'book.yml')) resp = self.request.put(self.excel.get_url(4), json=data) resp.close() self.assert_result(4, resp) def test_book_005(self): """删除书籍""" resp = self.request.delete(self.excel.get_url(5)) resp.close() self.assert_result(5, resp)
def read_yaml(self, filedir, filename): """读取标准格式的yaml文件,如data下面的login.yaml""" with open(file_path(filedir, filename), 'r', encoding='utf-8') as f: return list(yaml.safe_load_all(f))
def dict_yaml(self, fileDir='config', fileName='book.yaml'): """读取字典格式的yaml文件,读取内容与excel中的data映射起来""" with open(file_path(filedir=fileDir, filename=fileName), 'r', encoding='utf-8') as f: return yaml.safe_load(f)
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time :2021/4/9 13:24 # @Author :cjw import pytest import json import allure from utils.file_operator import ExcelOperator from utils.enum import ApiCaseEnum from common.public import file_path, write_content, read_content from base.method import Requests excel = ExcelOperator(file_path('data', 'api.xls')) request = Requests() def run_case(case: dict): """案例执行""" resp = None method = case[ApiCaseEnum.METHOD.value].lower() if method == 'get': if headers := case[ApiCaseEnum.HEADER.value]: resp = request.get(case[ApiCaseEnum.URL.value], headers=headers) else: resp = request.get(case[ApiCaseEnum.URL.value]) elif method == 'post': if headers := case[ApiCaseEnum.HEADER.value]: if data_type := case[ApiCaseEnum.TYPE.value]: if data_type.lower() == 'data': resp = request.post(case[ApiCaseEnum.URL.value], headers=headers,
return case_list def _get_sheet(self): """获取表格的sheet""" work_book = open_workbook(self._file_path) if not isinstance(self._sheet, (int, str)): raise TypeError(f'excel文件中sheet名字或者索引不存在:{self._sheet}') if isinstance(self._sheet, int): sheet = work_book.sheet_by_index(self._sheet) else: sheet = work_book.sheet_by_name(self._sheet) return sheet if __name__ == '__main__': import json # yml_obj = YamlOperator(file_path('config', 'book.yml')) # print(yml_obj.data) # for yml in yml_obj.data: # print('book_002' in yml.keys()) excel_obj = ExcelOperator(file_path('data', 'api.xls')) # for data in excel_obj.run_cases: # if case_data := data[ApiCaseEnum.DATA.value]: # print(type(case_data), case_data) # if header := data[ApiCaseEnum.HEADER.value]: # print(type(header), header) for data in excel_obj.run_cases: code = data[ApiCaseEnum.STATUS_CODE.value] print(type(code), code)
def test_book_004(self): """更新书籍""" data = self.excel.get_data(4, file_path('config', 'book.yml')) resp = self.request.put(self.excel.get_url(4), json=data) resp.close() self.assert_result(4, resp)
def __init__(self, file_name, file_dir='data'): self.file_path = file_path(file_dir, file_name)
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time :2021/4/8 9:55 # @Author :cjw import pytest from base.method import Requests from utils.file_operator import YamlReader from common.public import file_path request = Requests() yaml = YamlReader(file_path('data', 'login_test.yml'), True) @pytest.mark.parametrize('data', yaml.data) def test_add(data): resp = request.post(data['url'], json=data['body']) result = resp.json() resp.close() assert result['code'] == data['expect']['code'] if __name__ == '__main__': pytest.main(['-v', '-s', 'test_login.py'])