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

How to re-encapsulate the allure decorator

Open Kentonlzy opened this issue 4 years ago • 0 comments

Hello, I wanan replace unittest with allure-pytest.In my project , we used @caseid decorator to describe the testcase which is running now.
In order to avoid excessive changes to the existing code,I wanan re-encapsulate the allure decorator which named @allure.testcase by code like this:

  def case(func=None, *, tc=None):
    if func is None:
        return partial(case, tc=tc)

    @wraps(func)
    def wrapper(*args, **kwargs):
        url = "https://google.com" + "/prefix/" + tc
        return allure.testcase(url=url, name=tc)(func)(*args, **kwargs)
    return wrapper

but it didn't work.

class TestSomething:
    @case(tc="testcase1")
    def test_1(self):
        print("testcase1")

    @allure.testcase(url="https://google.com", name="testcase2")
    def test_2(self):
        print("testcase2")
 pytest --alluredir ./report/allure_raw
 allure serve report/allure_raw -p 8080 -h  127.0.0.1

the reslut is: testcase1: image testcase2: image

by the way , I re-encapsulate other decorator,and it worked。 the code like this:

def ex(func=None, *, url=None, tc=None):
    if func is None:
        return partial(ex, url=url, tc=tc)

    @wraps(func)
    def wrapper(*args, **kwargs):
        print(f"url:{url}, tc:{tc}")
        return func(*args, **kwargs)

    return wrapper


def case(func=None, *, tc=None):
    if func is None:
        return partial(case, tc=tc)

    @wraps(func)
    def wrapper(*args, **kwargs):
        url = "https://google.com" + "/prefix/" + tc
        print(f"case url:{url}")
        return ex(url=url, tc=tc)(func)(*args, **kwargs)

    return wrapper


@case(tc="testcase1")
def spam(x=1):
    print(f"num is {x}")
    return x


spam(1)
output:
case url:https://google.com/prefix/testcase1
url:https://google.com/prefix/testcase1, tc:testcase1
num is 1

Kentonlzy avatar Sep 14 '21 15:09 Kentonlzy