Cucumber之一Cucumber概述——学习新篇章

Cucumber(黄瓜)是什么?
你也可以通过这篇博文了解个大概(点击查看) 。

免费英语视频教程可见微信公众号:【软测小生】里面,请关注公号更新相关文章和视频资源。

开始使用Cucumber BDD在敏捷团队中进行测试

简介

敏捷软件方法最近在快速变化的市场中越来越受软件开发团队的欢迎。然而,这种不断增长的趋势也迫使测试团队管理和维护他们的测试用例和测试脚本,以满足不断变化的需求。因此,从一开始就应该选择合适的测试方法,以便顺利地实现任何敏捷软件项目。

 

Cucumber及其突出特点

由于使用Cucumber工具的行为驱动开发(BDD)方法,目前已有许多敏捷软件项目取得了成功。Cucumber是什么?

Cucumber是一个工具,用于运行以BDD格式创建的自动化验收测试。该工具最突出的特性之一是能够将纯文本功能描述(使用Gherkin语言编写)作为自动化测试执行。让我们来看看下面的例子:

Feature: Update password

 Scenario: Admin user can update the user password

Given I am in the HR system with an Admin account
When I update password of another user
Then I receive a message for updating password successfully
And user password is updated to the new password

#以下是中文的描述

Feature: 更新密码

 Scenario: 管理员用户可以更新用户密码

Given 我在人力资源系统中有一个管理帐户
When 我更新了另一个用户的密码
Then 我收到一条更新密码成功的消息
And 将用户密码更新为新密码

这种令人难以置信的行为驱动开发(BDD)方法具有以下优点:
——使用无所不在的语言编写BDD测试,这是一种围绕领域模型构建的语言,被开发人员、测试人员、BA人员和客户组成的所有团队成员广泛使用。
——连接软件团队的技术人员和非技术人员。
——允许与开发人员的代码直接交互,但是BDD测试是用一种语言编写的,业务利益相关者也可以使用这种语言编写测试。
——最后但并非最不重要的是,验收测试可以自动执行,同时也可以由业务利益相关者手动执行

Cucumber有助于增进沟通

Cucumber有助于提高同一项目中技术人员和非技术人员之间的沟通。让我们来看看下面的需求及其自动化测试:

As an Admin User,
I would like to change the password of other user's accounts.
Feature: Update password
 Scenario: Admin user can update the user password
   Given I am in the HR system with an Admin account
   When I update password of another user
   Then I receive a message for updating password successfully
   And user's password is updated to the new password

#中文
作为一个管理用户,
我想更改其他用户帐户的密码。
Feature: 更新密码
 Scenario: 管理员用户可以更新用户密码
Given 我在人力资源系统中有一个管理帐户
When 我更新了另一个用户的密码
Then 我收到一条更新密码成功的消息
And 将用户密码更新为新密码

使用TestNG,上述测试场景可以实现如下:

@test
public void testAdminUserCanUpdateUserAccountPassword() {
 // create users 创建用户
 User userAdmin = new User(UserRole.ADMIN, username, password);
 User user = new User(UserRole.VIEWER, user_username, user_password);
       
 // use Admin user to update another user password 使用管理员用户更新其他用户的密码
  String message = userAdmin.updatePassword(user, user_new_password);
  

// verify password changed  验证更新的密码
  Assert.assertEquals(message, "Password changed successfully");
  Assert.assertEquals(user.getPassword(), user_new_password);
}

同样的测试用例可以用Cucumber编写:

Feature: Update password
 Scenario: Admin user can update the user password
   Given I am in the HR system with an Admin account
   When I update password of another user
   Then I receive a message for updating password successfully
   And user's password is updated to the new password

上面的两个自动化测试脚本都可以很好地自动完成测试。但是你的团队的所有测试人员都做出了这些测试吗?其他业务分析师和其他利益相关者是否可以在验收测试阶段再次使用这些测试?

对于大多数手工测试人员和BA人员来说,使用TestNG进行自动化测试可能比较困难。而且,验收测试不可能再次使用这个测试。因此,基于上述缺陷,不能认为这是一个合适的方法。

相反,使用Cucumber的自动化测试是用业务领域语言或自然语言创建的,软件项目团队的所有成员都可以轻松地使用这些语言。沟通对于任何开发团队都是至关重要的,尤其是在敏捷团队中。通常在开发人员和测试人员之间会有许多连续的聊天、讨论,甚至争论,以确定某个特性的正确行为是什么。通过使用Cucumber,相同的需求规范现在被开发人员用于开发,被测试人员用于测试。它被认为是一个强大的工具,因为它可以帮助降低误解和沟通障碍的风险。

Cucumber是一个自动化的验收测试工具

验收测试通常由BA人员/客户进行,以确保开发团队已经构建了特定的功能。这个测试阶段的典型活动是使用来自线上环境特定的真实数据,根据原始需求对系统进行验证。Cucumber测试不仅遵循测试场景的需求,还可以帮助BA人员或产品经理轻松调整测试数据。下面是一个稍微调整一下的演示:

As an Admin User,
I would like to change the password of other user's accounts.         
Feature: Update password
 Scenario: Admin user can update the user password
   Given I am in the HR system with an Admin account
   When I update password of another user
   Then I receive a message for updating password successfully
   And user's password is updated to the new password

自动化测试用Cucumber框架编写:

Scenario Outline: Verify Updating user password feature
 Given I am in the HR system with "<account_type>" account
 And there is another user with "<old_password>" password
 When I update password of the user to "<new_password>"
 Then I got the message "<message>"
 And the user password should be "<final_password>"
