我正在使用黄瓜jvm用Groovy编写测试。到现在为止,我已经将所有步骤定义都放在一个文件中,并且一切都运行良好。我现在想将步骤定义移到单独的文件中,因为我有很多定义。我遇到的问题是实例变量。我试图使步骤定义尽可能通用,因此任何带@注释的对象在返回一个称为response的对象时,所有带@then注释的定义都会在响应中声明某些内容。我的问题是,是否可以将这些文件存储在单独的文件中?我已经阅读了一些有关“世界”的内容,但是我不确定这是否是我想要的内容,尽管查看了github上的示例项目(https://github.com/cucumber/cucumber-jvm/blob/master/groovy/src/test/groovy/cucumber/runtime/groovy/compiled_stepdefs.groovy),我无法使它正常工作。
我想要达到的目标的一个例子是这样的:
Scenario:
When I say hello
Then Hello should be printed
作为我的方案。然后,我想拥有两个共享变量的类,如下所示:
class sayHello{
def response
@When('^I say hello$')
def iSayHello() {
response = "hello"
}
}
class printHello{
@Then('^Hello should be printed$')
def iPrintHello() {
assert response == "hello"
}
}
我知道我可以使用spring将变量注入到类中,但是我想知道是否还有另一种方法,以及我是否在使用“ World”对象的正确轨道上。提前致谢
我已经找到了使用groovy的mixin功能解决此问题的方法。mixin允许一个类继承另一个类的方法,并在运行时使用classes变量。
使用上面的示例,我的解决方案如下所示:
@Mixin(printHello)
class sayHello {
@When('^I say hello$')
def iSayHello() {
response = "hello"
}
}
class printHello {
static def response
@Then('^Hello should be printed$')
def iPrintHello() {
assert response == "hello"
}
}
这使我能够从任何类中通用设置响应对象,然后在通用步骤文件中对其进行断言。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句