Debugging the Python behave test

1 minute read

When writing an end-to-end test using behave, debugging can be a little tricky. If you want to add a break point with pdb, you can provide a CLI flag behave --no-capture.

For example, I have a behave scenario, my scenario, in my test.feature file like this

Feeature: Test my scenario
  Scenario: my scenario
    Given I have a text file
    When I print "hello"
    Then I should get "hello" printed

where And step has a simple function to print hello and I want to add a break point for debugging.

from behave import when

@when("I print "{message}")
def print_hello(message):
    import pdb
    pdb.set_trace() # I want to add a break point in here (for whatever reason)
    print("hello")

Simply running behave -n "my scenario" won’t stop at the break point. Instead you have to add a flag --no-capture, which tells behave to stop capturing all ipdb stdout. So you run

behave -n "my scenario" --no-capture

which stops at pdb.set_trace() break point.

Subscribe to my blog

* indicates required
Thank you for reading my blog post! If you liked it, please subscribe to receive new posts. I won't give your address to anyone else, won't send you any spam, and you can unsubscribe at any time.