Examples:
|account_type  |old_password |new_password |message               |final_password |
|Admin         |$Test123     |@Test123     |Password changed..    |@Test123       |
|Viewer        |$Test123     |@Test123     |Invalid right access..|$Test123       |

所有测试人员都可以参与Cucumber BDD的自动化测试

除了改善测试团队成员之间的沟通,Cucumber还帮助有效地利用测试人员的技能。每个组织中都存在着专业知识差距。换句话说,一些测试人员在利用自动化测试进行编程方面具有很高的技术专长,而另一些测试人员在同一个团队中执行手工测试,他们的编程技能有限。多亏了Cucumber,所有的测试人员,无论他们的技术水平如何,都可以参与到执行自动化测试的过程中。

让我们来看看上面的例子:
-任何了解业务逻辑和工作流的测试人员都可以编写功能文件、添加更多场景和测试数据集。
-任何具有基本编程知识并知道如何创建对象、访问属性、调用方法的测试人员都可以生成步骤定义(
step definitions)。
-任何具有较高编程水平的测试人员都可以参与构建框架、定义数据源连接等过程。

在实施Cucumber时仍然存在一些潜在的问题:
Cucumber使用业务领域知识帮助运行纯文本文件中指定的测试场景。因此,语言的使用和创建测试的人的看法可能直接影响测试场景,从而导致误解的风险。应该清楚地展示测试场景,并且它们的实现应该对每个步骤都执行准确。
例如,当你想验证谷歌的搜索功能时,测试应该是:

Scenario: performing a search on google
Given I am on "www.google.com" site
When I search for "Cucumber and BDD"
Then ...

这些步骤可纳入以下测试:

Scenario: performing a search on google
When I search for "Cucumber and BDD"
Then ...

 

Cucumber工具的各个阶段用一种普通语言执行。它们可以在各种测试场景中再次使用。这有助于减少创建测试的工作。然而,保持测试可读性和可重用性是一个很大的挑战。
如果测试是在一个非常高的水平上编写的,供任何参与人员理解;
可以重用的步骤很少(粗体):上面的脚本都是正确的;
然而,第二个并不明显,因为它的作用比预期的要大得多:打开谷歌的网站并使用指定的文本进行搜索。 想象一下,如果您想扩展测试来搜索更多的文本,您可以重复上面的步骤,因此谷歌站点会打开两次。如果您没有严格遵守要求,Cucumber测试工具迟早会引起误解,并且在扩展时很难维护。

Feature: Update password

 Scenario: Admin user can update the user password
   Given I am in the HR system with an Admin account
   When I update password of another user
   Then I receive a message for updating password successfully
   And user's password is updated to the new password

 Scenario: Viewer user cannot update the user password
   Given I am in the HR system with a Viewer account
    When I update password of another user
    Then I receive a message for not able to update the user password
    And user's password remains the same

 

相反,如果测试是通用的并且可以重用,例如:核实更新用户的姓氏,非技术人员将难以跟进及进行验收测试:

Scenario: Admin user can update user password:
 Given I am in the "$System.HR_Page" with "admin@test.com" username
and "$Test123" password
 And there is another user in "$System.HR_Page" with "user@test.com" 
username and "$Test123" password
 When I update "$UserTemplate.Password" of "user@test.com" user to"@Test123"
 And I save the response message as "response_message"
 Then "$response_message" should be "Password changed successfully"
 And the  "user@test.com" user's "$UserTemplate.Password" should be"@Test123"

 

在测试过程中,您必须定期调整测试场景,直到它们完全达到所有成员都能理解和重用的可接受的平衡。

Scenario: Verify Updating user password feature
 Given I am in the HR system with "Admin" account
 And there is another user with "$Test123" password
 When I update password of the user to "@Test123"
 Then I got the message "Password changed successfully."
 And the user password should be "@Test123"

或者使用更多的测试数据:

Scenario Outline: Verify Updating user password feature
 Given I am in the HR system with "<account_type>" account
 And there is another user with "<old_password>" password
 When I update password of the user to "<new_password>"
 Then I got the message "<message>"
 And the user password should be "<final_password>"

Examples:
|account_type |old_password |new_password |message           |final_password |
|Admin        |$Test123 |@Test123     |Password changed.. |@Test123  |
|Viewer       |$Test123 |@Test123     |Invalid right access.. |$Test123  |

 

对于想要开始使用Cucumber的测试团队来说,这是非常重要的注意事项

-将自动化测试视为与实际项目同等重要的测试。代码应该遵循编码实践、惯例等。
-应考虑使用适当的编辑器工具。这个编辑器应该帮助调试和编辑标准文本格式的特性文件。Aptana(免费编辑器)、RubyMine(商业编辑器)和Katalon Studio是完全支持基于bdd的Cucumber的合适选项。
-最后但并非最不重要的,使feature文件成为一个实际的“通信(communication)”层,在那里您可以存储接收到的测试数据和格式化测试数据。不包含域业务逻辑。

综上所述,Cucumber是在健壮的测试框架之上为我们提供真实通信层的最强大工具之一。该工具可以帮助在从后端到前端的广泛测试需求上运行自动化测试。此外,Cucumber在测试团队成员之间建立了深厚的联系,这在其他测试框架中很难找到。基于多年的自动化测试经验,我建议应该实现用于Web UI和Web服务测试的Cucumber,以帮助成功运行敏捷软件项目。

 

后续会更新更多文章,上文种可能有些许错误,若有请指正,谢谢

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值