How can I see print() statements in behave (BDD)
Asked Answered
G

8

58

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")
Gerundive answered 6/8, 2014 at 0:17 Comment(3)
check behave's command line arguments: pythonhosted.org/behave/behave.htmlBagpipes
Also u need to set junit to False, because in behave/configuration.py u can see - it stderr_capture, stdout_capture and log_capture to TrueQr
A better approach than using prints is to fail the test manually with something like: test.fail(f'Could not run test:{meaningful_data}')Indecision
G
40

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.

Gerundive answered 13/8, 2014 at 16:22 Comment(1)
It does not solve all. I made a test with three line printed in a step which is called multiple times during my tests. The 3 lines are correctly printed for the two firsts tests, then only the third line remains visible in the following tests.Grantinaid
O
52

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.

Ovoid answered 16/2, 2015 at 22:9 Comment(2)
I found that when I call it with behave I also need -s, along with --no-capture.Necrose
--no-capture was not enough. After reading @Louis answer below, I got it working by adding --no-capture --no-color.Cobblestone
H
43

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.

Heterosis answered 31/1, 2017 at 23:7 Comment(2)
the --no-color option what the key to me seeing output - I was stumped as to why I couldn't see any until I read this answerBayreuth
Me too, --no-capture --no-color for the win!Pili
G
40

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.

Gerundive answered 13/8, 2014 at 16:22 Comment(1)
It does not solve all. I made a test with three line printed in a step which is called multiple times during my tests. The 3 lines are correctly printed for the two firsts tests, then only the third line remains visible in the following tests.Grantinaid
V
5

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.
Valedictory answered 31/1, 2018 at 22:43 Comment(3)
clarification: how exactly did you set --no-capture ?Damascene
I don't set it, I use it as a parameter in the command line when I run behaveValedictory
To see the last print() statement I had to run print() statement in afer_step in environment.pyYak
D
4

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

print(my_variable, "\n")
Dooryard answered 18/8, 2021 at 10:9 Comment(0)
D
2

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.

Daughterinlaw answered 26/11, 2018 at 13:41 Comment(1)
I had followed the steps above but none of the print statements printed to the console. Adding an empty print statment fixed the issue. A bug in behave?Pyoid
B
1

one way: is to use --no-capture on cmd prompt

second way: create a behave.ini file in project directory. add following

[behave] stdout_capture = no

Bebop answered 22/8, 2022 at 23:20 Comment(0)
O
1

this works 100% !!! add behave.ini to project folder:

[behave]
color = no
stdout_capture = no
stderr_capture = no
log_capture = no

color and stdout_capture are the most critical to print out!

run as:

$ behave my.feature

OR not having behave.ini: run as:

$ behave --no-capture --no-color my.feature
Odontograph answered 19/9, 2022 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.