简体   繁体   中英

Unable to run PyTest-bdd step definition file as it throws index out of range error

Feature file is as below

Feature: Nopcommerce Login

Scenario: login to nopcommerce website

Given nopcommerce page is displayed
When user enters username as admin@yourstore.com
When user enters password as admin
Then user is able to login to nocpmmerce website

step definition python file is as below

from pytest_bdd import scenarios, given, when, then 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pytest

scenarios('../features/NopcommerceLogin.feature')

@pytest.fixture()
def browser():
    driver = webdriver.Safari()
    yield driver
    driver.quit()

@given("nopcommerce page is displayed")
def webpage(browser):
    browser.get("http://admin-demo.nopcommerce.com")

@when("user enters username as admin@yourstore.com")
def enter_uname(browser):
    browser.find_element_by_id("Email").send_keys("admin@yourstore.com")

@when("user enters password as admin")
def enter_pwd(browser):
    browser.find_element_by_id("Password").send_keys("admin")
    browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div/div[2]/div[1]/div/form/div[3]/input").click()

@then("user is able to login to nocpmmerce website")
def loginsuccess(browser):
    assert browser.current_url == "https://admin-demo.nopcommerce.com/admin/"

when the step_def file is run, the following error message is displayed

Traceback (most recent call last):

File "~/tests/step_defs/test_NopcommerceLogin.py", line 6, in scenarios('../features/NopcommerceLogin.feature')

File "~/venv/lib/python3.8/site-packages/pytest_bdd/scenario.py", line 343, in scenarios features_base_dir = get_features_base_dir(module)

File "~/venv/lib/python3.8/site-packages/pytest_bdd/scenario.py", line 295, in get_features_base_dir return get_from_ini('bdd_features_base_dir', default_base_dir)

File "~/venv/lib/python3.8/site-packages/pytest_bdd/scenario.py", line 303, in get_from_ini config = CONFIG_STACK[-1]

IndexError: list index out of range

It's not entirely the solution, and I have the same issue in PycharmIDE, but I suggest using terminal and start tests like:

pytest <test_fily.py>

for IDEA solution, still working on it

I guess the problem is that the pytest is not identifying any executable pytest method's in your step definition file. Please try changing the "scenarios" to "scenario" and add a pytest identifiable method below the same

@scenario('../features/NopcommerceLogin.feature')
def test_login():
      pass

This approach always works for me and is based on Pytest-BDD doc.

Change your configurations to use pytest configuration: screenshot

or try running using terminal : pipenv run python -m pytest

Make sure that pytest configured in pycham Enable Pytest for your project

Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework. In the Default test runner field select pytest. Click OK to save the settings

@Vova I found that in the run configuration, the Working directory was incorrectly set to the directory where the steps python file was, instead of the project root directory. Fixing that made the test run successfully in PyCharm.

I had this issue - it was simply alignment in the feature file, I had a space on the Scenario definition after 'Scenario' and before the colon. When I removed - the error no longer occured.

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