chmp/ipytest

Cannot find @given for pytest-bdd

sunbiz opened this issue · 2 comments

When executing pytest_bdd tests, the definitions are not found. I have tried both the ipytest.run() as well as %%run_pytest[clean] but the step definitions are not identified in the file.

pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found:

The following is a straightforward Gherkin feature file:

Feature: DuckDuckGo Web Browsing

  Background:
    Given the DuckDuckGo home page is displayed

  Scenario: Basic DuckDuckGo Search
    When the user searches for "panda"
    Then results are shown for "panda"

and the steps code:

import pytest_bdd
import pytest
from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True

DUCKDUCKGO_HOME = 'https://duckduckgo.com/'

@pytest.fixture
def browser():
  driver = Firefox(options=options)
  driver.implicitly_wait(10)
  yield driver
  driver.quit()

@pytest_bdd.scenario('websearch.feature','Basic DuckDuckGo Search', features_base_dir='', strict_gherkin=False)
def test_websearch():
    pass

@pytest_bdd.given('the DuckDuckGo home page is displayed')
def ddg_home(browser):
    browser.get(DUCKDUCKGO_HOME)
 
@pytest_bdd.when(pytest_bdd.parsers.parse('the user searches for "{phrase}"'))
def search_phrase(browser, phrase):
    search_input = browser.find_element_by_id('search_form_input_homepage')
    search_input.send_keys(phrase + Keys.RETURN)
 
@pytest_bdd.then(pytest_bdd.parsers.parse('results are shown for "{phrase}"'))
def search_results(browser, phrase):
    links_div = browser.find_element_by_id('links')
    assert len(links_div.find_elements_by_xpath('//div')) > 0
    search_input = browser.find_element_by_id('search_form_input')
    assert search_input.get_attribute('value') == "phrase"

On the other hand, running these from the command-line using pytest works perfectly fine. Has anyone experienced this before?

chmp commented

Thanks for the report. Since I am not familiar with pytest-bdd it's hard for me to follow up.

Your example involves multiple dependencies, including selenium, and various test-cases. Would it be possible to post a minmal example with as little code as possible and only involvling pytest, pytest-bdd and ipytest?

chmp commented

Closing for now. Please feel free to re-open the issue with a minimal example.