Riccardo79
Riccardo79

Reputation: 1046

Get dynamic test description in allure-pytest

in pytest-allure is there a way to get the current test description after I set it with

allure.dynamic.description("""blah blah""")

Looking for something like:

description = pytest.xxx or request.node.xxx ....

I'm trying to explain better what I need. Every test calls a function before closing and it needs to know the value of testdescription. I do not want to save it in a variable and pass to the function. I would get it through allure variable.

@allure.title("MYTITLE")
def test_A1(self):
    allure.dynamic.description("""MYDESC""")
    ...
    myfunct()


def myfunct():
    testdescription = ???
    ...
    message: "Test done " + testdescription  
    smtpObj.sendmail(sender, receivers, message)      

Upvotes: 2

Views: 2452

Answers (1)

hoefling
hoefling

Reputation: 66231

Looking into allure_pytest plugin sources, you can grab the correct plugin object from the plugin manager that stores the infos:

import allure
from allure_commons._core import plugin_manager
from allure_pytest.listener import AllureListener


@allure.title("MYTITLE")
def test_A1(request):
    allure.dynamic.description("""MYDESC""")
    myfunct()


def myfunct():
    plugin = next(p for p in plugin_manager.get_plugins() if isinstance(p, AllureListener))
    testdescription = plugin.allure_logger.get_test(None).description
    ...

However, beware that the API is not public (and it looks like it's not intended to be public), so watch out for changes in the implementation of AllureListener which can easily break myfunct.

Upvotes: 2

Related Questions