Behavior Driven Development using Cucumber and Test NG

I have started playing with Test NG and Behavior Driven Development using Cucumber. Here are my notes from playing with Cucumber and Test NG.

In Eclipse, Click Help > Eclipse Marketplace

Enter into the Find Box – Cucumber

Click Go

You should see Eclipse plugin.

Click Install

When presented with the Review License dialog, click I Accept

Click Finish

Click Install anyway

Note, I personally clicked details and found they are the Eclipse Cucumber features

/Applications/Eclipse.app/Contents/Eclipse/plugins/cucumber.eclipse.editor_0.0.22.201806032001.jar /Applications/Eclipse.app/Contents/Eclipse/features/cucumber.eclipse.feature_0.0.22.201806032001

/Applications/Eclipse.app/Contents/Eclipse/plugins/cucumber.eclipse.steps.integration_0.0.22.201806032001.jar

/Applications/Eclipse.app/Contents/Eclipse/plugins/cucumber.eclipse.steps.jdt_0.0.22.201806032001.jar

You can always click on the job to see the status.  In the lower right, you are going to see the install progress

Click Restart Now

Be sure to save your work.

Once Eclipse has restarted, click Create > Maven Project

Click Create a simple project

Click Next

Click Advanced

Enter Group Id – cucumber

Enter Artifact Id – test

Enter Version – 99-SNAPSHOT

Click Finish

Right Click the Project – test

Select Configure -> Convert to Cucumber Project

Open the pom.xml and add a dependency for Cucumber and Test NG

     <dependencies>

          <dependency>

               <groupId>io.cucumber</groupId>

               <artifactId>cucumber-java</artifactId>

               <version>4.7.1</version>

               <scope>test</scope>

          </dependency>

          <dependency>

               <groupId>io.cucumber</groupId>

               <artifactId>cucumber-testng</artifactId>

               <version>4.7.1</version>

               <scope>test</scope>

               <exclusions>

                     <exclusion>

                          <groupId>junit</groupId>

                          <artifactId>junit</artifactId>

                     </exclusion>

               </exclusions>

          </dependency>

          <dependency>

               <groupId>org.testng</groupId>

               <artifactId>testng</artifactId>

               <version>6.14.3</version>

               <scope>test</scope>

          </dependency>

     </dependencies>

 

While in the pom.xml, make sure the version of the project is Java 1.8.

     <properties>

          <maven.compiler.source>1.8</maven.compiler.source>

          <maven.compiler.target>1.8</maven.compiler.target>

     </properties>

Click File > Save

Right Click New > Class

Enter Name – RunCucumberTest

Click Browse

Find AbstractTestNGCucumberTests

Click OK

Click OK

Annotate the class with

@CucumberOptions(plugin = "pretty", features = "src/test/resources/TestMe.feature")

It’s a good reminder of where you can go to modify Cucumber.

Right Click the Project > New > Other

Select Cucumber Step Definition File

Enter the Package Name – cucumber

Enter the Class Name – TestMe

Select a few of the annotations@Given @Then @And

Click Finish

Note, I changed the packageio.cucumber.” to the newest approaches.

Replace the content of TestMe.java

package cucumber;

import org.testng.Assert;

import io.cucumber.java.en.And;

import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

public class TestMe {

     private int value;

     private int percentage;

     @Given("^I have \"([^\"]*)\"$")

     public void given(String start) throws Throwable {

          this.value = Integer.parseInt(start);

     }

     @And("^I want to add a \"([^\"]*)\"$")

     public void and(String increase) throws Throwable {

          this.percentage = Integer.parseInt(increase);

     }

     @Then("^I should have \"([^\"]*)\"$")

     public void then(String target) throws Throwable {

          Assert.assertEquals(value * percentage, Integer.parseInt(target));

     }

}

The class tests the given, and, then predicates with a simple Assert.  You can always use the TestNG Assert.

Right Click on the TestMe file > Run Configuration

Select Cucumber Feature

Enter Name – TestMe.feature

Replace the Contents with the following:

Feature: TestMe

  Scenario Outline: use the API

    Given I have "<start>"

    And I want to add a "<increase>"

    Then I should have "<total>"

  Examples:

    | start | increase | total |

    |    100 |   100 |    10000 |

Right Click on the Project

Select Run As

Select maven test

You should see

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< cucumber:test>----------------------------
[INFO] Building test 99-SNAPSHOT
[INFO] --------------------------------[ jar]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test ---
[INFO] Surefire report directory: /Users/paulbastide/eclipse-workspace/test/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Running TestSuite

Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@123772c4

Feature: TestMe
  Scenario Outline: use the API with Java7 style # src/test/resources/TestMe.feature:2

    Given I have "<start>"
    And I want to add a "<increase>"
    Then I should have "<total>"

    Examples:

  Scenario Outline: use the API with Java7 style # src/test/resources/TestMe.feature:9
    Given I have "100"                           # TestMe.given(String)
    And I want to add a "100"                    # TestMe.and(String)
    Then I should have "10000"                   # TestMe.then(String)

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.336 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.644 s
[INFO] Finished at: 2019-08-04T13:03:15-04:00
[INFO] ------------------------------------------------------------------------

This is the scenario which you can use, mix and match with other inputs to drive an optimal testing experience.

 

Next step, to figure out how I can use this approach in my healthcare API projects and increase efficacy of my products.

References

Cucumber Eclipse https://cucumber.github.io/cucumber-eclipse/

Cucumber Test NG – https://github.com/cucumber/cucumber-jvm/tree/master/testng

Auto Populated Feature file – https://github.com/cucumber/cucumber-eclipse/wiki/Auto-Generation-of-Sample-Feature-Definition-Template

Step Definition https://github.com/cucumber/cucumber-eclipse/wiki/New-Step-Definition-File-Wizard

Maven Standard Directory Layout http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Test NG and Cucumber – https://medium.com/agile-vision/cucumber-bdd-part-2-creating-a-sample-java-project-with-cucumber-testng-and-maven-127a1053c180

Test https://cucumber.io/docs/guides/10-minute-tutorial/

Examples https://cucumber.io/docs/bdd/example-mapping/

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.