DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • View Test Results in Grafana - Part 1
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Solid Testing Strategies for Salesforce Releases
  • Why We Still Struggle With Manual Test Execution in 2025

Trending

  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Measuring the Impact of AI on Software Engineering Productivity
  • How to Practice TDD With Kotlin
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Monitoring and Observability
  4. View Test Results in Grafana - Part 2

View Test Results in Grafana - Part 2

In this article, see how to upload test results to Influx DB using TestNG Listeners and configure Grafana to view the data

By 
Gowthamraj Palani user avatar
Gowthamraj Palani
·
Jun. 05, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
14.7K Views

Join the DZone community and get the full member experience.

Join For Free

In the 1st part of this series, we set up Grafana and InfluxDB for our test reports. Now, as a follow up to that, we are going to see how to store test results in database and how to view the results in Grafana.

I am going to use Scala with TestNG framework in this demo. For those who are working in Java, it is mostly the same as Scala.

InfluxDB Dependency 

As the 1st step, add InfluxDB dependency to the project. Add the below line to your build.sbt file.

Scala
xxxxxxxxxx
1
 
1
libraryDependencies += "org.influxdb" % "influxdb-java" % "2.18"


Add the below plugin to the plugins.sbt:

Scala
xxxxxxxxxx
1
 
1
addSbtPlugin("de.johoop" % "sbt-testng-plugin" % "3.1.1")


Add Listeners

In the test project, now we are going to add TestNG Listeners to store data in InfluxDB. In the below code, I had

  •  implemented all methods of an interface "ITestListener"

In the postTestMethodStatus and postTestRunStatus methods, I am creating 2 measurements (tables) testmethod and testrun. 1st one is to store the tests and 2nd one is to store the runs. 

The listeners will be called for events like on test passed, on test failed, on test skipped and at the end of test run etc. 

I am updating "testmethod" table after every passed/ failed/ skipped test case and am updating the "testrun" table after every test run. 

Scala
xxxxxxxxxx
1
71
 
1
2
import org.testng.ITestListener
3
import org.influxdb.dto.Point
4
import org.testng.ITestContext
5
import org.testng.ITestResult
6
import java.util.concurrent.TimeUnit
7
8
9
class InfluxDBListener extends ITestListener {
10
11
  final val startTime = 1000
12
  var failedMethods: Int = 0
13
14
  def onTestStart(iTestResult: ITestResult): Unit = {
15
  }
16
17
  def onTestSuccess(iTestResult: ITestResult): Unit = {
18
19
    this.postTestMethodStatus(iTestResult, "PASS")
20
  }
21
22
  def onTestFailure(iTestResult: ITestResult): Unit = {
23
24
    this.postTestMethodStatus(iTestResult, "FAIL")
25
    this.failedMethods+=1
26
  }
27
28
  def onTestSkipped(iTestResult: ITestResult): Unit = {
29
30
    this.postTestMethodStatus(iTestResult, "SKIPPED")
31
  }
32
33
  def onTestFailedButWithinSuccessPercentage(iTestResult: ITestResult): Unit = {
34
  }
35
36
  def onStart(iTestContext: ITestContext): Unit = {
37
  }
38
39
  def onFinish(iTestContext: ITestContext): Unit = {
40
    this.postTestClassStatus(iTestContext)
41
    this.postTestRunStatus
42
  }
43
44
  private def postTestMethodStatus(iTestResult: ITestResult, status: String): Unit = {
45
    val point = Point.measurement("testmethod")
46
                .time(System.currentTimeMillis, TimeUnit.MILLISECONDS)
47
                .tag("testRun",startTime)
48
                .tag("testclass", iTestResult.getTestClass.getName)
49
                .tag("name", iTestResult.getName)
50
                .tag("description", iTestResult.getMethod.getDescription)
51
                .tag("result", status)
52
                .addField("duration", iTestResult.getEndMillis - iTestResult.getStartMillis)
53
                .build
54
    UpdateResults.post(point)
55
  }
56
57
58
  private def postTestRunStatus : Unit = {
59
    var testRunStatus = "Pass"
60
    if (this.failedMethods>0)
61
      testRunStatus = "Fail"
62
63
    val point = Point.measurement("testrun")
64
      .time(System.currentTimeMillis, TimeUnit.MILLISECONDS)
65
      .addField("name",startTime)
66
      .tag("status", testRunStatus)
67
      .build
68
    UpdateResults.post(point)
69
  }
70
}
71


Create connection to DB and update the database using the below code. 

Scala
xxxxxxxxxx
1
16
 
1
package utils
2
3
import org.influxdb.InfluxDBFactory
4
import org.influxdb.dto.Point
5
6
object UpdateResults {
7
  val INFLXUDB = InfluxDBFactory.connect("http://localhost:8086", "root", "root")
8
  val DB_NAME = "test_results"
9
  INFLXUDB.setDatabase(DB_NAME)
10
11
  def post(point: Point): Unit = {
12
13
    INFLXUDB.write(point)
14
  }
15
}
16


Include listeners to your testng.xml file.

XML
xxxxxxxxxx
1
13
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3
<suite name="My suite">
4
    <listeners>
5
        <listener class-name="utils.InfluxDBListener" />
6
    </listeners>
7
    <test name="Test">
8
        <classes>
9
            <class name="tests.CreateGoogleAccount"/>
10
        </classes>
11
    </test>
12
</suite>
13


Now run your tests. 

Shell
xxxxxxxxxx
1
 
1
sbt test


After the test execution, you can see the data of your tests populated in testrun and testmethod tables as below.

Our data is ready. 

Data is ready

Configure Grafana

Now let's take a case I need a graph that should show the number of runs that are passed and failed. To draw a chart, for me, data from "testrun" table is enough. 

In your Grafana home page, click on "Build a dashboard" or "Create" icon from left pane. This will create an empty dashboard for you. Now, click on "add panel" icon at the header. We will be adding visual representations in the panels.

Adding visualizations

Click on choose vizualisation and choose Pie-Chart. If you don't find the plugin, follow this to install.

Installing plugin

Set up the query like below, as per your requirement. 

Setting up the requirement

The graphs are real-time and will be updated as soon as there is data-change. 

Dashboard with some other graphs 

Dashboard with other graphs

That's it, now you don't need to share test-results with your clients or to your team through an email or in a shared folder. Results will be updated here after every test-run.

You can find sample project here.

Happy Reading!!

Testing Grafana

Opinions expressed by DZone contributors are their own.

Related

  • View Test Results in Grafana - Part 1
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Solid Testing Strategies for Salesforce Releases
  • Why We Still Struggle With Manual Test Execution in 2025

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!