我喜欢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中是否有类似执行依赖注入的机制?
答案 0 :(得分:1)
不,机器人没有内置任何你想要的东西。
如果你追求的是在运行时生成的字符串,你可以利用机器人的extended variable syntax。例如,如果你有一个机器人变量${v}
来保存对python对象的引用,你可以这样做:
| | ${v}= | Get reference to v
| | log | Hello ${v.silly_string()}`
以上将在silly_string
指向的对象上调用${v}
方法。关键字Get reference to v
将是您编写的基于python的关键字,它返回带有silly_string
方法的对象。