Python Pytest 是否与 Java TestNG DataProvider 类似? Pytest 如何处理 Excel 数据?
Does Python Pytest have anything similiar to Java TestNG DataProvider? How does Pytest Handle Excel Data?
再次更新了问题和代码以反映最近的更改。 Amit 我尝试了您的建议,但遇到了错误。试图通过传入测试名称让 openpyxl 从工作簿中的每个 excel sheet 读取日期,但它一直失败,找不到测试名称夹具。你能告诉我哪里出错了吗?谢谢。
import pytest
from base.webdriverFactory import WebDriverFactory
from pages.webpages.login.login_page import LoginPage
from utilities.excel_utils import ExcelUtils as excelutils
@pytest.fixture(scope="class")
def get_excel_data(testName):
datafile = "../testdata/TestData.xlsx"
data = excelutils.get_input_rows(datafile, testName)
for row in data:
yield row
from base.webdriverFactory import WebDriverFactory
from pages.webpages.login.login_page import LoginPage
from utilities.teststatus import TestStatus
from utilities.read_excel import ReadExcel
import unittest, pytest
import utilities.custom_logger as cl
import logging
import inspect
@pytest.mark.usefixtures("setUp", "get_excel_data")
class LoginTests(unittest.TestCase):
log = cl.customLogger(logging.DEBUG)
data=""
@pytest.fixture(autouse=True)
def classSetup(self, setUp, get_excel_data):
self.wdf = WebDriverFactory(self.browser, self.os, self.thisdir)
self.lp = LoginPage(self.driver)
self.ts = TestStatus(self.driver)
self.ex = ReadExcel(self.driver)
data = self.get_excel_data(testName="test_invalidLoginWrongEmail")
@pytest.mark.run(order=1)
def test_invalidLoginBlank(self):
self.log.info("++++++++++++++++++++++++++++++++++++STARTING test_invalidLoginBlank++++++++++++++++++++++++++++++++++++")
self.lp.clickSignInLink()
self.lp.blankLogin()
result = self.lp.verifyBlankLogin()
assert result == True #Verify login failed is true
self.ts.markFinal("test_invalidLoginBlank", result, "Verify Can't Login with Blank Fields")
self.log.info("++++++++++++++++++++++++++++++++++++ENDING test_invalidLoginBlank++++++++++++++++++++++++++++++++++++")
@pytest.mark.run(order=2)
@pytest.mark.parametrize("email, password", data)
def test_invalidLoginWrongEmail(self, email, password):
self.log.info("++++++++++++++++++++++++++++++++++++STARTING test_invalidLoginWrongEmail++++++++++++++++++++++++++++++++++++")
self.lp.wrongLogin(email, password)
browser1 = self.wdf.browser
result = self.lp.verifyWrongLogin(testbrowser=browser1)
assert result == True # Verify login failed is True
self.ts.markFinal("test_invalidLoginWrongEmail", result, "Verify Can't Login with Wrong Email")
self.log.info("++++++++++++++++++++++++++++++++++++ENDING test_invalidLoginWrongEmail++++++++++++++++++++++++++++++++++++")
from openpyxl import load_workbook
class ExcelUtils():
def __init__(self, driver):
self.driver = driver
def get_input_rows(self, datafile, testName):
wb = load_workbook(filename=datafile)
sheet = wb.active
# List sheets available
sheets = wb.get_sheet_names()
for i in sheets:
if i == testName:
sheet = i
self.log.info("Got active sheet.")
row_list = []
# Get second row as the headers' values
rows2 = sheet.iter_rows(min_row=2, max_row=2)
secondrowvalues = next(rows2)
for d in secondrowvalues:
row_list.append(d.value)
return row_list
您可以创建另一个函数(或夹具),您可以在其中读取 excel 并生成字典元素。然后你可以使用 @pytest.mark.parametrize
到 运行 对字典中的每个元素进行测试。
这是一个类似的示例,其中函数 returns 来自 excel 的行,然后使用 @pytest.mark.parametrize
对每一行进行测试 运行
def get_data():
data = ExcelUtils("inputData.xlsx", "SheetName").get_input_rows()
for row in data:
yield row
@pytest.mark.parametrize("test_input", get_data())
def test_for_all_input_rows(test_input):
print(test_input)
再次更新了问题和代码以反映最近的更改。 Amit 我尝试了您的建议,但遇到了错误。试图通过传入测试名称让 openpyxl 从工作簿中的每个 excel sheet 读取日期,但它一直失败,找不到测试名称夹具。你能告诉我哪里出错了吗?谢谢。
import pytest
from base.webdriverFactory import WebDriverFactory
from pages.webpages.login.login_page import LoginPage
from utilities.excel_utils import ExcelUtils as excelutils
@pytest.fixture(scope="class")
def get_excel_data(testName):
datafile = "../testdata/TestData.xlsx"
data = excelutils.get_input_rows(datafile, testName)
for row in data:
yield row
from base.webdriverFactory import WebDriverFactory
from pages.webpages.login.login_page import LoginPage
from utilities.teststatus import TestStatus
from utilities.read_excel import ReadExcel
import unittest, pytest
import utilities.custom_logger as cl
import logging
import inspect
@pytest.mark.usefixtures("setUp", "get_excel_data")
class LoginTests(unittest.TestCase):
log = cl.customLogger(logging.DEBUG)
data=""
@pytest.fixture(autouse=True)
def classSetup(self, setUp, get_excel_data):
self.wdf = WebDriverFactory(self.browser, self.os, self.thisdir)
self.lp = LoginPage(self.driver)
self.ts = TestStatus(self.driver)
self.ex = ReadExcel(self.driver)
data = self.get_excel_data(testName="test_invalidLoginWrongEmail")
@pytest.mark.run(order=1)
def test_invalidLoginBlank(self):
self.log.info("++++++++++++++++++++++++++++++++++++STARTING test_invalidLoginBlank++++++++++++++++++++++++++++++++++++")
self.lp.clickSignInLink()
self.lp.blankLogin()
result = self.lp.verifyBlankLogin()
assert result == True #Verify login failed is true
self.ts.markFinal("test_invalidLoginBlank", result, "Verify Can't Login with Blank Fields")
self.log.info("++++++++++++++++++++++++++++++++++++ENDING test_invalidLoginBlank++++++++++++++++++++++++++++++++++++")
@pytest.mark.run(order=2)
@pytest.mark.parametrize("email, password", data)
def test_invalidLoginWrongEmail(self, email, password):
self.log.info("++++++++++++++++++++++++++++++++++++STARTING test_invalidLoginWrongEmail++++++++++++++++++++++++++++++++++++")
self.lp.wrongLogin(email, password)
browser1 = self.wdf.browser
result = self.lp.verifyWrongLogin(testbrowser=browser1)
assert result == True # Verify login failed is True
self.ts.markFinal("test_invalidLoginWrongEmail", result, "Verify Can't Login with Wrong Email")
self.log.info("++++++++++++++++++++++++++++++++++++ENDING test_invalidLoginWrongEmail++++++++++++++++++++++++++++++++++++")
from openpyxl import load_workbook
class ExcelUtils():
def __init__(self, driver):
self.driver = driver
def get_input_rows(self, datafile, testName):
wb = load_workbook(filename=datafile)
sheet = wb.active
# List sheets available
sheets = wb.get_sheet_names()
for i in sheets:
if i == testName:
sheet = i
self.log.info("Got active sheet.")
row_list = []
# Get second row as the headers' values
rows2 = sheet.iter_rows(min_row=2, max_row=2)
secondrowvalues = next(rows2)
for d in secondrowvalues:
row_list.append(d.value)
return row_list
您可以创建另一个函数(或夹具),您可以在其中读取 excel 并生成字典元素。然后你可以使用 @pytest.mark.parametrize
到 运行 对字典中的每个元素进行测试。
这是一个类似的示例,其中函数 returns 来自 excel 的行,然后使用 @pytest.mark.parametrize
def get_data():
data = ExcelUtils("inputData.xlsx", "SheetName").get_input_rows()
for row in data:
yield row
@pytest.mark.parametrize("test_input", get_data())
def test_for_all_input_rows(test_input):
print(test_input)