How to show variable-length argument list in allure.step?

1.9k views Asked by At

Allure supports step name with function parameters https://github.com/allure-framework/allure-python#steps

@allure.step('my step with parameter a={0}, b={1}')
def my_step(important_parameter, my_parameter):
    pass

But it work for determined count of function variables. Is it possible to show all variables in case **args and **kwargs parameters? Something like

@allure.step('my step with parameter **kwargs}')
def my_step(**kwargs):
    pass
1

There are 1 answers

0
pupssman On BEST ANSWER

allure.step calls format method on the step title to form the actual step name

See source at https://github.com/allure-framework/allure-python/blob/master/allure/common.py#L56

So far string formatting has no means to show variable-arguments string.

https://docs.python.org/2/library/string.html#format-string-syntax

To work around your issue you may try to pass a wrapper object to the step title with a format method that will handle any custom logic.

Like

class MyFancyStepTitle(object):
    def __init__(self, title):
        self.title = title
    def format(self, *a, **kw):
        return self.title.format(args=a, kwargs=kw)

@allure.step(MyFancyStepTitle('Function called with args {args}, kwargs {kwargs}'))
def my_fancy_function(*a, **kw):
    return 'OK'

def test_foo():
    # should produce step named 
    # "Function called with args [1, 2, 3], kwargs {'foo': 'bar', 'baz': '123'}"
    my_fancy_function(1, 2, 3, foo='bar', baz='123')

Also, you are welcome to send any pull requests to possibly fix parameter-passing login in the allure-python itself.

Cheers!