allure-python icon indicating copy to clipboard operation
allure-python copied to clipboard

Add stdout and stderror capturing with python behave

Open Alpherie opened this issue 5 years ago • 11 comments

I'm submitting a ...

  • [ ] bug report
  • [x] feature request

What is the current behavior?

Output from stdout and stderror is not captured for the report when using allure-behave.

What is the expected behavior?

Stdout and stderror output should be available when viewing Overview for the test.

What is the motivation / use case for changing the behavior?

To have an easy way to see the test-related output for future debugging/bug reporting/etc.

Please tell us about your environment:

Alpherie avatar Jun 25 '19 20:06 Alpherie

Any update?

Ljancek avatar Jul 13 '19 06:07 Ljancek

Something news?

Ljancek avatar Aug 30 '19 12:08 Ljancek

Any update? @sseliverstov

Ljancek avatar Feb 11 '20 07:02 Ljancek

??

demetrissss avatar May 14 '20 10:05 demetrissss

any progress??

vishuhanda avatar May 02 '21 20:05 vishuhanda

@Alpherie @Ljancek @demetrissss @vishuhanda you can add a hook to the environment.py like this:

import allure


def after_scenario(context, scenario):
    stdout = context.stdout_capture.getvalue()
    stderr = context.stderr_capture.getvalue()
    if stdout:
        allure.attach(stdout, name="stdout", attachment_type=allure.attachment_type.TEXT)
    if stderr:
        allure.attach(stderr, name="stderr", attachment_type=allure.attachment_type.TEXT)

sseliverstov avatar Jun 02 '21 21:06 sseliverstov

Please do not close this issue. Probably in the next versions I will add captured automaticaly

sseliverstov avatar Jun 02 '21 21:06 sseliverstov

@sseliverstov thank you, it worked!

Alpherie avatar Oct 07 '22 09:10 Alpherie

@Alpherie @Ljancek @demetrissss @vishuhanda you can add a hook to the environment.py like this:

import allure


def after_scenario(context, scenario):
    stdout = context.stdout_capture.getvalue()
    stderr = context.stderr_capture.getvalue()
    if stdout:
        allure.attach(stdout, name="stdout", attachment_type=allure.attachment_type.TEXT)
    if stderr:
        allure.attach(stderr, name="stderr", attachment_type=allure.attachment_type.TEXT)

I don't seem to get this to work..

Where are these values set:

context.stdout_capture context.stderr_capture

Are they just directly accessible through context? I get an error when I copy this code directly:

HOOK-ERROR in after_scenario: AttributeError: 'NoneType' object has no attribute 'getvalue'

TurboCoder13 avatar Jan 06 '23 13:01 TurboCoder13

@eitel13 Are they just directly accessible through context? I get an error when I copy this code directly:

They should be. How are you launching behave? I use formatter -f allure_behave.formatter:AllureFormatter

Alpherie avatar May 19 '23 12:05 Alpherie

@Alpherie

How are you launching behave? I use formatter

I like to run tests with the IDE UI and unfortunately, there's things that just "don't work" with Behave and the IDE UI (eg: PyCharm Behave does not support "--junit" parameter).

I think this is one of them.

This is what the code looks like:

def after_scenario(context, scenario):
    try:
        stdout = context.stdout_capture.getvalue()
        stderr = context.stderr_capture.getvalue()

        print(f"stdout: {stdout}")
        print(f"stderr: {stderr}")

        if stdout:
            allure.attach(
                stdout,
                name="stdout",
                attachment_type=allure.attachment_type.TEXT,
            )
        if stderr:
            allure.attach(
                stderr,
                name="stderr",
                attachment_type=allure.attachment_type.TEXT,
            )

    except AttributeError:
        print("No stdout/stderr captured")
    else:
        print("stdout/stderr captured")

If I run through terminal, then I get:

stdout:

stderr:

stdout/stderr captured

Running through UI, the AttributeError is raised. 🙃

TurboCoder13 avatar May 22 '23 08:05 TurboCoder13