如何使用groovy在多个黄瓜步骤定义文件中共享变量 [英] How to share variables across multiple cucumber step definition files with groovy
问题描述
我正在使用cucumber jvm编写groovy测试。到目前为止,我已经将所有步骤定义放在一个文件中,并且一切都运行良好。我现在正在考虑将步骤定义移到单独的文件中,因为我有很多这样的文件。
我遇到的问题是实例变量。我试图使我的步骤定义尽可能通用,所以任何使用@when注释的对象都会返回一个名为response的对象,并且任何使用@then注释的定义都会在响应中声明某些内容。
我的问题是,是否有一种方法可以将这些文件存储在单独的文件中?我已经阅读了一些关于'世界'的内容,但我不确定这是否是我正在寻找的内容,尽管在github上查看了示例项目( https://github.com/cucumber/cucumber-jvm/blob/master/groovy /src/test/groovy/cucumber/runtime/groovy/compiled_stepdefs.groovy ),我无法得到这个工作。
我是一个例子试图实现将是这样的:
场景:
当我说你好
然后Hello应该被打印
作为我的场景。然后我想要两个共享变量的类,如下所示:
class sayHello {
def response
@When('^我说你好$')
def iSayHello(){
response =hello
}
}
class printHello {
@Then('^你好应该打印$')
def iPrintHello(){
assert response ==hello
$ / code>
我知道我可以使用spring将变量注入类,但我想知道是否有另一种方式,如果我与世界对象在正确的轨道上。
提前致谢
我使用groovy的mixin功能找到了解决方案。 mixin允许类继承另一个类的方法,也可以在运行时使用类变量。
使用上述示例,我的解决方案如下所示:
@Mixin(printHello)
class sayHello {
@When('^我说你好$ ')
def isayHello(){
response =hello
}
}
class printHello {
static def响应
@然后('^你好应该打印$')
def iPrintHello(){
断言响应==hello
}
}
这允许我一般地设置任何类的响应对象,然后断言它在通用步骤文件中。
I am using cucumber jvm to write tests with groovy. Up until now, I have had all of my step definitions in one file, and everything has worked perfectly. I am now looking to move the step definitions to seperate files, as I have a lot of them. The problem I am having is with instance variables. I tried to make my step definitions as generic as possible, so any annotated with @when returned an object called response, and any definitions annotated with @then asserted something on the response. My question is, is there are a way that I can have these stored in separate files? I have read a little about the 'World' but I am not sure if that is what I am looking for, and despite looking at the example project on github (https://github.com/cucumber/cucumber-jvm/blob/master/groovy/src/test/groovy/cucumber/runtime/groovy/compiled_stepdefs.groovy), I cannot get this to work.
An example of what I am trying to achieve would be something like this:
Scenario:
When I say hello
Then Hello should be printed
As my scenario. I then want to have two classes that share variables like so:
class sayHello{
def response
@When('^I say hello$')
def iSayHello() {
response = "hello"
}
}
class printHello{
@Then('^Hello should be printed$')
def iPrintHello() {
assert response == "hello"
}
}
I know that i could use spring to inject the variables into the classes, but I was wondering if there was another way, and if I was on the right tracks with the 'World' object. Thanks in advance
I have found a solution to this using groovy's mixin functionality. mixin allows a class to inherit the methods of another class, and also use the classes variables at runtime.
Using the above example, my solution looks like this:
@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"
}
}
This allows me to generically set the response object from any class, and then make an assertion on it in the common steps file.
这篇关于如何使用groovy在多个黄瓜步骤定义文件中共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!