【发布时间】:2019-08-06 21:01:24
【问题描述】:
我有一个 Python Selenium 测试,使用 at pytest 夹具将登录用户名和密码从电子表格传递到我的测试中。我正在使用 openpyxl 来实现这一点。
我的测试如下:
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 行中的“数据”
【问题讨论】:
-
您的测试函数需要将
data作为其参数之一。 -
@CharlieClark 感谢您的回答。我不是添加 pytest 夹具,从 Datagen 获取数据,然后将其传递到输入用户名和密码行吗?
-
你需要
def test_new(data):
标签: python selenium-webdriver pytest openpyxl data-driven-tests