How to Integrate Extent Report in a .NET C# Project
Importance of Reports in Software Testing
Reports are the heart and soul of software testing. Whether it is a simple website or a complex VR game, we simply can’t release the software without proper testing. Reports help us keep track of all the issues involved and their development. We can easily identify the core of an issue if everything is documented properly.
They also help us identify the vulnerabilities in an application. One of the major advantages of having proper documentation is code readability. Since there are many teams involved in the development of software, reports ease the communication process by providing all the vital information.
For instance, if a bug is found in a project in testing, the testing team has to inform the development team about the bug. They do so by giving a detailed report on what the bug is, where it is present, and other relevant details like its impact on the application, and sometimes even how to solve it.
Since it is such an important process, there are many libraries to create automated reports. One such library is Extent Report. The Extent Report has become one of the most popular reporting library used in a short span of time.
Generating the Extent Report
Now, I will be showing you how we create a real-time extent report mechanism in a .Net project, which will allow us to create the report at run time without having to mention test names. It will also help us write try-catch for every test case we have in our test class.
Since Nunit does not have any type of listener like in TestNG, we have the most commonly used ITestListener. We will use Nunit’s [OneTimeSetUp], [OneTimeTearDown] and [SetUp] features to do the magic. For this demo, I will be using Visual Studio 2019 and .Net Core Nunit test project.
First, we need to make sure our project has the necessary Nuget packages. We need the packages mentioned below. However, you don’t have to use the same version. However, I do recommend using the latest versions to avoid any compatibility issues.
● Selenium.Chrome.WebDriver (85.0.0)
● Selenium.WebDriver (3.141.0)
● ExtentReports (4.1.0)
You can download these from the Nuget package manager and reference them in your project.
After you install all the Nuget packages, you should see them in the packages subdirectory.
Note: If you used a Nunit Test Project (.Net Core), you will get the Nunit and Nunit3 packages by default. If you use any other project template, you may need to install Nunit from NuGet package manager. Also, I highly recommend you use Nunit Test Project (.Net Core) template for all your test automation projects as this is cross-platform.
Now for step two, we will be creating a TestBase class where we will define four important methods. We will also define two global variables ‘extent’ and ‘testlog’. The four methods that we will be using are:
- StartReport() — This method is used to define the path for our report and create the report
- EndReport() — This method is used to call others for logging the results
- LoggingTestStatus() — This method is responsible for the logic with which the reports are logged.
- StartExtentTest() — This method is responsible to know which test you want the extent report to log
Before we write our test, create a folder at the project root level and name it ‘Reports’ as this is where the extent report will be stored. Adding contextually appropriate names is a good coding practice and will ease the maintenance process. So, first, we are going to create a simple Webdriver test to generate the report.
For this demo, I have chosen to use Google and search for an item and close the browser. First, we extend TestBase to use the method ‘StartExtentTest’ and call this in our setup method. I have declared a private global variable ‘testName’ which will hold our test name and it will be generated at run time using the Nunit TestContext class.
This way we can avoid hard coding the name and also avoid calling the method repeatedly in each test. Imagine you have twenty test cases in one class. If we hardcoded the name, we will have to call the method twenty times as well. Using a dynamic name will make this process much easier and faster.
Well, that’s all you need to do, and you can add as many tests as you need after the test execution. Once that’s done, you will have the extent report in the “Reports” folder.
I hope, everyone finds this blog useful for the convenient and surprising easy-to-setup test execution results.
Please do follow me on Medium for more upcoming blogs on real-world use cases/usage of the latest tools and technologies for test automation.
Happy Testing! 😊