0

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?

LokiRagnarok
  • 220
  • 1
  • 10

1 Answers1

1

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.

diabolist
  • 3,990
  • 1
  • 11
  • 15
  • Hm, I notice I may have oversimplified. How would you deal with having to program a check not only for the one main page, but for multiple pages, each with their own set of expected links? Until now, I was doing it with a scenario outline which would iterate over two columns, visited_page and expected_link. – LokiRagnarok Dec 01 '22 at 08:03
  • I'd use exactly the same approach, but tidy up the Then to make a single call to initiate a bunch of checks. So your then would be something like `expect(check_pages).to be true`, and check pages would be a bit of Python code that can retrieve your page, expected_links hash and iterate over it visiting each page – diabolist Dec 01 '22 at 09:25