【发布时间】:2020-07-16 23:20:33
【问题描述】:
我正在 pytest 框架中编写自动化测试套件。我在 Conftest.py 中添加了一个 pytest 夹具函数来导入测试数据(这个夹具从 excel 导入数据并生成测试数据)。如何在测试套件中使用来自 conftest.py 的测试数据。
(注意:编写的测试套件更多的是伪代码,而不是实际代码。由于我的重点更多地放在测试参数化和数据上,请忽略测试套件代码。 )。
Conftest.py
import pytest
import openpyxl as xl
@pytest.fixture
def test_data():
book = xl.load_workbook('../utils/testdata.xlsx', data_only=True)
sheet=book.get_sheet_by_name('Sheet1')
max_rows=sheet.max_row
max_columns=sheet.max_column
dataset=[]
temp=[]
for x in range(2,max_rows+1):
for y in range(1,max_columns+1):
val=sheet.cell(x,y)
temp.append(val.value)
dataset.append(temp)
temp=[]
yield dataset
Test_Suite:
import pytest
@pytest.mark.usefixtures('test_data')
class Test_main():
@pytest.mark.parametrize("portal_code,DOB,user_name,pwd",dataset) # dataset from conftest.py
def test_login(self,portal_code,DOB,user_name,pwd):
login.enter_portal_code(portal_code)
login.choose_DOB(DOB)
login.enter_username(user_name)
login.enter_pwd(pwd)
login.click_login()
Excel 内容
【问题讨论】:
-
我认为这是不可能的。要么将夹具添加为测试参数并在测试中对其进行迭代,要么全局加载测试数据,以便在加载时可以将其传递给
parametrize。
标签: python selenium-webdriver pytest