Behave Example with Allure Report

Now Let us create a feature file along with the steps package which has a python file of step definitions data and implement allure report logs for the test methods.

  • Create a Feature file with two scenarios
  • Now create a steps package and create two python files which have step definitions methods of the scenarios in the feature file along with allure log method as allureLogs(text).
  • In one scenario we are failing one test case to show in the report.

Allure Logs

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


    Scenario: Teacher Login credentials

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



Student_Login.py

import allure
from behave import given, when, then


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


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


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


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


def allureLogs(text):
    with allure.step(text):
        pass



Teacher_Login.py

import allure
from behave import given, when, then


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


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


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


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

def allureLogs(text):
    with allure.step(text):
        pass





Code Explanation

1. First we need to create feature file loginScreen.feature

2. Now create a steps package along with two python files in it as Student_Login.py and Teacher_Login.py

3. Import the allure and behave modules in python files.

4. Create the step definition for the scenarios which are defined in the feature file.

5. Create a method to save steps in allure report

Create a user defined method as allureLogs() which takes a string argument.

def allureLogs(text):
   with allure.step(text):
       Pass

Now, We can use this method in python test methods to save the logs in allure reports.

allureLogs("Contact Us Form page opened")


Create a method to save screenshots

Create a method and add below allure attach method to it. This helps to take screenshots and attach to allure reports.

allure.attach(self.driver.get_screenshot_as_png(), name=text, attachment_type=AttachmentType.PNG)

Here self.driver is an appium or selenium driver object.Please look at the appium framework or selenium framework to use this screenshot method.