def file_data(file, line=1, sheet="Sheet1", key=None): """ Support file parameterization. d.json ```json { "login": [ ["admin", "admin123"], ["guest", "guest123"] ] } ``` >> @file_data(file="./d.json", key="login") ... def test_case(self, username, password): ... print(username) ... print(password) """ suffix = file.split(".")[-1] if suffix == "csv": data_list = conversion.csv_to_list(file, line=line) elif suffix == "xlsx": data_list = conversion.excel_to_list(file, sheet=sheet, line=line) elif suffix == "json": data_list = conversion.json_to_list(file, key=key) elif suffix == "yaml": data_list = conversion.yaml_to_list(file, key=key) else: raise FileTypeError("Your file is not supported: {}".format(file)) return data(data_list)
def file_data(file, line=1, sheet="Sheet1", key=None): """ Support file parameterization. d.json ```json { "login": [ ["admin", "admin123"], ["guest", "guest123"] ] } ``` >> @file_data(file="d.json", key="login") ... def test_case(self, username, password): ... print(username) ... print(password) """ if file is None: raise FileExistsError("File name does not exist.") if os.path.isfile(file) is True: file_path = file else: stack_t = sys_inspect.stack() ins = sys_inspect.getframeinfo(stack_t[1][0]) file_dir = os.path.dirname( os.path.dirname(os.path.abspath(ins.filename))) file_path = None for root, dirs, files in os.walk(file_dir, topdown=False): for f in files: if f == file: file_path = os.path.join(root, file) break else: continue break suffix = file.split(".")[-1] if suffix == "csv": data_list = conversion.csv_to_list(file_path, line=line) elif suffix == "xlsx": data_list = conversion.excel_to_list(file_path, sheet=sheet, line=line) elif suffix == "json": data_list = conversion.json_to_list(file_path, key=key) elif suffix == "yaml": data_list = conversion.yaml_to_list(file_path, key=key) else: raise FileTypeError("Your file is not supported: {}".format(file)) return data(data_list)
from seldom.testdata.conversion import json_to_list import pytest from os.path import dirname, abspath case_path = dirname(abspath(__file__)) sys.path.insert(0, case_path) from element.baidu_element import BaiduElem @pytest.mark.parametrize("name, search_key", [ ("1", "Selenium"), ("2", "pytest文档"), ("3", "pytest-html"), ], ids=["case1", "case2", "case3"]) def test_baidu_search_param(name, search_key, page, base_url): page.goto(base_url) page.type(BaiduElem.search_input, search_key) page.click(BaiduElem.search_button) sleep(2) assert page.title() == search_key + "_百度搜索" @pytest.mark.parametrize("name, search_key", json_to_list(case_path + "/data/data_file.json")) def test_baidu_search_data_file(name, search_key, page, base_url): page.goto(base_url) page.type(BaiduElem.search_input, search_key) page.click(BaiduElem.search_button) sleep(2) assert page.title() == search_key + "_百度搜索"