Setup and Teardown in Behave

This is used to define some required process before and after the code during our test execution.

In behave this is achieved by using the “environment.py” file. We need to define all the prerequisites in this file.So that while execution behave will process according to it.

Here “environment.py” filename is a constant name. We shouldn't change the name of a file. If we do it throws an error while execution.

Setup and Teardown

Below are the methods which we use in setup and teardown.

1. before_all : This method is used to execute any prerequisites code before the execution process starts. It takes one parameter as a “context” attribute.

2. after_all : This method is used to execute after the execution process completes.It takes one parameter as a “context” attribute.

3. before_tag : This Method is used to execute before the tag.It takes two parameters as a “context” and “tag”.

4. after_tag : This Method is used to execute after the tag.It takes two parameters as a “context” and “tag”.

5. before_feature : This Method is used to execute before the feature line.It takes two parameters as a “context” and “feature”.

6. after_feature : This Method is used to execute after the feature line.It takes two parameters as a “context” and “feature”.

7. before_scenario : This Method is used to execute before the scenario line.It takes two parameters as a “context” and “scenario”.

8. after_scenario : This Method is used to execute after the scenario line.It takes two parameters as a “context” and “scenario”.

9. before_step : This Method is used to execute before the step definition.It takes two parameters as a “context” and “step”.

10. after_step : This Method is used to execute after the step definition.It takes two parameters as a “context” and “step”.

environment.py

def before_all(context):
    print("1.1 Before all")


def after_all(context):
    print("1.2 After all")


def before_tag(context, tag):
    print("2.1 Before tag")


def after_tag(context, tag):
    print("2.2 After tag")


def before_feature(context, feature):
    print("3.1 Before feature")


def after_feature(context, feature):
    print("3.2 After feature")


def before_scenario(context, scenario):
    print("4.1 Before scenario")


def after_scenario(context, scenario):
    print("4.2 After scenario")


def before_step(context, step):
    print("5.1 Before step")


def after_step(context, step):
    print("5.2 After step")





loginScreen.feature

# This is a Feature file

Feature: Fill the Contact Form

    
    Scenario: User Login credentials

        Given Launch the App and Click on Login Button
        When Enter UserID
        When Enter password
        Then Verify Home Screen

Login.py

from behave import given, when, then


@given("Launch the App and Click on Login Button")
def methodOne(context):
    print("L1 - Launching the App")


@when("Enter UserID")
def methodTwo(context):
    print("L2 - Enter the UserID in Login Screen")


@when("Enter password")
def methodThree(context):
    print("L3 - Enter the Password in Login Screen")


@then("Verify Home Screen")
def methodFour(context):
    print("L4 - Home Screen Opened")