1

I have been handed an existing framework that uses allure reports for reporting purposes. I am not really liking allure reports for primarily a couple of reasons:

  • It displays the results only after the whole suite has run. So the waiting time is a lot and at times ends up wasted as well!

-I apparently can't print and log the data created at each test step. So in the framework I have to check the console output to check the data.

How do I switch to extent reports without any risk of breaking anything. I have heard that extent reports is better and more User friendly. Does allure offer solution to the aforementioned two problems?

2
  • Can you please provide more input here, whether you are printing statement on console directly or creating separate log file. Jun 4, 2018 at 17:37
  • 1
    What's your tech stack? Jun 16, 2018 at 22:28

2 Answers 2

0

In your existing framework , you may write a function to write data in a file at runtime instead of console ,for this you don't need any additional fancy framework.

Although assuming if you are using Java with TestNG,you may add the maven dependencies and further classes of your new framework.And further you can create and call the object with reporter function in your framework assertion calls.

As far as impacting any existing framework is concerned, you may maintain multiple frameworks at the same time as long as keeping both reporter function calls in your assertion method in main automation framework.

0

You can easily implement the extent class and extend it in your test classes. Since your framework's environment configurations could vary, you will need to customize the OneTimeSetUp method accordingly. Additionally, to adhere to current automation standards for a robust test automation strategy, it's advisable to incorporate Extent reporting.

I have implemented it in NUnit framework using C#. You can follow the similar approach, if you are using any other programming language in Automation.

Extent Base Class.

using APITestAutomation.APIObjects.REST;
using APITestAutomation.Utilities;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using System.IO;

namespace APIAutomationTest.Utilities
{
public class ExtentBase
{
    public static ExtentReports extent;
    public static ExtentTest test;


    [OneTimeSetUp]
    public void Setup()
    {
        // Get the current date and time to create a unique folder
        string timestamp = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");

        // Get the environment name from the ConfigurationManager
        string environment = ConfigurationManager.Environment;

        string workingDirectory = Environment.CurrentDirectory;
        string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;

        // Specify the path for the Extent report in the "target" folder
        var reportPath = Path.Combine(projectDirectory, $"{timestamp}_Html_Report_{environment}", "target", "automation-report.html");

        var htmlReporter = new ExtentSparkReporter(reportPath);
        extent = new ExtentReports();
        extent.AttachReporter(htmlReporter);
        // Add system information (including the environment name) to the report with HTML formatting
        extent.AddSystemInfo("Environment", $"<span style='color:red; font-weight:bold; font-family:Arial; font-size:40px;'>{environment}</span>");

    }

    [SetUp]
    public void BeforeTest()
    {
        // Get the current test name
        var testName = TestContext.CurrentContext.Test.Name;

        // Get the environment name from the ConfigurationManager
        string environment = ConfigurationManager.Environment;

        // Append the environment to the test name
        testName = $"{testName}_{environment}";

        // Create the test with the modified name
        test = extent.CreateTest(testName);
    }


    [TearDown]
    public void AfterTest()
    {
        var status = TestContext.CurrentContext.Result.Outcome.Status;
        var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
            ? ""
            : string.Format("<pre>{0}</pre>", TestContext.CurrentContext.Result.StackTrace);

        Status logstatus;
        // Get the current test name
        var testName = TestContext.CurrentContext.Test.Name;

        Logger.LogTestCaseName(testName); // Log test case name

        switch (status)
        {
            case TestStatus.Failed:
                // Log the failure reason from your assertion
                logstatus = Status.Fail;
                var failureReason = TestContext.CurrentContext.Result.Message;
                test.Log(logstatus, $"Test ended with {logstatus}. Failure Reason: {failureReason}");

                // Add the stacktrace to the report
                test.Log(logstatus, "Stack Trace: " + stacktrace);

                Logger.LogError($"Test ended with {logstatus}. Failure Reason: {failureReason}");

                // Add the stacktrace to the report
                Logger.LogError("Stack Trace: " + stacktrace);

                break;

            case TestStatus.Inconclusive:
                logstatus = Status.Warning;
                test.Log(logstatus, "Test ended with " + logstatus + stacktrace);
                logstatus = Status.Warning;
                Logger.LogInfo("Test ended with " + logstatus + stacktrace);
                break;

            case TestStatus.Skipped:
                logstatus = Status.Skip;
                test.Log(logstatus, "Test ended with " + logstatus);
                Logger.LogInfo("Test ended with " + logstatus);
                break;

            default:
                logstatus = Status.Pass;
                test.Log(logstatus, "Test ended with " + logstatus);
                Logger.LogInfo("Test ended with " + logstatus);
                break;
        }
        extent.Flush();
    }

    [OneTimeTearDown]
    public void TearDown()
    {
        extent.Flush();
    }
}
}

Here's an example of a test class where I've validated both the response code and ensured the absence of a null body. Fluent Assertions offer a sophisticated approach to conducting these validations.

Test Class:

using APIAutomationTest.APIObjects.REST.JsonBody;
using APITestAutomation.APIObjects.REST;
using APITestAutomation.Utilities;
using NUnit.Framework;
using FluentAssertions;
using System.Net;
using APIAutomationTest.Utilities;
using APIAutomationTest.APIObjects;

namespace APITestAutomation.Tests
{

[TestFixture]
public class ApiTests : ExtentBase
{
    private ApiUtils apiUtils;

    [OneTimeSetUp]
    public void Setup()
    {
        apiUtils = new ApiUtils();
    }

    [Test]
    [Category("Smoke")]
    public void GetSingleUser()
    {
        // Use apiUtils to make API requests
        var response = apiUtils.ExecuteGetRequest(ApiEndpoints.SingleUser);
        response.StatusCode.Should().Be(HttpStatusCode.OK);
        var responseBody = apiUtils.DeserializeResponse<SingleUsers>(response);
        responseBody.Data.Should().BeNull(because: "The 'Data' property should not be null.");

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.