JBehave:表内参数,如

发布于 2024-12-29 09:08:56 字数 419 浏览 4 评论 0原文

我想知道是否有人知道如何做到这一点。

我有一个类似于以下代码片段的场景。在表中,我希望有运行测试时可以替换的参数。示例片段:

...
Given blah blah blah
Then yada yada yada
...
And the quotes should have details:
|Ref|Product|Issue Date|Maturity Date|
|<A VALID REF>|Vanilla Option|<TODAY>|<TODAY+3M>

所以我想知道 JBheave 是否支持表中的这种参数化,我可以在表中为 和 定义一个值,并在运行时替换它。

我没有找到涉及此的文档,因此我怀疑我必须手动执行此操作。这确实没问题,但如果有更好的方法,那么我很乐意学习。

I wonder if anyone would know how this can be done.

I have a scenario that goes something like the following snippet. Within the table, I would like to have parameters that can be substituted when the test is run. Example snippet:

...
Given blah blah blah
Then yada yada yada
...
And the quotes should have details:
|Ref|Product|Issue Date|Maturity Date|
|<A VALID REF>|Vanilla Option|<TODAY>|<TODAY+3M>

So what I would like to know is whether JBheave supports this kind of parameterisation within a table where I can define a value for and somewhere in the code and have it substituted at runtime.

I find no documentation that refers to this, so I suspect that I would have to do it manually. That's no problem really, but if there's a better way to do it then I'd be keen to learn.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦冥 2025-01-05 09:08:56

我已经有一段时间没有使用 JBehave 了(自 2.X 以来),但当时它不支持它,我认为现在它不太可能支持它。

您在这里所做的就是制作一个适用于所有示例的场景。这并不是真正的场景——它是以场景形式编写的验收标准。如果这样做,您将错过使用具体示例的其他好处,特别是它们激发想象力、唤起有趣的行为以及允许与业务利益相关者提出有用的问题和对话。

在这种情况下,如果产品是该场景结果中最有趣的事情,那么更好的做法是将其单独列出来,例如

然后报价应包含普通期权产品。

更强大的是找出结果对谁有用,并提及它以何种方式有用,例如

然后,用户应该通过电子邮件收到报价,以吸引他们购买普通选项。

请注意,在我给出的示例中,实际上受益的是企业,而不是真正为用户带来的利益。

通过在更高的抽象级别上执行这些步骤,您会发现更容易向用户/利益相关者展示结果的价值,这将帮助您进行更好的对话。 BDD 的核心是对话,而不是自动化。

这就是为什么我不相信 JBehave 支持它,我也不认为它应该支持它。

I haven't played with JBehave for a while (since 2.X) but it didn't support it then and I think it's unlikely to support it now.

What you're doing here is crafting a scenario which works for all examples. That's not really a scenario - it's acceptance criteria, written in scenario form. If you do this, you'll miss out on the other benefits of using concrete examples, particularly in the way that they excite the imagination, call out the interesting behavior and allow useful questions and conversations with business stakeholders.

In this case, if the product is the most interesting thing about the outcome of this scenario, the better thing to do is to call it out separately, e.g.

Then the quotes should contain the Vanilla Option product.

Even more powerful would be to work out who the outcome is useful for, and mention in what way it's useful, e.g.

Then the user should receive a quote by email to entice them to buy the Vanilla Option.

Notice that in the example I've given it's actually the business that benefits, and it's not really done for the user.

By making the steps at a higher level of abstraction you'll find it easier to call out the value of the outcome to the users / stakeholders, which will help you have better conversations. Conversation, not automation, is at the heart of BDD.

This is why I don't believe JBehave supports it, nor do I think it should.

雨后彩虹 2025-01-05 09:08:56

我认为这就是您正在尝试做的事情。

我在测试中使用了大量随机数据,例如人名。当我生成随机名称时,我将其写入一个 Java 类,用于存储此类数据,以便稍后我可以回忆起来。

在故事中,我将有类似的内容

When I create a new customer <customer>
When I do some stuff

When I do some more customer stuff
|NAME    |VALUE     |
|custName|<CUSTNAME>|
|dob     |01/01/1970|

When I do some more stuff

Examples:
|<customer>|<BLAH1>|<BLAH2>|<CUSTNAME>|<BLAH4>|
|\$random  |para1  |para2  |\$name    |para4  |
|George    |para1  |para2  |Peter     |para4  |

在从我使用的表中提取数据的类中:

if (value.equalsIgnoreCase("$name") {
    this.value = getStoredData().getName();
} else {
    this.value = value;
}

这是我自己的个人惯例,我使用 <>围绕故事中的变量,$ 代表 Java 代码生成或调用变量值的变量。

如果您在示例表中使用 $,则必须对其进行转义,因此需要使用反斜杠

I think that this is what you are trying to do.

I use a lot of random data in my tests, for example, for people's names. When I generate the random name I write it to a Java class for storing this type of data which I can then recall later on.

In the story I will have something like

When I create a new customer <customer>
When I do some stuff

When I do some more customer stuff
|NAME    |VALUE     |
|custName|<CUSTNAME>|
|dob     |01/01/1970|

When I do some more stuff

Examples:
|<customer>|<BLAH1>|<BLAH2>|<CUSTNAME>|<BLAH4>|
|\$random  |para1  |para2  |\$name    |para4  |
|George    |para1  |para2  |Peter     |para4  |

In the class where the data is extracted from the table I use:

if (value.equalsIgnoreCase("$name") {
    this.value = getStoredData().getName();
} else {
    this.value = value;
}

It is my own personal convention that I use <> around variables within the story and $ for variables where the Java code generates or recalls the value of the variable.

If you use $ in the Examples table it has to be escaped, hence the back slash

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文