pythonselenium-webdriverpytestopenpyxldata-driven-tests

Pytest data fixture causing function uses no argument 'data' error


I have a Python Selenium testing using at pytest fixture to pass login username and password from a spreadsheet into my test. I am using openpyxl to achieve this.

My test is as follows:

from Base import InitiateDriver
from Pages import LogIn
import pytest
from DataGenerate import DataGen
import openpyxl

@pytest.mark.parametrize('data', DataGen.data_generator())
def test_new():
  browser = InitiateDriver.start_browser()
  login = LogIn.LogInClass(browser)
  login.enter_username(data[0])
  login.enter_password(data[1])

I have a DataGen.py file to get the test data from the spreadsheet here:

import openpyxl

def data_generator():
  wk = openpyxl.load_workbook("/users/marklane/Automation/TestData.xlsx")
  sh = wk['login']
  r = sh.max_row
  li = []
  li1 = []
  for i in range(1, r + 1):
    li1 = []
    un = sh.cell(i, 1)
    up = sh.cell(i, 2)
    li1.insert(0, un.value)
    li1.insert(1, up.value)
    li.insert(i - 1, li1)

    print (li)
    return li

when running the test I get the following error:

latform darwin -- Python 2.7.10, pytest-4.3.0, py-1.8.0, pluggy-0.8.1
rootdir: /Users/marklane/PycharmProjects/test, inifile:
Tests/test_TC_001_ValidateRegistration.py:None 
(Tests/test_TC_001_ValidateRegistration.py)
In test_new: function uses no argument 'data'
collected 0 items / 1 errors

==================================== ERRORS ====================================
__________ ERROR collecting Tests/test_TC_001_ValidateRegistration.py __________

In test_new: function uses no argument 'data'------------------------------- Captured stdout ----------------------- ---------[[u'marklane2001', u'Ipswichtow1!']]
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!

As you can see from the error, the data is being found in the spreadsheet but it is not passing that back to 'data' in my selenium lines


Solution

  • You must pass a parameter to the test itself so that you can use it.

    @pytest.mark.parametrize('data', DataGen.data_generator())
    def test_new(data):
      browser = InitiateDriver.start_browser()
      login = LogIn.LogInClass(browser)
      login.enter_username(data[0])
      login.enter_password(data[1])