How can I see print() statements in behave (BDD)

PythonBddPython Behave

Python Problem Overview


Context: I am using Python with Behave (BDD).

Whether I run my tests from the command line (behave) or from a custom main(), the behavior is the same: the test runs and the only output that I see in the console is the standard BDD report.

My tests include print() statements that help me debug my code. However, none of these print statements are being displayed in the console output when I run behave.

Is there any way we can have "behave" display the print statements in our code?

My Main()

config = Configuration()
if not config.format:
    default_format = config.defaults["default_format"]
    config.format = [ default_format ]
    config.verbose = True
r = runner.Runner(config)
r.run()

if config.show_snippets and r.undefined_steps:
    print_undefined_step_snippets(r.undefined_steps)

My test.feature file:

Feature: My test feature with the Behave BDD
	Scenario: A simple test
	Given you are happy
	When someone says hi
	Then you smile

My test_steps.py file:

from behave import given, when, then, step, model

@given('you are happy')
def step_impl(context):
    pass

@when ('someone says {s}')
def step_impl(context, s):
    context.message = s
    print("THIS IS NEVER DISPLAYED IN THE CONSOLE")
    pass

@then ('you smile')
def step_impl(context):
        assert(context.message == "hi")

Python Solutions


Solution 1 - Python

from command line, you can use the following:

--no-capture for any stdout output to be printed immediately.

--no-capture-stderr for any stderr output to be printed immediately.

Solution 2 - Python

The first thing to do is to prevent capture of stdout (and maybe also stderr) as explained by Xuan or Ben.

However, there's a further complication that will stump people who are not aware of it. By default, behave outputs its report in color. This is problematic because the way it works is that when it runs a step, it first prints out the line of the step in a neutral color that indicates it does not yet know whether the step has passed or not. Once the step has finished, it uses escape codes to overwrite the previous line with a new color. If you don't do something to work around it, behave may simply overwrite what your print statement produced, and it may be difficult to figure out what happened.

In the following illustrations, I'm going to put the color in brackets at the end of the line. If you do not use print, the step "do something" would appear like this, before it is executed:

When do something [gray]

And once executed it would be replaced with a green line:

When do something [green]

behave outputs an escape sequence that makes the terminal go up and overwrite the line with a new color. No problem there.

If you put print "foo" in your step, the terminal would contain this, just before the step is completed:

When do something [gray]
foo

And then when the step completes successfully this is what you'd see on the terminal:

When do something [gray]
When do something [green]

The same escape sequence has caused behave to overwrite the output produced by the print statement.

I've used two methods to work around the issue in addition to turning off stdout capture:

  1. Use the --no-color option. This turns off the escape sequences and your print statements should produce visible output.

  2. Add a few extra newlines at the end of a print. So print "foo\n\n", for instance. behave will overwrite a useless blank line instead of overwriting the information you want. This is what I end up doing most often because I never invoke behave directly and adding a single additional option to behave's invocation, or editing a settings file is more cumbersome than just adding a few newlines to print.

Solution 3 - Python

I figured it out after spending more time reading the documentation. It is actually quite simple. By default, behave does not display any output (i.e. by using print()) unless there is a failure in the test. To force displaying all output regardless of the outcome of the test (pass/fail), all you need is to change some of the default settings. The easiest way to achieve that is to create a file named behave.ini in the root of your project's directory and put the following:

Filename: behave.ini

[behave]
stderr_capture=False
stdout_capture=False

Next time you run your behave tests, you will see all outputs from your debug statements whether your tests pass or fail.

Solution 4 - Python

behave.ini doesn't work for me.
I choose to

  • use the --no-capture argument to see print() statements in the console (though the last print() never going to appear, and I don't know why), or
  • redirect the output to a file so I can see all the stdout lines there to debug.

Solution 5 - Python

One other way to view the last line of stdout/stderr even when using coloured output with --no-capture; place the following in your environment.py:

def after_step(context, step):
	print()

That way the empty line gets eaten, not the one you actually want to see.

Solution 6 - Python

My problem was that the lines got eaten. This worked (hack).

print(my_variable, "\n")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - PythonXuanView Answer on Stackoverflow
Solution 2 - PythonLouisView Answer on Stackoverflow
Solution 3 - PythonBenView Answer on Stackoverflow
Solution 4 - PythonRuberzenView Answer on Stackoverflow
Solution 5 - PythonILikeFishView Answer on Stackoverflow
Solution 6 - PythonM3RSView Answer on Stackoverflow