1

Updated the question and code again to reflect recent changes. Amit I tried your suggestion and I encountered an error. Trying to get openpyxl to read date from each excel sheet in a workbook by passing a testname in, but it keep failing with can't find testname fixture. Can you advise where I went wrong? Thanks.

  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
Charlie Clark
  • 18,477
  • 4
  • 49
  • 55

1 Answers1

4

You can create another function(or fixture) in which you can read the excel & yield the dictionary elements. Then you can use @pytest.mark.parametrize to run a test for each element in the dictionary.

Here's a similar example where function returns rows from excel, then test is run for each row using @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
  • 19,780
  • 6
  • 46
  • 54
  • Thanks Amit. Where is this ExcelUtils.get_input_rows() coming from? It is an import from a python package or did you create your own ExcelUtils class and a method called get_input_rows? Can you explain a bit more? Thanks. – seleniumappiumnewbie Jun 01 '19 at 06:11
  • It is just a separate class & its method. You can use your ReadExcel class in a similar way. – Amit Jun 01 '19 at 12:33
  • Thanks Amit for your help and quick response. Have a good weekend! – seleniumappiumnewbie Jun 01 '19 at 16:37
  • Amit sorry to bother you again, but I encountered an error. It says it can't find fixture testName. I updated question and code again. Please see above. Thanks. – seleniumappiumnewbie Jun 01 '19 at 18:35
  • I think you don't need the param testName in `def get_excel_data(testName="")`. You can just have it as a function & pass it to parametrize. – Amit Jun 01 '19 at 18:38
  • I don't understand can you explain a bit more? Thanks. – seleniumappiumnewbie Jun 01 '19 at 18:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194297/discussion-between-amit-and-seleniumappiumnewbie). – Amit Jun 01 '19 at 19:00