Cucumber 测试中的特性是什么?

2025年3月17日 | 阅读 3 分钟

特性是软件应用程序的一个功能或独立单元。换句话说,特性是用于测试客户对软件产品的要求的参数。

让我们通过一个非常常见的社交网站的例子来理解它。

社交网站的一些基本特性可以确定为 -

  • 在社交网站上创建和删除用户。
  • 用户登录功能,用于访问社交网站。
  • 在社交网站上分享视频或照片。
  • 发送好友请求。
  • 注销或退出。

在测试时,最佳实践是我们应该首先确定特性,然后再导出要测试的测试脚本。

因此,从上面的讨论中可以清楚地看出,当我们谈论 cucumber 时,产品或 Web 应用程序的每个独立功能都可以称为一个特性。 一个特性通常有一个要测试的场景列表,该特性及其描述存储在 特性文件中。

一个软件产品可以有许多特性。 因此,为了更好地管理特性,我们应该为每个特性创建一个单独的特性文件。

序号特性特性文件名
1登录特性Login.feature
2分享帖子特性sharePost.feature
3创建帐户特性AccountCreation.feature
4删除帐户特性AccountDelete.feature

关键字 "Feature" 表示 Gherkin 语言中正在测试的特性。

注意: 建议在特性文件中,在关键字 feature 下面写一个小的特性描述。

示例

假设,社交网站的登录功能正在测试中。 因此,我们需要按照以下方面对其进行测试

  • 如果凭据(即用户名和密码)都正确,则用户应登录到社交网站。
  • 如果用户名不正确但密码正确,则应向用户显示错误消息。
  • 如果用户名正确但密码不正确,则应向用户显示错误消息。
  • 成功登录后,用户应导航到“我的帐户”或“个人资料”页面。
feature in cucumber testing

现在,我们将为社交网站的登录功能创建一个特性文件

Feature: Login functionality
Scenario: Successful Login with Valid entries
Given user navigates to the website facebook.com	
And user logs in through Login link by using username as "[email protected]" and password as "prity123sharma"

Then login must be successful.
Scenario:  Unsuccessful Login with Invalid entries
Given user navigates to the website facebook.com
When username is incorrect, but the password is correct
user logs in through Login link by using Username as "[email protected]" and Password as "prity123sharma"
When username is correct, but the password is incorrect
user logs in through Login link by using username as "[email protected]" and Password as "12345678"
Then login must be unsuccessful.

根据上面的例子,我们可以根据特定特性创建特性文件。 特性文件始终基于特定情况下应用程序的行为。