Python Pytest数据固定装置导致函数不使用参数';数据';错误
我有一个PythonSelenium测试,它使用PythonTestFixture将电子表格中的登录用户名和密码传递到我的测试中。我正在使用openpyxl来实现这一点 我的测试如下:Python Pytest数据固定装置导致函数不使用参数';数据';错误,python,selenium-webdriver,pytest,openpyxl,data-driven-tests,Python,Selenium Webdriver,Pytest,Openpyxl,Data Driven Tests,我有一个PythonSelenium测试,它使用PythonTestFixture将电子表格中的登录用户名和密码传递到我的测试中。我正在使用openpyxl来实现这一点 我的测试如下: from Base import InitiateDriver from Pages import LogIn import pytest from DataGenerate import DataGen import openpyxl @pytest.mark.parametrize('data', Dat
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])
我有一个DataGen.py文件从电子表格中获取测试数据:
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
在运行测试时,我遇到以下错误:
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 !!!!!!!!!
从错误中可以看出,在电子表格中找到了数据,但它没有将数据传递回我的selenium行中的“数据”您必须将参数传递给测试本身,以便使用它
@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])
必须将参数传递给测试本身,以便使用它
@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])
测试函数需要有
数据
作为其参数之一。@charlectorlak感谢您的回答。难道我不需要添加pytest fixture,从Datagen获取数据,然后将其传递到输入用户名和密码行吗?您需要def test\u new(data):
您的测试函数需要data
作为其参数之一。@charlechlark感谢您的回答。难道我不需要添加pytest夹具,从Datagen获取数据,然后将其传递到输入用户名和密码行吗?您需要def test\u new(data):
对不起,我更正了我的答案并添加了解释。希望对您有所帮助。对不起,我更正了我的答案并添加了解释。希望对你有所帮助。