Cucumber (Gherkin)

Understanding Cucumber Tags

Estimated reading: 5 minutes 133 views
cucumber-guides-featured-image

In this guide, you’ll learn how to use Cucumber tags and hooks to organize and manage your tests efficiently. Tags allow you to categorize your scenarios, and hooks provide a way to run code before or after scenarios.

Tags in Cucumber

Tags are a way to organize and control the execution of your tests. They allow you to group scenarios, features, or even specific steps, making it easier to manage large test suites. Tags are prefixed with @ and placed above the relevant Gherkin keyword.

 

Single Tag

A single tag, such as @smoketest or @ignore, is used to label a specific test scenario. For instance, @smoketest indicates a basic test run to check the essential functionality, while @ignore is used to exclude certain tests from execution.

home-screen.feature

				
					  @smoketest
  Scenario: Click the Login Icon
    Given the user is on the home screen
    When the user clicks the "login" icon
    Then the "Login" screen is displayed
  
  @ignore 
  Scenario: Click the Products link
    Given the user is on the home screen
    When the user clicks the "Products" link
    Then the "Products" screen is displayed
				
			

Multiple Tags

Multiple tags, like @smoketest @critical, combine multiple categories, providing a more granular control over which tests to run. This combination might be used to run tests that are both part of the smoke test suite and critical for the application, ensuring that key functionalities are validated promptly.

home-screen.feature

				
					  @smoketest @critical
  Scenario: Click the Login Icon
    Given the user is on the home screen
    When the user clicks the "login" icon
    Then the "Login" screen is displayed
   
   @ignore
   Scenario: Click the Products link
    Given the user is on the home screen
    When the user clicks the "Products" link
    Then the "Products" screen is displayed
				
			

Creating a Test Runner

Create a test runner class in the src/test/java/com/kicksapp/runners directory. We’re going to name this class HomeScreenRunner.

creating-home-screen-runner-file
Creating Home Screen Runner File

Running Tests with Tags

To run tests with specific tags, use the @RunWith
and @CucumberOptions annotations in your test runner class.

The @CucumberOptions annotation is where you configure various aspects of your Cucumber tests. Here’s a breakdown of the options used in the snippet:

cucumber-runner-file-with-options
Cucumber Runner File With Options

HomeScreenRunner.class

				
					import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features/home-screen.feature",
    glue = "com.kicksapp.stepdefs",
    tags = "@smoketest",
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
public class HomeScreenRunner {
}
				
			
  • features = "src/test/resources/features/home-screen.feature": This specifies the path to the feature files. Feature files contain scenarios written in Gherkin language, which Cucumber executes.
  • glue = "com.kicksapp.stepdefs": This option specifies the package where step definition methods are located. Step definitions are the actual code implementations of the steps described in the feature files.
  • tags = "@smoketest": Tags are used to filter which tests to run. In this case, only the scenarios tagged with @smoketest will be executed.
  • plugin = {"pretty", "html:target/cucumber-reports.html"}: This specifies the reporting plugins to use. The html plugin generates a simple html test report.
  • monochrome = true: This option makes the console output more readable by removing unnecessary characters and colors.

Tag Expressions

Cucumber supports tag expressions to combine multiple tags using logical operators:

AND

  • The AND operator in Cucumber tag expressions is used to run scenarios that are tagged with multiple tags simultaneously. For instance, consider the tags @login and @critical. By using the expression @login and @critical, Cucumber will execute only those scenarios that are tagged with both @login and @critical. This is particularly useful when you need to focus on tests that are both essential and pertain to specific functionalities.
				
					@CucumberOptions(
    features = "src/test/resources/features/home-screen.feature",
    glue = "com.kicksapp.stepdefs",
    tags = "@login and @critical",
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
				
			

OR

  • The OR operator broadens the scope by running scenarios that match any of the specified tags. For example, using @moketest or @critical will run all scenarios tagged with either @smoketest or @critical. This approach is beneficial when you want to cover a broader range of tests without being too restrictive, ensuring that all relevant functionalities are tested.
				
					@CucumberOptions(
    features = "src/test/resources/features/home-screen.feature",
    glue = "com.kicksapp.stepdefs",
    tags = "@smoketest or @critcal",
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
				
			

NOT

  • The NOT operator is employed to exclude specific tags from the test run. For example, the expression not @ignore will execute all scenarios except those tagged with @ignore. This is especially useful for excluding tests that are either known to fail, are under development, or are not currently relevant to the test cycle.
				
					@CucumberOptions(
    features = "src/test/resources/features/home-screen.feature",
    glue = "com.kicksapp.stepdefs",
    tags = "@smoketest and not @ignore",
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
				
			

Conclusion

You’ve mastered the art of using tags. Tags allow you to categorize your scenarios, making it easier to run specific tests based on your needs. Whether you’re focusing on a particular feature or running regression tests, tags streamline the process, saving you valuable time.

Now that you are comfortable with tags, it’s time to start Understanding Cucumber Tags.

Leave a Comment

Share this Doc

Understanding Cucumber Tags

Or copy link

CONTENTS
Review Your Cart
0
Add Coupon Code
Subtotal
Total Installment Payments
Bundle Discount