如何在多个类上使用范围报告代码并合并结果(动态类加载)

时间:2019-09-10 10:31:35

标签: c# api automation nunit extentreports

我只是API自动化和范围报告中的新手。请你帮助我好吗。我创建了一个单独的ExterntReportsDemo.cs文件,并为每种功能创建了多个测试类。例如login.cs,registration.cs文件。现在,我想在login.cs,registration.cs中使用我的ExterntReportsDemo.cs文件。并提供一个报告文件。

    using AventStack.ExtentReports;
    using AventStack.ExtentReports.Reporter;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

namespace APIAutomation.Resource
{
    class ExtentReportingDemo
    {
        protected ExtentReports _extent;
        protected ExtentTest _test;

    [OneTimeSetUp]
    protected void Setup()
    {
        var dir = TestContext.CurrentContext.TestDirectory + "\\";
        var fileName = this.GetType().ToString() + ".html";
        var htmlReporter = new ExtentHtmlReporter(@"D:\CG Project\APIAutomation\ExtentReport.html");

        _extent = new ExtentReports();
        _extent.AttachReporter(htmlReporter);
    }

    [OneTimeTearDown]
    protected void TearDown()
    {
        _extent.Flush();
    }

    [SetUp]
    public void BeforeTest()
    {
        _test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
    }

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

        switch (status)
        {
            case TestStatus.Failed:
                logstatus = Status.Fail;
                break;
            case TestStatus.Inconclusive:
                logstatus = Status.Warning;
                break;
            case TestStatus.Skipped:
                logstatus = Status.Skip;
                break;
            default:
                logstatus = Status.Pass;
                break;
        }

        _test.Log(logstatus, "Test ended with " + logstatus + stacktrace);
        _extent.Flush();
    }
}

}

login.cs文件

using APIAutomation.Base;
using APIAutomation.Models;
using APIAutomation.Resource;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using RestSharp;
using RestSharp.Serialization.Json;
using System;
using Assert = NUnit.Framework.Assert;

namespace APIAutomation.TestCases
{
[TestFixture]
public class ResetPSWD
{
    Settings settings = new Settings();
    public const string email = "commongenius.qa3@gmail.com";
    public const int verfCode = 0975;
    public const string newPaswd = "0b9f1803667b0707f2485a47fe1fb259f21d90646ec725911e60640c0b052710";
    readonly string BaseURL = baseurl.FinalURL + "/resetPassword";
    JsonDeserializer deserial = new JsonDeserializer();
    protected ExtentReports _extent;
    protected ExtentTest _test;

    [Test]
    public void GetVerfCode()
    {
        _test = _extent.CreateTest("GetVerfCode");
        //var htmlReporter = new ExtentHtmlReporter(@"D:\CG Project\APIAutomation\ExtentReport.html");
        //htmlReporter.Configuration().Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;

        //var extent = new ExtentReports();
        //extent.AttachReporter(htmlReporter);

        //extent.CreateTest("GetVerfCode").Info("Test Started.");           

        //var client = new RestClient(baseurl.FinalURL + "/getVerificationCode");            

        settings.BaseUrl = new Uri(BaseURL);
        settings.RestClient.BaseUrl = settings.BaseUrl;
        settings.Request = new RestRequest(Method.POST);

        settings.Request.AddHeader("Content-Type", "application/json");
        settings.Request.AddBody(new ResetPswdModel() { email = email });

        settings.Response = settings.RestClient.Execute(settings.Request);

        RootObject returnData = deserial.Deserialize<RootObject>(settings.Response);

        int statusCode = (int)settings.Response.StatusCode;
        Console.WriteLine("Response Code: " + statusCode);
        Assert.AreEqual(200, statusCode);

        var contentType = settings.Response.ContentType;
        Console.WriteLine("Content Type: " + contentType);
        Assert.AreEqual("application/json;charset=UTF-8", contentType);
        var description = returnData.info[0].description;
        Console.WriteLine("Status Description: " + description);
        //extent.Flush();

    }

    //Reset Pswd without Email.
    [Test]
    public void ResetPswdWithoutEmail()
    {
        _test = _extent.CreateTest("ResetPswdWithoutEmail");
        settings.BaseUrl = new Uri(BaseURL);
        settings.RestClient.BaseUrl = settings.BaseUrl;

        settings.Request = new RestRequest(Method.POST);

        settings.Request.AddHeader("Content-Type", "application/json");
        settings.Request.RequestFormat = DataFormat.Json;
        settings.Request.AddBody(new ResetPswdModel() { verificationCode = verfCode, newPassword = newPaswd });

        settings.Response = settings.RestClient.Execute(settings.Request);

        RootObject returnData = deserial.Deserialize<RootObject>(settings.Response);

        //Compare the status code.
        int statusCode = (int)settings.Response.StatusCode;
        Console.WriteLine("Status Code: " + statusCode);
        Assert.AreEqual(200, statusCode);

        //Check the Content Type.
        Console.WriteLine("The Content Type is: " + settings.Response.ContentType);
        Assert.AreEqual("application/json;charset=UTF-8", settings.Response.ContentType);

        //Check the Description.
        Console.WriteLine("The Description is: " + returnData.info[0].description);
        Assert.AreEqual("Please enter your email address", returnData.info[0].description);

        //Check the Info status code.
        int InfoStatusCode = (int)returnData.info[0].status;
        Console.WriteLine("From Info Status Code: " + InfoStatusCode);
        Assert.AreEqual(126100, InfoStatusCode);
    }

0 个答案:

没有答案