Thursday, May 18, 2023

api in c#

 using System;

using NUnit.Framework;

using RestSharp;

using Newtonsoft.Json;

using AventStack.ExtentReports;

using AventStack.ExtentReports.Reporter;


namespace APITesting

{

    [TestFixture]

    [Parallelizable(ParallelScope.Fixtures)]

    public class APITest

    {

        private static ExtentReports extent;

        private ExtentTest test;

        private RestClient client;


        [OneTimeSetUp]

        public static void Setup()

        {

            // Initialize Extent Reports

            extent = new ExtentReports();

            var htmlReporter = new ExtentHtmlReporter("extent-report.html");

            extent.AttachReporter(htmlReporter);

        }


        [SetUp]

        public void BeforeTest()

        {

            // Create a new test in the Extent Report

            test = extent.CreateTest(TestContext.CurrentContext.Test.Name);


            // Set the base URL of the API

            string baseUrl = "https://api.example.com";

            

            // Create a RestSharp client

            client = new RestClient(baseUrl);

        }


        [Test]

        [TestCaseSource(nameof(UserData))]

        public void TestAPI(string username, string password)

        {

            // Create a RestSharp request

            RestRequest request = new RestRequest("/endpoint", Method.GET);


            // Add any necessary headers, parameters, or body to the request

            // request.AddHeader("Authorization", "Bearer <token>");

            // request.AddParameter("paramName", "paramValue");


            // Send the request and get the response

            IRestResponse response = client.Execute(request);


            // Extract the response details

            int statusCode = (int)response.StatusCode;

            string content = response.Content;


            // Deserialize the response content to a data model

            var responseModel = JsonConvert.DeserializeObject<ResponseModel>(content);


            // Log the response details in the extent report

            test.Log(Status.Info, $"API Response Code: {statusCode}");

            test.Log(Status.Info, $"API Response Content: {content}");


            // Perform assertions or further processing on the response

            Assert.AreEqual(200, statusCode, "Unexpected status code");


            // Add a pass status to the extent report

            test.Pass("API test passed");

        }


        [TearDown]

        public void AfterTest()

        {

            // End the test in the extent report

            extent.EndTest(test);

        }


        [OneTimeTearDown]

        public static void TearDown()

        {

            // Flush and close the extent report

            extent.Flush();

            extent.Close();

        }


        private static object[] UserData()

        {

            return new object[]

            {

                new object[] { "user1", "password1" },

                new object[] { "user2", "password2" },

                new object[] { "user3", "password3" },

                new object[] { "user4", "password4" },

                new object[] { "user5", "password5" }

            };

        }

    }


    public class ResponseModel

    {

        // Define properties for the response data

        [JsonProperty("id")]

        public int Id { get; set; }


        [JsonProperty("name")]

        public string Name { get; set; }


        // Add other properties as needed

    }

}


No comments:

Post a Comment