JayadevBS
JayadevBS

Reputation: 23

Scenario in BDD(behave) can have without 'Given'?

Can we write 'Scenario' in behave without 'Given' and directly start with 'When' ?

More description:

'Background' section is already in the state what it requires for test scenario. Hence, I just wanted start directly with 'When' and perform some actions.

Upvotes: 3

Views: 1583

Answers (1)

Verv
Verv

Reputation: 2473

Simply, yes. As seen in this example:

Feature: Testing Feature Without Given

    Scenario: No given step

        When we have no given step
        Then our test should still work

# coding: utf-8

from behave import *

@when("we have no given step")
def step_impl(context):

    pass

@then("our test should still work")
def step_impl(context):

    pass

Feature: Testing Feature Without Given # test.feature:1

  Scenario: No given step           # test.feature:3
    When we have no given step      # steps\step.py:5
    Then our test should still work # steps\step.py:10

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
2 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.002s

However, this may not be the best practice. A test case implies a condition and an expected result. The background isn't meant to cover this condition, but rather broader pre-conditions such as environment setup or obvious steps like opening a browser to test a web app.

Upvotes: 4

Related Questions