Cucumber测试中的特性文件

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

特性文件是Cucumber工具的重要组成部分,用于编写自动化测试的验收步骤。验收步骤通常遵循应用程序规范。

特性文件通常是一个通用文件,用于存储特性场景和要测试的特性描述。

特性文件是编写Cucumber测试的入口点,并在测试时用作实时文档。

Feature File in Cucumber Testing

特性文件的扩展名为".feature"。软件的每个功能都必须有一个单独的特性文件。

示例

为了确保登录功能正常工作,我们通过创建一个特性文件来实现Cucumber测试。它将验证登录功能是否正常工作。

Feature: Login 
Scenario: Login Functionality
Given user navigates to the website javatpoint.com
And there user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.

执行自动化测试后,将创建一个表作为自动化测试的结果。此表用于标签中。

结果表如下表所示

Feature File in Cucumber Testing

具有多个场景的特性文件

特性文件可以包含多个场景或场景大纲。我们可以在特性文件中编写特定特性的所有可能场景。

Feature File in Cucumber Testing

通过使用关键字“Scenario”或“Scenario Outline”,可以将一个场景与另一个场景分开。

但是,单个特性文件可以包含任意数量的场景,但一次只关注一个特性,例如注册、登录等。因此,最好将与特定特性相关的场景保存在单个特性文件中。

可以并行执行场景,也可以将它们作为一个组一起执行。 让我们举一个例子来更清楚地说明

示例

特性文件1

Feature: Registration 
Background: 
Given user on the homepage  
And user follows "Sign in"  
@regression  
Scenario: Create a New User 
When user fills "registration email textbox" with "[email protected]"  
And user clicks "create an account button"  
And user enters the following details 
| First Name | Chitrali| 
| Last Name | Sharma| 
| Password | Inquiry@1234 | 
| Date | 17| | Month | 02| | Year | 1992 |  
And user clicks "register button"
Scenario: User does not follow form validations
When user enters wrong characters
Then error message displayed with invalid password
And user returns back on registration page

特性文件2

Feature: Login
Background: 
Given user on the login page  
And user follows "Log in"  

@regression @smoke
Scenario: Verification of Login Function  
Given user on the Login Page
And user enters "email address" with "[email protected]" 
And user enters "password" with "Inquiry@1234"  
And user click "log in" button
Then user should see "My Account" 
Scenario: Unsuccessful login
Given user on the Login Page
And user enters "email address" with "[email protected]" 
And user enters "password" with "qsder@1234"  
And user clicks "login" button
Then error message displayed with wrong password
And user returns back on login page

特性文件中的注释

如果我们不需要一次执行某个特定场景,那么我们可以注释掉该场景。

在Eclipse中,要注释多行或使用块注释,首先选择要注释的所有行,然后按Ctrl + /。 同样,要删除注释,我们需要按Ctrl + \。 其他IDE可能包含其他执行此操作的快捷方式。

注释任何场景时,请不要忘记注释完整的场景。 否则,未注释的场景的其余行将被视为先前场景的一部分。