Search This Blog

Wednesday, 7 October 2015

Configure Cucumber (BDD) in Java

Cucumber Configuration Steps:

Configuring Cucumber BDD tool in Java can be achieved by following the below simple steps: 

Step 1: Install the following softwares
             Java (JDK And JRE) 
             Java IDE - To write the code 
                Maven - Centralized repository for all the jar files


Step 2: Create a simple Maven Project in the Java IDE                    
                The Maven project structure looks like this:
                                <<ProjectName>>
                                |-- pom.xml
                                `-- src
                                       |-- main
                                       |   |-- java
                                       |   `-- resources
                                       `-- test
                                              |-- java
                                              `-- resources



Step 3: (Optional step in most of the IDE. Mandatory in ItelliJIdea Ide)   
                Src -> main -> java - as Source root
                Src -> test -> java - as Test source root
                Src -> test -> resources - as "Resources for Test Source"

Add the following maven dependencies to the POM:
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-java</artifactId>
                                <version>1.2.2</version>
                </dependency>
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-core</artifactId>
                                <version>1.2.2</version>
                </dependency>
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-junit</artifactId>
                                <version>1.2.2</version>
                </dependency>
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-picocontainer</artifactId>
                                <version>1.2.2</version>
                </dependency>

Alternatively: If working on a non-maven project, download the respective jar files and add them to the project build path

Action: Save the POM.xml and clean install
Observation:  This should automatically download the required jar files into the Maven repository.
                                               
Step 4: Create files to resemble the folder structure of the project like below:
projectName
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |         `-- packageName
    |               `-- Sample.java
    `-- test
        |-- java
        |     `-- packageName
        |                      |-- RunCukesTest.java
        |           `-- Definitions.java
        `-- resources
              `-- packageName
                    `-- demo.feature


Details of the files:
1. Demo.feature is the Cucumber feature file.
                File Description: This contains the cucumber steps in Given When Then format
2. Defintions.java is the bridge between the actual java code and the GWT (Given When Then) format files.
   File Description: This contains the references to the functions which are to be invoked for every step. In general, this type of file(s) can be called as <<featureName>>StepDefs.java
3. RunCukesTest.java is the configuration file which holds details of the "Features Files"
4. Sample.java contains the actual java code. This code can be referenced from definitions.java

The source code of these files will be discussed in detailed

Step 5:
 (Assuming that the code is placed in the respective files)
1.       Open RunCukesTest.java
2.       Run the file as Junit Test
                               
                Observation:
This should trigger the execution.
                        As a result, the test steps defined in "demo.feature" are invoked.
                        Compiler finds the respective step definitions from "Definitions.java"
                        Defintions.java calls the functions/executes the logic to complete the test



Tuesday, 6 October 2015

BDD - Behaviour Driven Development


Need for BDD:

To understand the need of BDD, let us throw some light on “What in case of Absence of BDD”.
1. Requirement Analyst writes Acceptance Tests to determine if the product is doing right Thing
2. Developer writes Unit Tests to determine if the code is doing things RIGHT
3. Automation test script (using any tool) to cater regression/functional testing
               
Now, we have three different documentation/levels of definitions i.e. with Business, Developer and QA teams respectively for the same Application under development.

- There are high chances that, the gap between "Business requirement", "Development Specification" and "Acceptance Scenarios"
                - This is a big disadvantage, which ends up in more time to deliver a quality product.

BDD unites all the three definitions into ONE, which eventually minimizes the gap between Requirements, Developer & QA.

Advantages of BDD:
ü  Well defined documentation technique, which conveys the requirement to all the stake holders involved in the complete Software Development Life Cycle in a very simple language.
ü  The steps written in BDD could be understood by Business, Requirement Team,  Acceptance Team, Quality Assurance (Manual and Automation) and also to the developers.

BDD Format
                BDD follows a very simple format using simple keywords.
                Keywords like "Given", "When", "Then", "AND".
                                Given - used to define a precondition
                                When - used to define Actual test step
                                Then - used to define Observation (Expected behaviour)
                                And - used to connect two steps (Anywhere)
These keywords can be used to convey the requirement in a very simplified manner to avoid confusions. 


See an example here


The cucumber format can clearly convey the requirement to the entire stake holders, as they are very understandable.
Cucumber format removes the gap between the "Functional Specifiction", "Business Requirement Document", "Acceptance Specifications", etc