Debugging python-behave steps with Pycharm


I’m using Pycharm to write tests and running them with behave. I’m running the behave commands with cli. To write the features and scenarios i’m using Pycharm. How can i debug each step?

Asked By: Shizzle



You need Pycharm Professional to easily setup debuging. Just create run/debug configuration, choose behave framework, then specify feature files folder and behave params.

Screenshot of the valid configuration

Otherwise, if you doesn’t have PyCharm Professional, you can create just basic python configuration, specify module behave and enter path to your feature folders in parameters.

Screenshot of the valid configuration

Answered By: rasklaad

If you don’t have PyCharm proffesional and you want to launch behave from commands, you can resort to the well-known technique of placing prints with debug information wherever you think necessary to help you solve possible errors.

For these prints to be shown in the console, you must launch the behave command with the –no-capture option. An example would be:

  • features/test.feature
Feature: Test

  Scenario: Scenario title
    Given This is one step
  • steps/
from behave import *

@given("This is one step")
def step_impl(context):
    print("I'm executing this code??")

@given("this is other setp")
def step_impl(context):
    print("or I'm executing this other code??")

  • ouptut of behave --no-capture features/test.feature
$ behave --no-capture features/test.feature
Feature: Test # features/test.feature:1

  Scenario: Scenario title  # features/test.feature:3
    Given This is one step  # steps/
I'm executing this code??

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
1 step passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

As you can see, the print tells you exactly which step you are running. With this technique you can debug your code by printing variable values or viewing the execution flow of your code.

It is possible to step-by-step debug from the console.
call the function breakpoint() anywhere you want the test to stop, then launch the tests with --no-capture flag.

Once the execution reaches the breakpoint you will have a Pdb console ready to receive commands.

Answered By: JoulinRouge
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.