Behave - Runner Scripts



We can run a Behave test, by running the command line arguments, or we can create a runner script. This script gives the provision of running the test and generating the corresponding report.

We can do a re-try and execute the failed test. Also, before executing the entire suite, the runner script is capable of making an application programming interface (API) call and ensuring that there are no issues with the API.

Steps for Runner Script

Follow the steps given below to create and execute a runner script successfully in Behave.

Step 1 − Create a runner script (runner.py) within the features folder.

The following screen will appear on your computer −

Steps for Runner Script

Step 2 − Runner Script Implementation to run tests

The runner script can be implemented to run the tests by using the below mentioned code −

import subprocess
if __name__ == '__main__':
#command line args along with error capture on failure with check true
      s = subprocess.run('behave --no-capture',shell=True, check=True)

Step 3 − Execute the runner script

Execute runner.py file with command py runner.py. The following screen will appear on your computer:

(myenv) D:\beautiful_soup\myenv\pythonProject\features>py runner.py
USING RUNNER: behave.runner:Runner
Feature: Multi-Methods # payment.feature:1

  Scenario: Purchase             # payment.feature:2
    Given User is on shop        # steps/stepImpPayment.py:11 0.000s
    When user purchases 3 shirts # steps/stepImpPayment.py:15
User purchased:
shirts
Count is:
    When user purchases 3 shirts # steps/stepImpPayment.py:15 0.001s
    And user purchases 4 pants   # steps/stepImpPayment.py:21
User purchased:
pants
Count is:
    And user purchases 4 pants   # steps/stepImpPayment.py:21 0.000s

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped
Took 0min 0.001s

Step 4 − Parametrise runner script by passing command line arguments.

The runner script implementation to run tests can be done as follows −

import argparse
import subprocess
if __name__ == '__main__':
   p = argparse.ArgumentParser()
  #--testdir command line argument added
   p.add_argument('--testdir', required=False, help="File path")
   a = p.parse_args()
   testdir = a.testdir
   #complete command
   c= f'behave --no-capture {testdir}'
   s = subprocess.run(c, shell=True, check=True)

Step 5 − Execute the runner script

Execute runner.py file with command python3 runner.py --testdir=features.

(myenv) D:\beautiful_soup\myenv\pythonProject>py features/runner.py --testdir=features
USING RUNNER: behave.runner:Runner
Feature: Multi-Methods # features/payment.feature:1

  Scenario: Purchase             # features/payment.feature:2
    Given User is on shop        # features/steps/stepImpPayment.py:11 0.000s
    When user purchases 3 shirts # features/steps/stepImpPayment.py:15
User purchased:
shirts
Count is:
    When user purchases 3 shirts # features/steps/stepImpPayment.py:15 0.000s
    And user purchases 4 pants   # features/steps/stepImpPayment.py:21
User purchased:
pants
Count is:
    And user purchases 4 pants   # features/steps/stepImpPayment.py:21 0.001s

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped
Took 0min 0.001s
Advertisements