【问题标题】:Robot Framework equivalent of pytest.fixture机器人框架相当于 pytest.fixture
【发布时间】:2014-07-24 03:15:37
【问题描述】:

我非常喜欢 pytest 的一个特性是pytest.fixture,它允许依赖注入。例如,我可以让一个夹具将一个新的随机字符串 silly_string 注入到任何需要它的函数中:

conftest.py:

@pytest.fixture(scope='function')
def silly_string(request):
    return ''.join(random.choice(string.ascii_lowercase) for _ in range(5))

test_strings.py

def test_string_length(self, silly_string)
    assert len(silly_string) == 5

显然是人为的例子。

我不知道 Robot Framework 中有任何等效功能。目前,我使用关键字创建一个新变量,然后将该变量作为参数传递。自动注入参数就好了。

Robot Framework 中是否有类似执行依赖注入的机制?

【问题讨论】:

    标签: robotframework pytest


    【解决方案1】:

    不,机器人没有内置任何东西可以做你想做的事。

    如果您追求的是运行时生成的字符串,您可以利用机器人的extended variable syntax。例如,如果您有一个机器人变量${v} 包含对python 对象的引用,您可以执行以下操作:

    | | ${v}= | Get reference to v
    | | log | Hello ${v.silly_string()}`
    

    上面会在${v}指向的对象上调用silly_string方法。关键字Get reference to v 将是您编写的基于python 的关键字,它使用silly_string 方法返回一个对象。

    【讨论】:

    • 感谢您的确认。我不认为您知道 Robot Framework 和 pytest 可以协同工作的任何方式,pytest 提供依赖注入,Robot Framework 处理执行?
    • @alkalinity:我不知道。我以前从未使用过 pytest。
    猜你喜欢
    • 2016-04-26
    • 2018-04-25
    • 2020-08-23
    • 2015-11-03
    • 2017-11-11
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多