简体   繁体   中英

How to use the test data from conftest.py in the test suite

I'm writing an automation test suite in pytest framework. I'm adding a pytest fixture function in Conftest.py for test data importing (this fixture imports data from excel and generates me the test data). How to use the test data from conftest.py in the test suite.

( Note : The Test Suite written is more of a pseudocode and not the actual code. Since my emphasis is more on the test parameterization & the data kindly ignore the test suite code. ).

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 Content

在此处输入图像描述

You need to add name of function from conftest.py to your case directly in suite Example of suite:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM