pythoncucumberpytestgherkinpytest-bdd

How to stop pytest_bdd from performing the teardown steps after each iteration of a Gherkin Scenario Outline?


I have the following Gherkin Scenario Outline:

Scenario: Links on main page
  When I visit the main page
  Then there is a link to "<site>" on the page

Examples:
  |site             |
  |example.com      |
  |stackoverflow.com|
  |nasa.gov         |

and the respective test.py:

from pytest_bdd import scenario, given, when, then

@scenario("test.feature", "Links on main page")
def test_links():
  pass

In my conftest.py, I perform a login and logout at startup/teardown respectively:

@pytest.fixture(autouse=True, scope="function")
def login_management(driver, page_url, logindata):
  login()
  yield
  logout()

However, I don't want the browser to log out and log in between checking every link - I would rather all the links were checked on one page visit. I also would prefer to keep this tabular syntax instead of writing a dozen of steps to the tune of

And there is a link to "example.com"
And there is a link to "stackoverflow.com"
And there is a link to "nasa.gov"

Is there any way to signal that for this test only, all of the scenarios in this outline should be performed without the teardown?


Solution

  • Scenario Outlines are just a compact way of writing several individual scenarios. Cucumber and other testing frameworks work on the idea of isolating each individual test/scenario to prevent side effects from one test/scenario breaking other test/scenarios. If you try an bypass this you can end up with a very flaky test suite that has occasional failures which are based on the order test/scenarios are run, rather than the test/scenario failing for a legitimate reason.

    So what you are trying to do breaks a fundamental precept of testing, and you really should avoid doing that.

    If you want to be more efficient testing your links, group them together and give them a name. Then test for them in a single step and get rid of your scenario outline e.g.

    Scenario: Main page links
      When I visit the main page
      Then I should see the main page links
    
    Then "I should see the main page links" do
      expect(page).to have_link("example.com")
      expect(page).to have_link("nasa.gov")
      ...
    end
    

    Now you have one simple scenario that will just login once and run much faster.

    NOTE: examples are in ruby (ish), but the principle applies no matter the language.

    In general I would suggest avoiding scenario outlines, you really don't need to use them at all.