How to see exactly what went wrong in Behave

Don't forget you can always add an info message to an assert statement. For example:

assert output is expected, f'{output} is not {expected}'

Instead of using "raw assert" statements like in your example above, you can use another assertion provider, like PyHamcrest, who will provide you with desired details. It will show you what went wrong, like:

# -- file:features/steps/my_steps.py
from hamcrest import assert_that, equal_to
...
    assert_that(context.response.status, equal_to(int(status)))

See also:

  • http://jenisys.github.io/behave.example/intro.html#select-an-assertation-matcher-library
  • https://github.com/jenisys/behave.example

According to https://pythonhosted.org/behave/tutorial.html?highlight=debug,and This implementation is working for me.

A “debug on error/failure” functionality can easily be provided, by using the after_step() hook. The debugger is started when a step fails.

It is in general a good idea to enable this functionality only when needed (in interactive mode). This is accomplished in this example by using an environment variable.

# -- FILE: features/environment.py
# USE: BEHAVE_DEBUG_ON_ERROR=yes     (to enable debug-on-error)
from distutils.util import strtobool as _bool
import os

BEHAVE_DEBUG_ON_ERROR = _bool(os.environ.get("BEHAVE_DEBUG_ON_ERROR", "no"))

def after_step(context, step):
    if BEHAVE_DEBUG_ON_ERROR and step.status == "failed":
        # -- ENTER DEBUGGER: Zoom in on failure location.
        # NOTE: Use IPython debugger, same for pdb (basic python debugger).
        import ipdb
        ipdb.post_mortem(step.exc_traceback)