本文目的:从excel中获取内容,并自动生成.py case文件,然后基于pytest模块自动运行case
1.使用python解析excel,获取case number和case执行步骤
import os from openpyxl import load_workbook # CaseList = [] # CaseProcedure = [] # CaseDict = {} # SheetAddress = '' def GetCaseInfor(PYList, Address,Dict): Path = os.getcwd() FileList = os.listdir(Path) for file in FileList: if os.path.splitext(file)[1] == ".py" and 'TS' in os.path.splitext(file)[0]: PYList.append(file) # print(PYList) if os.path.splitext(file)[1] == ".xlsx": Address = str(os.path.abspath(file)) # print(Address) wb = load_workbook(Address) ws = wb.active CN = ws.max_column + 1 CR = ws.max_row + 1 for i in range(1, CR): CaseNO = str(ws.cell(i, 1).value) if CaseNO.startswith('TS'): # Case.append(CaseNO) CaseStep = ws.cell(i, 2).value # Procedure.append(CaseStep) Dict[CaseNO] = CaseStep i = i + 1
2.获取解析内容,自动生成运行case的.py文件,注:文件以test_开头,便于pytest moudle识别到运行文件,并执行
from CaseLib import GetCaseInform import os from openpyxl import load_workbook CaseList = [] CaseProcedure = [] CaseDict = {} SheetAddress = '' GetCaseInform.GetCaseInfor(CaseList, SheetAddress, CaseDict) for n in CaseDict.keys(): filename = 'test'+'_'+str(n)+'.py' filecontent=CaseDict.get(n) fileline=filecontent.split('\n') # 打开文件进行写入,如果文件不存在则创建 with open(filename, 'w') as file: # 写入Python代码 file.write("from CaseFunction import * \n") file.write("def test_RunTestCase(): \n") for line in fileline: line=' '+line+'\n' file.write(line)
3.导入pytest moudle自动运行case
C:\Users\uic75642\AppData\Local\Programs\Python\Python310\python.exe D:/Study/Pytest/CaseLib/test_case/RunCase.py
============================= test session starts =============================
platform win32 -- Python 3.10.0a3, pytest-8.2.0, pluggy-1.5.0 -- C:\Users\uic75642\AppData\Local\Programs\Python\Python310\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.10.0a3', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '8.2.0', 'pluggy': '1.5.0'}, 'Plugins': {'allure-pytest': '2.13.5', 'html': '4.1.1', 'metadata': '3.1.1', 'ordering': '0.6', 'rerunfailures': '14.0', 'xdist': '3.5.0'}}
rootdir: D:\Study\Pytest\CaseLib
configfile: pytest.ini
plugins: allure-pytest-2.13.5, html-4.1.1, metadata-3.1.1, ordering-0.6, rerunfailures-14.0, xdist-3.5.0
collecting ... collected 2 items
test_TS_1599.py::test_RunTestCase please input your nameLIN
please input your password123321
Hello
PASSED
test_TS_1600.py::test_RunTestCase please input your nameLIN
please input your password123321
Hello
PASSED
============================== warnings summary ===============================
C:\Users\uic75642\AppData\Local\Programs\Python\Python310\lib\site-packages\_pytest\config\__init__.py:1448
C:\Users\uic75642\AppData\Local\Programs\Python\Python310\lib\site-packages\_pytest\config\__init__.py:1448: PytestConfigWarning: Unknown config option: python_function
self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================== 2 passed, 1 warning in 21.64s ========================
Process finished with exit code 0