In this tutorial we will discuss to create a JUnit project using IntelliJ. We will be at first creating a simple Java Project and will add JUnit5 as well as create a Maven Project, then we will add a basic Class and a JUnitTest for it.
Step 2 – Right click on the project and select Open Module Settings.
Step 3 – Go to the “Libraries” group, click the little plus (look up), and choose “From Maven…” option.
Step 4 – Search for “junit” — something like “junit:junit-4.13“. Click the “OK“ button.
Step 5– A new dialog will appear to confirm that “junit:junit:4.13.2” will be added to the module. Click the “OK“ button.
Step 6 – This screens shows that junit:junit:4.13.2 is added to the Libraries. It contains the highlighted classes – junit-4.13.2.jar and hamcrest-core-1.3.jar. Click the “OK” button.
Step 7– This image shows that the Junit is added to the External Libraries.
Step 8– Create a Java Class – JUnit4Test under src and create a JUnit test to verify that it is installed properly.
import org.junit.Assert;
import org.junit.Test;
public class JUnit4Test {
@Test
public void Test() {
String str1 = "Happy";
String str2 = new String("Happy");
Assert.assertEquals("String1 and String 2 are equal",str1, str2);
}
}
Step 9– There are many ways to run the test. One of the way is to Right-Click and select Run JUnit4Test
The successful execution of the test shows that the JUnit is configured properly.
Create a Maven Project
Add Junit dependency to the POM.xml and build the project.
Now we need to apply the changes in the build script. Press Ctrl+Shift+O or click Load Maven Changes in the notification that appears in the top-right corner of the editor.
Create a Java Class – JUnit4Test under src/test/javaand create a JUnit test to verify that it is installed properly.
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class JUnitMavenTest {
@Test
public void Test() {
String[] expected = {"happy","days","summer","spring"};
String[] actual = {"happy","days","summer","spring"};
assertArrayEquals("Expected and Actual Arrays are not equal",expected,actual);
}
}
The output of the above program is
Similarly, to add JUnit5 we can add below mentioned dependencies to the POM.xml.
Selenium needs Java to be installed on the system to run the tests. Click here to know How to install Java.
Step 2 – Download and setup Eclipse IDE on the system
The Eclipse IDE (integrated development environment) provides strong support for Java developers. Click here to know How to install Eclipse.
Step 3 – Setup Gradle
To build a test framework, we need to add several dependencies to the project. This can be achieved by any build tool. I have used Gradle Build Tool. Click here to know How to install Gradle.
Step 4 – Create a new Gradle Project
Below are the steps to create the Gradle project from command line.
Step 5 – Add Selenium and TestNG dependencies to the Gradle project
dependencies {
// Use TestNG framework, also requires calling test.useTestNG() below
testImplementation 'org.testng:testng:7.10.0'
// This dependency is used by the application.
implementation libs.guava
implementation 'org.seleniumhq.selenium:selenium-java:4.24.0'
}
Step 6 – Add Gradle Test Task to build.gradle
tasks.named('test') {
// Use TestNG for unit tests.
useTestNG() {
useDefaultListeners = true
outputDirectory = file("$projectDir/TestNG_Reports")
}
reports.html.setDestination(file("$projectDir/GradleReports"))
}
The complete gradle.build looks like something shown below.
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.10/userguide/building_java_projects.html in the Gradle documentation.
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'java'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use TestNG framework, also requires calling test.useTestNG() below
testImplementation 'org.testng:testng:7.10.0'
// This dependency is used by the application.
implementation libs.guava
implementation 'org.seleniumhq.selenium:selenium-java:4.24.0'
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
tasks.named('test') {
// Use TestNG for unit tests.
useTestNG() {
useDefaultListeners = true
outputDirectory = file("$projectDir/TestNG_Reports")
}
reports.html.setDestination(file("$projectDir/GradleReports"))
}
Step 7 – Create Test Code under src/test/java
Let us write the code to test a web application. I have created 3 tests and out of 3, 1 test will fail intentionally.
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;
public class LoginTests {
WebDriver driver;
@BeforeMethod
public void setUp() {
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
driver.get("https://opensource-demo.orangehrmlive.com/");
}
@Test(description = "This test validates error message when credentials are incorrect", priority = 0)
public void verifyIncorrectCredentials() {
driver.findElement(By.name("username")).sendKeys("Admin");
driver.findElement(By.name("password")).sendKeys("admin123$$");
driver.findElement(By.xpath("//*[@class='oxd-form']/div[3]/button")).submit();
String actualErrorMessage = driver.findElement(By.xpath("//*[@class='orangehrm-login-error']/div/div/p")).getText();
// Verify Error Message
Assert.assertEquals(actualErrorMessage,"Invalid credentials");
}
@Test(description = "This test will fail", priority = 1)
public void verifyBlankCredentials() {
driver.findElement(By.name("username")).sendKeys("");
driver.findElement(By.name("password")).sendKeys("admin123$$");
driver.findElement(By.xpath("//*[@class='oxd-form']/div[3]/button")).submit();
String actualErrorMessage = driver.findElement(By.xpath("//*[@class='oxd-form-row']/div/span")).getText();
// Verify Error Message
Assert.assertEquals(actualErrorMessage,"Invalid credentials");
}
@Test(description = "This test validates successful login to Home page", priority = 2)
public void verifyLoginPage() {
driver.findElement(By.name("username")).sendKeys("Admin");
driver.findElement(By.name("password")).sendKeys("admin123");
driver.findElement(By.xpath("//*[@class='oxd-form']/div[3]/button")).submit();
String homePageHeading = driver.findElement(By.xpath("//*[@class='oxd-topbar-header-breadcrumb']/h6")).getText();
//Verify new page - HomePage
Assert.assertEquals(homePageHeading,"Dashboard");
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
Step 8 – Create testng.xml
Right-click on the project and select TestNG and select Convert to TestNG.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Selenium Tests with TestNG">
<classes>
<class name="org.example.LoginTests"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Step 9 – Run the tests from TestNG
Right-Click on the testng.xml and select Run As TestNG Suite.
The output of the above tests in Eclipse Console is as shown below.
This also generates a folder with the name test-output that contains the TestNG reports like index.html,emailable-report.html.
Step 10 – Run the tests from Command Line
To run the tests from the command line, use the below-mentioned command.
gradle clean test
The output of the above program is
Step 11 – TestNG and Gradle Report generation
Once the test execution is finished, refresh the project. We will see 2 folders – GradleReports and TestNG_ Reports.
Gradle Reports
This folder contains index.html.
Right-click on index.html and select open with Web Browser. This report shows the summary of all the tests executed. As you can see that Failed tests are selected (highlighted in blue), so the name of the test failed along with the class name is displayed here.
TestNG Reports
Go to TestNG_Reports folder and right-click and open emailable-report.html.
Index.html
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
JUnit is an open source Unit Testing Framework for JAVA. JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
For the successful execution of Agile testing requirements, a perfect test automation tool is required. And there are numerous factors to consider when creating a solid automation framework. One such component is reporting, which not only informs you of the success or failure of the project but also assists you in identifying potential bugs. JUnit is another useful framework that can add the ability to generate reports in Selenium. This tutorial explains the steps to generate the JUnit5 Report.
The PATCH method is used to partially modify an existing resource. This operation updates an existing resource but does not require sending the entire body with the request. PUT modifies a record’s information and creates a new record if one is not available, and PATCH updates a resource without sending the entire body of the request. Unlike PUT Request, PATCH does partial update e.g. Fields that need to be updated by the client, only that field is updated without modifying the other field.
We will use the following URL for this Postman tutorial.
https://reqres.in/api/users/2
Sample Request Body
{
"name": "Patch_Test"
}
Implementation Steps:
To create the first PATCH request in Postman, follow the following steps:
Create a Collection
Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.
Step 2: Provide a name to the collection – “API Testing”.
Add a request to the Collection
Step 3: To create a new request, click on “Add a request” if it is a new Collection. Otherwise, click on the 3 dots and select “Add request”.
Step 4: Once you create a new request, then you will get the following window:
Enter the details of request
Step 5: Enter the “name” in the request. Here, the name is “PartiallyUpdateUser”.
Step 6:Enter the “URL” in the address bar.
Step 7:Now, select the “PATCH” request from the list of request methods.
Step 8: Add a Request body to the Post request
For this, select the Body tab.
Now in the Body tab, select raw and select JSON as the format type from the drop-down menu, as shown in the image below. This is done because we need to send the request in the appropriate format that the server expects. Copy and paste the request body example mentioned at the beginning of the tutorial to the postman request Body.
Step 9: Press the “Send” button.
Verify the Response
Step 10: Once you press the send button, you will get the response from the server. Make sure you have a proper internet connection; otherwise, you will not get a response.
Status
You can check the status code. Here, we got the status code 200, which means we got a successful response to the request. In the case of new resource creation, the status code should be 201. But as this is a dummy API, we are getting a status code of 200.
Body
In the Body tab of the response box, we have multiple options to see the response in a different format.
Format Type
Each request has a defined response to it as defined by the Content-Type header. That response can be in any format. Such as in the above example, we have JSON code file.
Below are the various format type present in Postman.
XML
HTML
Text
Headers
Headers are the extra information that is transferred to the server or the client. In Postman, headers will show like key-value pairs under the headers tab. Click on the Headers link as shown in the below image:
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.
Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.
Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.
Click on the “Create” Button.
Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.
Below is the image of the new project created in PyCharms.
Step 2 – Create a new directory in the new project
Right-Click on the project, select New->Directory and provide name as Tests
Below is the image of the new directory.
Right-click on the new directory and select New File and provide the name as HeadlessChrome_Demo.robot and Headlessas shown below:
Step 3 – Execute tests in headless mode
We are now going to write test cases. The test case details will be as follows −
Below is an example of executing Chrome tests in headless mode.
*** Settings ***
Documentation To validate the Login Form
Library SeleniumLibrary
*** Test Cases ***
Validate Unsuccessful Login
Open the Browser with URL
Fill the login form
verify error message is correct
*** Keywords ***
Open the Browser with URL
Open Browser https://opensource-demo.orangehrmlive.com/web/index.php/auth/login headlesschrome
Maximize Browser Window
Set Selenium Implicit Wait 5
Fill the login form
Input Text css:input[name=username] Admin
Input Password css:input[name=password] Admin
Click Button css:.orangehrm-login-button
verify error message is correct
${result}= Get Text CSS:.oxd-alert-content-text
Should Be Equal As Strings ${result} Invalid credentials
Below is an example of executing Firefox tests in headless mode.
*** Settings ***
Documentation To validate the Login Form
Library SeleniumLibrary
*** Test Cases ***
Validate Unsuccessful Login
Open the Browser with URL
Fill the login form
verify error message is correct
*** Keywords ***
Open the Browser with URL
Open Browser https://opensource-demo.orangehrmlive.com/web/index.php/auth/login headlessfirefox
Maximize Browser Window
Set Selenium Implicit Wait 5
Fill the login form
Input Text css:input[name=username] Admin
Input Password css:input[name=password] Admin
Click Button css:.orangehrm-login-button
verify error message is correct
${result}= Get Text CSS:.oxd-alert-content-text
Should Be Equal As Strings ${result} Invalid credentials
All the below-mentioned keywords are derived from SeleniumLibrary except the last one. The functionality of keywords mentioned above:
1. Open Browser − The keyword opens a new browser instance to the optional URL.
2. Maximize Browser Window – This keyword maximizes the current browser window.
3. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.
4. Input Text − This keyword is used to type the given text in the specified textbox identified by the locator name:username.
5. Input Password – This keyword is used to type the given text in the specified password identified by the locator name:password.
The difference compared to Input Text is that this keyword does not log the given password on the INFO level.
6. Click button – This keyword is used to click on the button with location css:.orangehrm-login-button.
7. ${result} – This is a variable that holds the text value of the error message that is located by css:.oxd-alert-content-text
8. Get Text – This keyword returns the text value of the element identified by located by css:.oxd-alert-content-text.
9. Should Be Equal As Strings – This keyword is used from builtIn keyword. This keyword returns false if objects are unequal after converting them to strings.
To run this script, go to the command line and go to directory tests.
Step 4 – Execute the tests
We need the below command to run the Robot Framework script.
robot .
The output of the above program is
Step 5 – View Report and Log
We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.
Let us now see the report and log details.
Report
Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).
The Report generated by the framework is shown below:
Log
Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.
Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.
Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.
Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.
Click on the “Create” Button.
Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.
Below is the image of the new project created in PyCharms.
Step 2 – Create a new directory in the new project
Right-Click on the project, select New->Directory, and provide the name as Tests
Below is the image of the new directory.
Right-click on the new directory, select New File and provide the name as Drag_And_Drop_Demo.robot as shown below:
Step 3 – Download ChromeBinaries from the below location
The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.
The chromedriver and geckodriver are placed in a folder named drivers in the RobotFramework_Demo project. I have renamed chromedriver to Chrome and geckodriver to Firefox.
Step 4 – Automate the drag and drop option
We are now going to write test cases. The test case details will be as follows −
Open the Browser with the URL
Verify element Text before drag
Drag the element and drop
Verify element Text after drag
To work with the Radio Button, we need a locator. A locator is an identifier for the textbox like id, name, class, xpath, css selector, etc.
To know more about locators, refer to these Selenium Tutorials:
Let us inspect the locator of the drag and drop option.
Below is an example of a drag and drop option.
*** Settings ***
Documentation To drag the box
Library SeleniumLibrary
*** Test Cases ***
Verify that the user can drag and drop elements
[documentation] This test case verifies that a user can drag and drop an element from source to destination
Open the Browser with URL
Verify element Text before drag
Drag the element and drop
Verify element Text after drag
*** Keywords ***
Open the Browser with URL
Create Webdriver Chrome executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/Chrome
Go To https://demoqa.com/droppable
Maximize Browser Window
Set Selenium Implicit Wait 5
Verify element Text before drag
Element Text Should Be id:droppable Drop here timeout=5 #Before Drag and Drop
Drag the element and drop
Drag And Drop id:draggable id:droppable
Verify element Text after drag
Element Text Should Be id:droppable Dropped! timeout=5 #After Drag and Drop
All the below-mentioned keywords are derived from SeleniumLibrary except the last one. The functionality of keywords mentioned above:
1. Create Webdriver − The keyword creates an instance of Selenium WebDriver.
2. Go To – This keyword navigates the current browser window to the provided URL – https://demoqa.com/droppable.
3. Maximize Browser Window – This keyword maximizes the current browser window.
4. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.
5. Element Text Should Be – This keyword is used to verify that the current page contains the exact text identified by the locator.
6. Drag And Drop – Drags the element identified by the locator into the target element. The locator argument is the locator of the dragged element and the target is the locator of the target.
7. Drag And Drop By Offset – Drags the element identified with locator by xoffset/yoffset. The element will be moved by xoffset and yoffset, each of which is a negative or positive number specifying the offset.
Step 5 – Automate the Drag and Drop by Offset option
Drag And Drop By Offset – Drags the element identified with the locator by xoffset/yoffset.
*** Settings ***
Documentation To drag the box
Library SeleniumLibrary
Test Teardown Close Browser
*** Test Cases ***
Verify that the user can drag and drop element by offset
[documentation] This test case verifies that a user can drag and drop an element by offset
Open the Browser with URL
Verify element Text before drag
Drag the element and drop locator by xoffset/yoffset
Verify no change in Text
*** Keywords ***
Open the Browser with URL
Create Webdriver Chrome executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/Chrome
Go To https://demoqa.com/droppable
Maximize Browser Window
Set Selenium Implicit Wait 5
Verify element Text before drag
Element Text Should Be id:droppable Drop here timeout=5 #Before Drag and Drop
Drag the element and drop locator by xoffset/yoffset
Drag And Drop By Offset id:draggable 50 70
Verify no change in Text
Element Text Should Be id:droppable Drop here timeout=5 #After Drag and Drop
Before
After
Step 6 – Execute the tests
We need the below command to run the Robot Framework script.
robot Drag_Drop_Demo.robot
The output of the above program is
Step 7 – View Report and Log
We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.
Let us now see the report and log details.
Report
Right-click on report.html. Select Open In->Browser->Chrome (any browser of your wish).
The Report generated by the framework is shown below:
Log
Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.
Right-click on log.html. Select Open In->Browser->Chrome (any browser of your wish).
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Jenkin’s installed and started on the computer. The current Jenkins version is – 2.361.2
Implementation Steps
To generate HTML Report in Jenkins, we need to download HTML Publisher Plugin. Please refer to this tutorial to install the plugin – How to install Plugins in Jenkins.
Step 1: Create a new Maven project
Give the Name of the project – HTMLReport_Demo
Click on the Maven project.
Click on the OK button.
In the General section, enter the project description in the Description box.
Select Source Code Management as None if the project is locally present on the machine.
Step 2: Build Management
Go to the Build section of the new job.
In the Root POM textbox, enter the full path to pom.xml
In the Goals and Options section, enter “clean test site“
Here, I have used the Selenium project with JUnit, so to see the complete project, please refer to this tutorial – How to generate JUnit4 Report.
Click on the Advanced button.
Step 3: Select a custom workspace
Mention the full path of the project in the directory.
Step 4: Select “Publish HTML reports” from “Post Build Actions”
Scroll down to “Post Build Actions” and click on the “Add Post Build Actions” drop-down list. Select “Publish HTML reports“.
If you want to see where the report is saved in Jenkins, go to the Dashboard ->HTMLReport_Demo project -> Workspace ->target -> site -> surefire-report.html.
Enter the below details to publish HTML reports
HTML directory to archive – target/site/
Index page[s] – surefire-report.html
Report title – HTML Report
Click on the Apply and Save buttons.
We have created a new Maven project “HTMLReport_Demo” with the configuration to run the Selenium with JUnit4 Tests and also to generate HTML Report after execution using Jenkins.
Step 5: Execute the tests
Let’s execute it now by clicking on the “Build Now” button.
Right-click on Build Number (here in my case it is #2).
Click on Console Output to see the result.
Step 6: View the HTML Report
Once the execution is completed, click on go “Back to Project“, and we can see a link to view the “HTML Report“.
We can see here that the HTML Report link is displayed in the Console.
Below is the HTML Report generated in Jenkins.
We can see that the HTML Report does not look very pretty. The reason is that CSS is stripped out because of the Content Security Policy in Jenkins.
We can customize the Content Security Policy in Jenkins. But keep in mind that it should be done after checking with the Security team in your organization. This is a workaround solution. I can’t emphasize enough that this is not a standard practice.
Go to Manage Jenkins -> Manage Nodes and Clouds.
Click on the Script Console option.
Type in the following command and Press Run. If you see the output as ‘Result:’ then the protection is disabled. Re-run your build and you can see that the new HTML files archived will have the CSS enabled.
In the previous tutorial, I explained the Integration of the Allure Report with Rest Assured with JUnit4. In this tutorial, I will explain how to Integrate Allure Report with Rest Assured and TestNG.
The below example covers the implementation of Allure Report for Rest API using Rest Assured, TestNG, Java, and Maven.
Step 4 – Create the Test Code for the testing of REST API under src/test/java
To see our request and response in more detail using Rest Assured, we need to add a line to our Rest Assured tests. This will provide the request and response details in the report.
.filter(new AllureRestAssured())
import io.qameta.allure.*;
import io.qameta.allure.restassured.AllureRestAssured;
import io.restassured.http.ContentType;
import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
@Epic("REST API Regression Testing using TestNG")
@Feature("Verify CRUID Operations on User module")
public class RestAPITests {
@Test(description = "To get the details of user with id 3", priority = 0)
@Story("GET Request with Valid User")
@Severity(SeverityLevel.NORMAL)
@Description("Test Description : Verify the details of user of id-3")
public void verifyUser() {
// Given
given()
.filter(new AllureRestAssured())
// When
.when()
.get("https://reqres.in/api/users/3")
// Then
.then()
.statusCode(200)
.statusLine("HTTP/1.1 200 OK")
// To verify user of id 3
.body("data.email", equalTo("emma.wong@reqres.in"))
.body("data.first_name", equalTo("Emma"))
.body("data.last_name", equalTo("Wong"));
}
@Test(description = "To create a new user", priority = 1)
@Story("POST Request")
@Severity(SeverityLevel.NORMAL)
@Description("Test Description : Verify the creation of a new user")
public void createUser() {
JSONObject data = new JSONObject();
data.put("name", "RestAPITest");
data.put("job", "Testing");
// GIVEN
given()
.filter(new AllureRestAssured())
.contentType(ContentType.JSON)
.body(data.toString())
// WHEN
.when()
.post("https://reqres.in/api/users")
// THEN
.then()
.statusCode(201)
.body("name", equalTo("RestAPITest"))
.body("job", equalTo("Testing"));
}
}
Step 5 – Create testng.xml for the project
<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite1">
<test name = "TestNG Test Demo">
<classes>
<class name = "org.example.RestAPITests"/>
</classes>
</test>
</suite>
Step 6 – Run the Test and Generate Allure Report
To run the tests, use the below command
mvn clean test
In the below image, we can see that all three tests are passed.
This will create the allure-results folder with all the test reports. These files will be used to generate the Allure Report.
To create an Allure Report, use the below command
allure serve
This will generate the beautiful Allure Test Report as shown below.
Allure Report Dashboard
The overview page hosts several default widgets representing the basic characteristics of your project and test environment.
Categories in Allure Report
The categories tab gives you a way to create custom defect classifications to apply for test results. There are two categories of defects – Product Defects (failed tests) and Test Defects (broken tests).
Suites in Allure Report
On the Suites tab a standard structural representation of executed tests, grouped by suites and classes, can be found.
View test history
Each time you run the report from the command line with the mvn clean test command, a new result JSON file will get added to the allure-results folder. Allure can use those files to include a historical view of your tests. Let’s give that a try.
To get started, run mvn clean test a few times and watch how the number of files in the allure-reports folder grows.
Now go back to view your report. Select Suites from the left nav, select one of your tests and click Retries in the right pane. You should see the history of test runs for that test:
Graphs in Allure Report
Graphs allow you to see different statistics collected from the test data: status breakdown or severity and duration diagrams.
Timeline in Allure Report
Timeline tab visualizes retrospective of tests execution, allure adaptors collect precise timings of tests, and here on this tab, they are arranged accordingly to their sequential or parallel timing structure.
Behaviors of Allure Report
This tab groups test results according to Epic, Feature, and Story tags.
Packages in Allure Report
The packages tab represents a tree-like layout of test results, grouped by different packages.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
As we know, REST Assured is a Java DSL for simplifying the testing of REST-based services built on top of HTTP Builder. In this tutorial, I’ll create a Test Framework for the testing of REST API using REST Assured and TestNG as the test framework.
Java needs to be present on the system to run the tests. Click here to know How to install Java. To know if Java is installed or not on your machine, type this command in the command line. This command will show the version of Java installed on your machine.
java -version
Step 2 – Download and setup Eclipse IDE on the system
The Eclipse IDE (integrated development environment) provides strong support for Java developers, which is needed to write Java code. Click here to know How to install Eclipse.
Step 3 – Setup Maven
To build a test framework, we need to add a number of dependencies to the project. It is a very tedious and cumbersome process to add each dependency manually. So, to overcome this problem, we use a build management tool. Maven is a build management tool that is used to define project structure, dependencies, build, and test management. Click here to know How to install Maven.
To know if Maven is already installed or not on your machine, type this command in the command line. This command will show the version of Maven installed on your machine.
To know more about priority in TestNG, please refer tothis tutorial.
import io.restassured.http.ContentType;
import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class RestAPITests {
@Test(description = "To get the details of user with id 3", priority = 0)
public void verifyUser() {
// Given
given()
// When
.when()
.get("https://reqres.in/api/users/3")
// Then
.then()
.statusCode(200)
.statusLine("HTTP/1.1 200 OK")
// To verify user of id 3
.body("data.email", equalTo("emma.wong@reqres.in"))
.body("data.first_name", equalTo("Emma"))
.body("data.last_name", equalTo("Wong"));
}
@Test(description = "To create a new user", priority = 1)
public void createUser() {
JSONObject data = new JSONObject();
data.put("name", "RestAPITest");
data.put("job", "Testing");
// GIVEN
given()
.contentType(ContentType.JSON)
.body(data.toString())
// WHEN
.when()
.post("https://reqres.in/api/users")
// THEN
.then()
.statusCode(201)
.body("name", equalTo("RestAPITest"))
.body("job", equalTo("Testing"));
}
}
Step 7 – Test Execution through TestNG
Go to the Runner class and right-click Run As TestNG Test. The tests will run as TestNG tests. (Eclipse)
Right-click on the test page and select Run ‘RestAPITests’ in thecase of Intellij.
This is how the execution console will look like. (IntelliJ)
Eclipse
Step 8 – Run the tests from TestNG.xml
Create a TestNG.xml as shown below and run the tests as TestNG. Here, the tests are present in class – com.example. Selenium_TestNGDemo.API_Test.
<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite1">
<test name = "TestNG Test Demo">
<classes>
<class name = "org.example.RestAPITests"/>
</classes>
</test>
</suite>
Step 9 – TestNG Report Generation
After the test execution, refresh the project, and a new folder with the name test-output will be generated. This folder contains the reports generated by TestNG. The structure of folder test-output looks as shown below.
Emailable-report.html
We are interested in “emailable-report.html” report. Open “emailable-report.html”, as this is an HTML report, open it with the browser. The below image shows emailable-report.html.
Index.html
TestNG also produces “index.html” report, and it resides under the test-output folder. The below image shows the index.html report. This report contains a high-level summary of the tests.