top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

TestNG Vs Cucumber Frameworks

Introduction:

Functional testing is pivotal before any feature release for a software application, to check if the feature is working as per the business requirements. While there are different frameworks that are used to perform functional testing of web applications, this article compares TestNG and Cucumber to highlight when to use which.


TestNG:

TestNG is a testing framework inspired from JUnit and NUnit, but introducing some new functionalities that make it more powerful and easier to use. TestNG is an open source automated testing framework; where NG means Next Generation.


Cucumber:

Cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to write acceptance tests for a web application. It allows automation of functional validation in easily readable and understandable format (like plain English) to Business Analysts, Developers, Testers, etc.


Cucumber vs TestNG: Which is better?



In Automation, both are used to test smallest testable parts of an application, called units.

Cucumber features(tests) are written in plain english language which is actually a Gherkin language.

Another point is cucumber can be implemented with web driver-implementation in java, ruby, php, python etc.

Implementing cucumber with your tests form BDD (behaviour driven development) framework.

Unit testing is a part of the test-driven developthement (TDD) methodology that requires developers to first write failing unit tests. Then they write code in order to change the application until the test passes.

Nowadays, unit testing is done with BDD(Behavior Driven Development) which is extension of TDD where user or tester create the automated specifications in plain english language and developer write the underlying code that implements the test.

Developers describe Cucumber as "Simple, human collaboration". Cucumber is a tool that supports Behaviour-Driven Development (BDD) - a software development process that aims to enhance software quality and reduce maintenance costs. On the other hand, TestNG is detailed as "A testing framework inspired from JUnit and NUnit". It is a testing framework designed to simplify a broad range of testing needs, it covers all categories of tests: unit, functional, end-to-end, integration, etc.Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc.

  • Cucumber and TestNG can be primarily classified as "Testing Frameworks" tools.

  • Cucumber is an open source tool with 2.58K GitHub stars and 502 GitHub forks.

Cucumber can be executed in parallel using **TestNG and Maven test execution plugins** by setting the **data provider parallel option to true**. In TestNG the **scenarios and rows in a scenario outline are executed in multiple threads**. One can use either Maven Surefire or Failsafe plugin for executing the runners.

How to write tests in TestNG?

Let’s take a look at the unit test case where we will be checking if the given PIN is correct or not in a banking application:

Step 1: Writing the business logic of the test

  • The core logic which needs to be tested with the required scenarios is placed here

  • Check the length of the given input PIN

  • This is marked by the annotation @Test

Step 2: Insert appropriate TestNG annotations

There might be a requirement where a task needs to be repeatedly done before running each test case. TestNG can be used to automate it. TestNG provides annotations like @BeforeTest and @AfterTest

In this case, since it is required to open the banking app, enter the PIN and close the baking app after every test, hence using annotations as follows:

  • @BeforeTest – Open the Banking app

  • @Test – Has the business logic to verify if the given PIN is valid or not

  • @AfterTest – Closes the banking app

Step 3: Create an XML file with required groups and classes, where tests need to be executed by using any modern IDE tool such as IntelliJ or Eclipse.

Step 4: Run TestNG

How to write tests in Cucumber?

In Cucumber, the tests are written files that have an extension called .feature written in Gherkin. A .feature file contains a feature defining high-level functionality and scenarios describing individual test cases and how they behave within a feature. A particular feature can have multiple scenarios.


Writing scenarios:

The scenarios in Cucumber framework can be written in the following format “Given-When-Then”.

Example:

This example showcases a feature called “checking the bank balance from the banking app”.

The feature functionality can be described as

User logins to the banking app,

Selects the check balance option,

Bank balance displays when a user enters the correct 4-digit PIN.


Feature: Checking bank balance from the banking app.

Scenario 1: Check the balance by entering the correct PIN.

Given user opens the banking application and selects Check Balance

When user enters PIN as 1234

Then bank balance should be displayed


Scenario 2: Check the balance by entering 5-digit PIN

Given user opens the banking application and selects Check Balance

When user enters PIN as 12345

Then app should show a notification that an incorrect pin has been entered


These kinds of tests are easily understood by non-technical users (business stakeholders) as it has been written descriptively.


Conclusion:

By analyzing the differences between TestNG and Cucumber, here’s how you can decide when to use which:

  • When there is a need to test an important feature that needs to be understood by the business stakeholders, Cucumber can be used.

  • To test a technically implemented complex business logic/algorithm where behavior is not a deciding factor TestNG can be used.

!!!!!!Happy Learning !!!!!

1,926 views0 comments

Recent Posts

See All

Salesforce Backup

Salesforce Backup is part of the Salesforce data management and security portfolio. Salesforce backup can automatically create backup copies of your data and allow you to restore in future if needed i

bottom of page