mardi 28 avril 2020

How to use Class initialize and Class Cleanup only once in the framework passing test context

I have integrated log4net and extent reports together for logging. I wanted to pass Test Method name as the name of the log file so in order to do that I used to pass Test context in Class Initialize method before every test case run.

the script looks like this:

    [TestClass]
    public class ToverifySequenceProperty : BaseSetup
    {
        BaseSetup baseobj = new BaseSetup();
        BaseDeveloper_CF devobj = new BaseDeveloper_CF();
       // public TestContext TestContext { get; set; }
       // public static string logfilename;


        [TestMethod]
        public void VerifySequenceProperty()
        {
            try
            {

                baseobj.LocateParentWindowbyName("SystemModeler17 - Microsoft Visual Studio  (Administrator)");
                htmlLogging.HTMLReportLogging("Info", "Prent Window Located Successfully");
                var projectnode = "SystemModeler17 [(OS 2200) Standard mode] on SystemModeler17";
                devobj.AddElement(projectnode, "Folder", "fol1");
                htmlLogging.HTMLReportLogging("Info", "Element Added Successfully");
                devobj.AddElement("fol1", "Segment", "seg1");
                htmlLogging.HTMLReportLogging("Info", "Element Added Successfully");
                devobj.AddElement("seg1", "Ispec", "ispec1");
                htmlLogging.HTMLReportLogging("Info", "Element Added Successfully");
                devobj.AddElement("ispec1", "Attribute", "Attribute1");
                htmlLogging.HTMLReportLogging("Info", "Element Added Successfully");
                devobj.AddElement("ispec1", "Attribute", "Attribute2");
                htmlLogging.HTMLReportLogging("Info", "Element Added Successfully");
                devobj.AddElement("ispec1", "Attribute", "Attribute3");
                htmlLogging.HTMLReportLogging("Info", "Element Added Successfully");
                devobj.DeleteElement("Attribute1");
                htmlLogging.HTMLReportLogging("Info", "Element Deleted Successfully");
                devobj.CheckSequenceError();
                htmlLogging.HTMLReportLogging("Info", "Sequence error checked Successfully");
                devobj.ResolveSequenceError("ispec1");
                htmlLogging.HTMLReportLogging("Info", "Sequence Error Resolved Successfully");
            }
            catch (Exception ex)
            {
                htmlLogging.HTMLReportLogging("Error", ex.ToString());
            }

        }

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            test = extent.CreateTest($"{context.TestName}");
            logfilename = $"{context.TestName}";
            Setup();
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            TearDown();
            Directory.CreateDirectory(@"C:\LogFiles");
            File.Move(@"C:\temp\default.log", @"C:\LogFiles\" + logfilename + ".log");

        }
    }

Now I am adding this Class initialize and Class Cleanup method in all the Test Class Files. Is there a way I can use this only once and it automatically gets run for every test class. I tried making a constructor in BaseSetup class but then context is null there. How to pass the test context of the test classes i am not getting that part.

Aucun commentaire:

Enregistrer un commentaire