编写使用站点的 Cucumber 功能/场景(作为管理员、用户和访客)

发布于 2024-10-12 09:54:59 字数 3349 浏览 1 评论 0原文

我是黄瓜新手,目前一直在尝试组织/构建一些功能/场景。我试图捕捉的是使用我的网站的用户、访客和管理员的行为。该网站基本上用于创建私人和公共待办事项列表。

这些是我想要测试的具体场景:

  • 注册用户可以查看他们的私人待办事项列表(但不能查看其他人的私人待办事项列表)
  • 访客(匿名用户)只能查看公共待办事项列表
  • 管理员可以查看所有列表
  • 管理待办事项列表

现在我我正在处理这样的事情:

Feature: Managing Todo lists
  In order to be more productive
  As a user of the site
  I want to be able to manage todo lists

Background:
  Given a user named "[email protected]" with password "secret-one"
  Given a user named "[email protected]" with password "secret-two"
  Given an admin named "[email protected]" with password "admin"

Scenario: [email protected] can view his own private todo lists
Scenario: [email protected] can not view [email protected] private todo lists
Scenario: [email protected] can view [email protected] and [email protected] private todo lists
Scenario: [email protected] can create private todo lists
Scenario: [email protected] can delete todo lists they own
Scenario: [email protected] can delete user-one@email todo list
Scenario: guests can view all public todo lists

我遇到的问题是每个场景的设置。例如,在第一种情况下,我必须假设 [email protected] 已登录。在第一个 [email protected] 场景我必须假设管理员已登录。在最后一个场景中,我需要假设没有人登录。

那么我如何管理这些 Given 呢?我是否只需添加一个

Given [email protected] is logged in

到每个场景?或者有更好的方法来构建这一切吗?请帮忙!我确信这是黄瓜用户需要测试的非常常见的模式。

I'm new to cucumber and currently stuck trying to organize/structure some features/scenarios. The behavior I'm trying to capture is that of users, guests and admins using my website. The website is basically for creating private and public todo lists.

And these are the specific scenarios I want to test:

  • Registered users can view their private todo lists (but not someone else's private todo list)
  • Guests (anonymous users) can only view public todo lists
  • Admins can view all lists
  • Managing todo lists

Right now I'm going with something like this:

Feature: Managing Todo lists
  In order to be more productive
  As a user of the site
  I want to be able to manage todo lists

Background:
  Given a user named "[email protected]" with password "secret-one"
  Given a user named "[email protected]" with password "secret-two"
  Given an admin named "[email protected]" with password "admin"

Scenario: [email protected] can view his own private todo lists
Scenario: [email protected] can not view [email protected] private todo lists
Scenario: [email protected] can view [email protected] and [email protected] private todo lists
Scenario: [email protected] can create private todo lists
Scenario: [email protected] can delete todo lists they own
Scenario: [email protected] can delete user-one@email todo list
Scenario: guests can view all public todo lists

The problem I'm running into is the setup for each scenario. For example, in the first scenario I have to assume [email protected] is logged in. In the first [email protected] scenario I have to assume the admin is logged in. In the last scenario I need to assume no one is logged in.

So how do I manage those Givens? Do I just add a

Given [email protected] is logged in

To every single scenario? Or is there a better way to go about structuring all this? Please help! I'm sure this is a very common pattern that cucumber users need to test.

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

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

发布评论

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

评论(1

蒲公英的约定 2024-10-19 09:54:59

一般来说,我会尝试只列出重要的步骤。在你的情况下,我会这样做:

Feature: Managing Todo lists
  In order to be more productive
  As a user of the site
  I want to be able to manage todo lists

Background:
  Given a user named "[email protected]" with password "secret-one"
  Given a user named "[email protected]" with password "secret-two"
  Given an admin named "[email protected]" with password "admin"

Scenario: Users can see own private todo lists
  Given user "user-one" has a private todo list
  Then user "user-one" can see the private todo list of "user-one"

Scenario: Users cannot see others private todo lists
  Given user "user-one" has a private todo list
  Then user "user-two" cannot see the private todo list of "user-one"

如果你在其他地方有登录步骤,你可以在然后用户“user-one”可以看到“user-one”的私人待办事项列表中重复使用该步骤 步骤定义,而不是在特征中。或者您的步骤可以直接共享相同的代码。

请参阅步骤重用

Generally I try to list only the steps that are important. In your case I would do something like:

Feature: Managing Todo lists
  In order to be more productive
  As a user of the site
  I want to be able to manage todo lists

Background:
  Given a user named "[email protected]" with password "secret-one"
  Given a user named "[email protected]" with password "secret-two"
  Given an admin named "[email protected]" with password "admin"

Scenario: Users can see own private todo lists
  Given user "user-one" has a private todo list
  Then user "user-one" can see the private todo list of "user-one"

Scenario: Users cannot see others private todo lists
  Given user "user-one" has a private todo list
  Then user "user-two" cannot see the private todo list of "user-one"

If you have a login step elsewhere you can re-use that step in the Then user "user-one" can see the private todo list of "user-one" step defintion, rather than in the feature. Or your step could just share the same code directly.

See step re-use

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