【问题标题】:How to use the test data from conftest.py in the test suite如何在测试套件中使用来自 conftest.py 的测试数据
【发布时间】: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


【解决方案1】:

您需要在套件中直接将 conftest.py 中的函数名称添加到您的案例中 套件示例:

import pytest
class Test_main():
    def test_login(self,test_data):
        """in your case the dict will be better"""
        login.enter_portal_code(test_data["key"]) #by key in dict
        login.choose_DOB(test_data["key"]) #by key in dict
        login.enter_username(test_data["key"]) #by key in dict
        login.enter_pwd(test_data["key"]) #by key in dict
        login.click_login()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多