简体   繁体   English

如何阻止 pytest_bdd 在 Gherkin 场景大纲的每次迭代后执行拆卸步骤?

[英]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:和各自的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:在我的conftest.py中,我分别在启动/拆卸时执行登录和注销:

@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?有什么方法可以表明仅针对此测试,此大纲中的所有场景都应该在没有拆解的情况下执行吗?

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. Cucumber 和其他测试框架致力于隔离每个单独的测试/场景,以防止一个测试/场景破坏其他测试/场景的副作用。 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 eg然后一步测试它们并摆脱你的场景大纲,例如

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.注意:示例在 ruby (ish) 中,但无论使用哪种语言,原则都适用。

In general I would suggest avoiding scenario outlines, you really don't need to use them at all.一般来说,我会建议避免场景大纲,你真的根本不需要使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM