【问题标题】:pytest, using multiple commands at the same linepytest,在同一行使用多个命令
【发布时间】:2018-08-14 06:27:20
【问题描述】:

我有这种类型的代码:

@pytest.mark.run(order=2)
@pytest.mark.parametrize('time_ms', range(100, 160, 20))
    def test_wifi_advertising_interval(time_ms):
    test_wifi_screen_edit()
    ....
    ....

现在,我想要的是在一行中创建所有命令(在我的例子中是两个),

像这样:

@pytestCodeWithSingleLine(pytest.mark.parametrize('time_ms', range(100, 160, 20),pytest.mark.run(order=2)))

有可能吗? 谢谢

【问题讨论】:

    标签: python python-3.x python-decorators


    【解决方案1】:

    你可以很容易地编写一个函数来组合装饰器:

    def combine(*decorators):
        def combined_decorator(func):
            for deco in decorators:
                func = deco(func)
            return func
        return combined_decorator
    

    这不是pytest 特定的,它应该适用于在单独的行上编写时一起工作的任何装饰器组合。

    虽然这并不难,但这并不一定意味着这样做是个好主意。我认为你的代码的两行版本比一长行的组合版本更容易理解,所以我建议保留未组合的版本。

    【讨论】:

    • 感谢您的回答和洞察!
    猜你喜欢
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2017-10-12
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2010-10-01
    相关资源
    最近更新 更多