Cucumber场景下的if语句怎么做?

发布于 2024-12-02 23:57:46 字数 89 浏览 0 评论 0原文

我的应用程序具有权限,并且当特定权限打开时不需要运行某些测试,而当打开相同权限时需要运行某些测试。

有办法做到这一点吗?或者我需要使用不同的框架吗?

My app has permissions, and certain tests need to not be run when a particular permission is on, and some tests need to be run when that same permission is on.

Is there a way to do this? or do I need to use a different framework?

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

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

发布评论

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

评论(3

同尘 2024-12-09 23:57:46

在 Cucumber 中排除“测试”的标准方法是向它们添加一个标签来识别它们,然后在调用 Cucumber 时指定要包含/排除哪些标签。在您的示例中,您可以标记特定场景:

@needs_permission
Scenario: View users billing information

或标记整个功能:

@needs_permission
Feature: Administrative area
    Scenario: View users billing information

或标记场景大纲中的某些示例:

Scenario Outline: Visit a page
    Given I visit "<page>"

Examples: Don't need permission
    | page    |
    | index   |
    | sitemap |

@needs_permission
Examples: Do need permission
    | page  |
    | admin |

现在,当您运行 Cucumber 时,您可以根据需要排除这些标记:

当权限打开且您想要时运行所有测试:

cucumber .

当权限关闭并且您想要排除需要它的测试时:

cucumber . -t ~@needs_permission

如果您确实提前不知道,我使用的另一种方法是将步骤标记为 待定如果它不适用给定当前场景,例如

Given /^I visit some page which needs permission$/ do
    pending "Permissions aren't enabled - skipping" unless permissions_enabled?
end

这会将步骤标记为“待处理”,这实际上意味着“未完全实施”,这并不理想,特别是如果您有许多这样的步骤 - 很难确保其他未实施的步骤不会意外蔓延并被所有您故意标记为此类的人隐藏。

The standard way of excluding 'tests' in Cucumber is to add a tag to them to identify them, then when invoking Cucumber you specify which tags to include/exclude. In your example, you could tag a specific scenario:

@needs_permission
Scenario: View users billing information

Or tag the whole feature:

@needs_permission
Feature: Administrative area
    Scenario: View users billing information

Or tag certain examples in a scenario outline:

Scenario Outline: Visit a page
    Given I visit "<page>"

Examples: Don't need permission
    | page    |
    | index   |
    | sitemap |

@needs_permission
Examples: Do need permission
    | page  |
    | admin |

Now, when you run Cucumber, you can exclude those tags if necessary:

When the permission is on and you want to run all tests:

cucumber .

When the permission is off and you want to exclude the tests that need it:

cucumber . -t ~@needs_permission

An alternative which I have used with mixed results, if you really don't know ahead of time, is to mark a step as pending if it doesn't apply given the current scenario, e.g.

Given /^I visit some page which needs permission$/ do
    pending "Permissions aren't enabled - skipping" unless permissions_enabled?
end

This will mark the step as 'pending' which really means 'not fully implemented', which isn't ideal, especially if you have many such steps - it can be difficult to ensure that other unimplemented steps don't accidentally creep in and get hidden by all the ones you've deliberately marked as such.

如此安好 2024-12-09 23:57:46

我们经常遇到类似的情况,我们有两种解决方案,
按照建议添加标签,或者
为不同的权限类型创建不同的场景:

Given i log in as a moderator 
and there is a new post
when i view the post
Then i should see a delete button  

Given i log in as a normal user 
and there is a new post
when i view the post
Then i should not see a delete button  

We get a similar instance of this a lot, we have 2 solutions,
either tags as suggested, or
create different scenarios for different permission types:

Given i log in as a moderator 
and there is a new post
when i view the post
Then i should see a delete button  

Given i log in as a normal user 
and there is a new post
when i view the post
Then i should not see a delete button  
糖果控 2024-12-09 23:57:46

或许可以使用标签。如果没有更多细节,很难说。

May be possible using tags. Difficult to say without more detail.

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