TestNG and Cucumber (Configuration,execution and simple comparison):
What is TestNG
TestNG is a Open source Automation Framework for JAVA. In TestNG- NG means for Next Generation. It Is used by developers and Testers in Test case creation. It has ease of using annotations, grouping, dependence, prioritization and parameterization features.
Step 1:
Installation of TestNG:
- Install Eclipse IDE from eclipse website.
- Once Eclipse installed, go to help and navigate to Market Place.
- Look for TestNG for Eclipse.
- Click the install button.
- Once the installation done,restart the Eclipse.
- To verify Check by right clicking on a project name and Try changing it to the TestNG class.
Step 2:
Create a sample TestNG project:
- Create a Java Project
- Add TestNG libraries to the project by navigating to the library tab.
- Create the TestNG class in the src folder with TestNG annotations.
Step3:
TestNG Annotations:
- Test annotations are tags that describes about method,class and suite. It is where we explain the execution approach of the test cases and features associated to it.
- To include annotations in the selenium code,we need to import testng annotations with the package
“import org.testng.annotations.Test”
3. The Important and most used Annotations are
- @BeforeMethod and @AfterMethod — These annotations run before and after each test method
- @BeforeClass and @AfterClass — These annotations run once before and after the first @test method in a class
- @BeforeTest and @AfterTest — The BeforeTest annotation runs before the @BeforeClass.
- @AfterTest annotation runs after the @AfterClass annotation.
- @BeforeSuite and @AfterSuite– These annotations run before and after any test annotated method in a class respectively. These annotations start the beginning of a test and the end of it, for all the classes in a suite
Annotations execution Order:
@BeforeSuite ->
@BeforeTest ->
@BeforeClass ->
@BeforeMethod ->
@Test ->
@AfterMethod ->
@AfterClass ->
@AfterCTest ->
@AfterSuite ->
According to the need of the Project, The annotations are used. TestNG class will be created with a schema as below:
package testng;
import org.testng.annotations.Test;
public class Testsuite1
{
@Test
public static void Test001()
{ }
}
-> In the above code @Test is also a testNG annotations.
-> No main() method is used while using TestNG. Annotations plays the execution role here.
Step 4:
Executing the TestNG script:
Project name -> Run as -> TestNG test
- TestNG output will be on two windows — 1) Console Window
2) TestNG result Window.
What is Cucumber:
Cucumber is a Framework used for automation testing using selenium. It supports BDD(Behavioral data driven) approach. Cucumber uses Gherkin language as Test scripts.
Configuring the cucumber in Eclipse:
- For configuring the cucumber the jar files are added to the project which can be dowloaded.
- Add the required cucumber Maven dependencies from Maven repository and add it to the pom.xml
Cucumber Framework:
-> Cucumber framework has two steps.
- Feature files — Code is written in simple english(Gherkin language)
- Step definition- Has the actual code.
- Runner file — Code written here to execute the project which includes feature file and step definition file.
Step 1:
Feature file and Gherkin:
Gherkins are simple language test scripts. These scripts can be easily understandable just using three words : Given, When and Then.
Feature files are created src/test/ folder with “.feature”.
Below is the example for login page:
@tag
Feature: Login
I want to use this template for my feature file
@tag1
Scenario: Title of your scenario
Given I want to write a step with precondition
And some other precondition
When I complete action
And some other action
And yet another action
Then I validate the outcomes.
Step 2: Step definition File:
Below is the example of stepdefinition file of login page.
package stepDefinitions;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
public class LoginSD
{
@Given(“I want to write a step with precondition”)
public void i_want_to_write_a_step_with_precondition() {
System.out.println(“Executing preconditions”);
}
@Given(“some other precondition”)
public void some_other_precondition() {
}
@When(“I complete action”)
public void i_complete_action() {
}
@When(“some other action”)
public void some_other_action() {
}
@When(“yet another action”)
public void yet_another_action() {
}
@Then(“I validate the outcomes”)
public void i_validate_the_outcomes() {
}
Step 3: Runner File:
Below is the example for runner class. Execution is done by right clicking on the runner class file and Run as JUnit test or TestNG.
package cucumberOptions;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions( //path of feature file features “src/test/java/features/Login.feature”, //path of step definition file glue = “stepDefination” )
public class TestRunner
{ }
Comparison:
- TestNG used in Unit Testing,Integration Testing, End-to-End Testing where Cucumber is used in Acceptance Testing.
- TestNG is a Testing Framework for Java Programming language where Cucumber Testing tool for Behaviour- driven-development.
- In TestNG multiple test cases can be used in single XML file with defining priorities which test cases need to be executed first. In Cucumber use of tag Cucumber feature files or individual tests to group tests.