locked
How can I log info of a test case whether it is passed or not after the test case execution is done? RRS feed

  • Question

  • I am using selenium webdriver in visual studio Using NUNIT C#. The code of test case is

    public class FMSAutomationClass
    {
        private IWebDriver WDriver;
        private log4net.ILog Testlog;
    
    
        [TestInitialize]
        public void wd()
        {
            WDriver = Helper.GetWebDriver(helperconst.browserType.Chrome);
            Testlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        }
        [Priority(1)]
        [TestMethod]
        public void search()
        {
            Testlog.Info("Test ID:001, This test will select the criteria and update the customer dropdown");
    
            Testlog.Info("Step 1/1 : Select customer from cutomer dropdown");
            var dc = gmethods.GetSelectElement(WDriver, dpo.customermenu);
            dc.SelectByText(dpc.customer);
        }
        [TestCleanup]
        public void CleanUp()
        {
            WDriver.Close();
            WDriver.Quit();
        }
    }

    In dpo page:

    public static class dpo
    {
        public const string customermenu = "[data]";
    }

    In dpc page

    public static class dpc
    {
        public const string customer = "A";
    }

    In gmethods page:

    static public SelectElement GetSelectElement(IWebDriver drv, string elem)
    {
        SelectElement a = new SelectElement(drv.FindElement(By.CssSelector(elem)));
        return a;
    }

    I want to Log that test case is passed or fail in TestLog right after the test case is executed.How can I achieve that?

    Friday, March 20, 2020 12:31 PM

Answers

  • Hi Amna,

    Thanks for your feedback.

    I think this codes could help you.

    if(TestResultKeeper.TestOutcome.ToString().Equals("Failed"))
    {
           //Testlog.Info(5); add other codes here.
    }
    else if(TestResultKeeper.TestOutcome.ToString().Equals("Passed"))
    {
           //Testlog.Info(1); I just use it to test, replace it to where you'd like to save(form maybe).
    }

    Hope this helps.

    Sincerely,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mehwish 001 Monday, April 6, 2020 11:31 AM
    Thursday, April 2, 2020 8:53 AM

All replies

  • I need to capture test case result into an array and pass it into another file having integration code of TestRail and update test Results in TestRail.

    I am using Selenium webdriver NunitC#.

    Tuesday, March 24, 2020 7:13 AM
  • Hi Amna,

    Sorry for delay in reply.

    TestContext class is helpful, I suggest, you could get test result(passed/failed) by using TestContext class and UnitTestOutcome Enum.

    Please refer to codes below(simple and for reference only):

    [TestClass]
        public class UnitTest1
        {
    
            private TestContext TestContext { get; set; }
    
            private log4net.ILog Testlog;
    
            [TestInitialize]
            public void wd()
            {
                //string path="XXXXXX";
                .....
            }
    
            [TestMethod]
            public void TestMethod1()
            {        
                ......
                
                Testlog =log4net.LogManager.GetLogger("testlog");
             
            }
    
            [TestCleanup]
            public void TestCleanup()
            {
                .....
                var context = TestContext.CurrentTestOutcome;
                Testlog.Info(context.ToString());
            }
        }

    See also, Fetch And Store Unit Test Results in a text file? and Understanding the MSTest TestContext.

    Hope this could help you a little.

    Kind Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 24, 2020 8:41 AM
  • Hi Amna,

    Welcome to MSDN forum.

    You could use TestContext Class, UnitTestOutcome Enumeration to get test results programmatically.

    About passing it into another file, you may need to use other tools, like log4net which you mentioned before. And about updating test Results in TestRail, I suggest you could go to TestRail forum and start a new thread there to seek for a more professional help.

    Hope this could help you a little.

    Kind Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 25, 2020 2:15 AM
  • My testcase is not executing. It stars executing and then not run state appear.
    Wednesday, March 25, 2020 1:01 PM
  • I am using MStest C# in selenium webdriver. My code is

    public class MainProjectFIle
    {
        private IWebDriver WDriver;
        private log4net.ILog Testlog;
    
    
        [TestInitialize]
        public void wd()
        {
            WDriver = Helper.GetWebDriver(helperconst.browserType.Chrome);
            Testlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        }
        [Priority(1)]
        [TestMethod]
        public void search()
        {
            Testlog.Info("Test ID:001, This test will select the criteria and update the customer dropdown");
    
            Testlog.Info("Step 1/1 : Select customer from cutomer dropdown");
            var dc = gmethods.GetSelectElement(WDriver, dpo.customermenu);
            dc.SelectByText(dpc.customer);
        }
        [TestCleanup]
        public void CleanUp()
        {
            WDriver.Close();
            WDriver.Quit();
        }
    }

    In dpo page:

    public static class dpo
    {
        public const string customermenu = "[data]";
    }

    In dpc page

    public static class dpc
    {
        public const string customer = "A";
    }

    In gmethods page:

    static public SelectElement GetSelectElement(IWebDriver drv, string elem)
    {
        SelectElement a = new SelectElement(drv.FindElement(By.CssSelector(elem)));
        return a;
    }

    In TestRailPO file the code is

       
    public class TestResultKeeper
        {
            public static TestResult TestResult { get; set; }

            public static UnitTestOutcome TestOutcome =>  UnitTestOutcome.Failed;

            //TestResult ?? Outcome ?? UnitTestOutcome.Failed;

            public static bool IsPassed => TestOutcome == UnitTestOutcome.Passed;
            public static Exception Exception { get; set; }
        }

        public class TestMethodAttribute : Attribute
        {
            public virtual TestResult[] Execute(ITestMethod testMethod)
            {
                return new TestResult[] { };
            }
        }

        public class LogTestTestMethod : TestMethodAttribute
        {
            public override TestResult[] Execute(ITestMethod testMethod)
            {
                var testResult = base.Execute(testMethod)[0];


                TestResultKeeper.TestResult = testResult;
                //TestResultKeeper.Exception = testResult.TestFailureException;

                return new[] { testResult };

            }


        }

    I amusing MStest .  I want to saved testcase result in TestResultKeepr. I want to call that result in the form of 1 and 2, 1 for pass 2 for fail To update test case result on runtime in the variable ttresult in TestRailPM file. The code for in testRailPM is

    public class TestRailPM
        {
        public void testrail()
                {
    
                    string testRunID = CreateTestRun.CreateRun(testRailUrl, testRailUser, testRailPassowrd, projectId, suiteId, milestoneId, "Automation of TestCases", "Automated Test Cases", testRailUserId, false, testCaseIds);
                    int testRunIdInInt = Convert.ToInt16(testRunID);
    
                    int[] newTestRunCaseIds = GetTestCases.getTestCaseIds(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, true);
                    int[] originalTestCaseIds = GetTestCases.getTestCaseIds(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, false);
    
    
                    int singleTestCaseId = 98765;
    
                    UpdateTestRun.updateSingleTestCaseInATestRun(ttresult, "testCaseComments", testRailUserId);
       }
    }




    Wednesday, March 25, 2020 1:17 PM
  • Hi Amna,

    Thank you for your feedback.

    It seems you both use MSTest and NUnit test, I suggest you need to confirm which test framework you’d like to use, delete the related references and codes, and just use one of them.

    Any feedback will be expected.

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 26, 2020 1:49 AM
  • I deleted Nunit references. and used MStest only.
    Thursday, March 26, 2020 5:02 AM
  • Hi Amna,

    Sorry for delay in reply.

    It seems you are trying to create a class(TestResultKeeper) and a customized test attribute to store test results, and detect test results for every test method. I think it’s a good way to get test results.

    Not sure if TestRailPM file is another .cs file in the same project with UnitTest.cs file. I suppose you could try call method where you store and get test results directly in  UnitTest.cs file.

    Hope this helps.

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 26, 2020 9:44 AM
  • TestRailPM file is cs file in the same project.

    How can i call the customized test attribute in TestRailPm file to pass that result in ttresult in TestRailpm.cs file ?


    • Edited by Mehwish 001 Thursday, March 26, 2020 1:00 PM
    Thursday, March 26, 2020 12:57 PM
  • Hi Amna,

    Thanks for your feedback.

    Does your project compile well this time? I mean successfully get the test results as the first step.

    If yes, you could try to create a method in another file which you mentioned before, to get test results(call the class/methods which you use to store test results)

    Hope this helps.

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 27, 2020 9:41 AM
  • Hi,

    Could you please guide how can i check that it is successfully get the test results as the first step?Also could you please help how to call customized test attribute in TestRailPm file to pass that result in ttresult in TestRailpm.cs file ? I tried alot but failed. Will appreciate if you provide help by guiding in code. And according to code, where test results should get saved?

    Thanks


    • Edited by Mehwish 001 Monday, March 30, 2020 12:47 PM
    Monday, March 30, 2020 7:51 AM
  • Hi Amna,

    Sorry for delay in reply.

    To confirm if you get the test results, you could use codes like "var context =TestContext.CurrentTestOutcome; Testlog.Info(context.ToString())"

    var context =TestContext.CurrentTestOutcome; 
    Testlog.Info(context.ToString())

    to see the test results in the log file(For me, I just output the test results to my local file to see if I caught it successfully).

    In addition, it seems you confused about the usage of this class: TestMethodAttribute. I notice that you override the TestResult, it will be helpful to catch the test results for every test methods. You just need to do what you used to do to program your code, and consider or use [TestMethodAttribute] as [TestMethod]. I should be more specified, replace [TestMethod] to [TestMethodAttribute], and then catch the test results. See sample codes below.

    namespace UnitTestProject { [TestClass] public class UnitTest1 { public TestContext TestContext { get; set; } private log4net.ILog Testlog; public class TestResultKeeper { public static TestResult TestResult { get; set; } public static UnitTestOutcome TestOutcome => TestResult?.Outcome ?? UnitTestOutcome.Failed; public static bool IsPassed => TestOutcome == UnitTestOutcome.Passed; public static Exception Exception { get; set; } } public class LogTestTestMethodAttribute : TestMethodAttribute { public override TestResult[] Execute(ITestMethod testMethod) { var testResult = base.Execute(testMethod)[0]; TestResultKeeper.TestResult = testResult; TestResultKeeper.Exception = testResult.TestFailureException; return new[] { testResult }; } }

    ..................... [TestInitialize] public void XX() { //...... } [LogTestTestMethod] public void TestMethod() { //Assert..... } [LogTestTestMethod] public void TestMethod2() { //Assert..... } [TestCleanup] public void TestCleanup() { Testlog = log4net.LogManager.GetLogger("testlog"); var context = TestContext.CurrentTestOutcome; Testlog.Info(context.ToString());//You could try to collect the results to other place(array) } } }

    And for calling methods in TestRailpm.cs file, I think use format like “classname XX=new classname(); XX.TestCleanup()“

    classname XX=new classname(); 
    XX.TestCleanup()

    then you could get the test results from the method(here is from TestCleanup(), you could replace it, of course)

    Hope this could help you a little.

    Sincerely,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Tuesday, March 31, 2020 6:11 AM
  • Hi. Thankyou for the efforts but I think I am not able to convey the proper question so I am putting the hierarchy here of my project in the screenshot

    Level1-MainProjectfile

         Level2-Properties

         Level2-Refernces

         Level2-AppObj(folder)

                   Level3-DP(folder)

                            Level4-dpo.cs

                            Level4-dpc.cs

                   Level3-TestRail(folder)

                             Level4-TestRailpm.cs

                             Level4-TestRailpo.cs

                   Level3-gmethods.cs

          Level2-AUtomationCode.cs

          Level2-log4net.config

    Now My Unit Testcases are present in AutomationCode.cs file this is the main project file. The code in my AutomationCode.cs file is 

    public class AutomationCode
    {
        private IWebDriver WDriver;
        private log4net.ILog Testlog;
    
    
        [TestInitialize]
        public void wd()
        {
            WDriver = Helper.GetWebDriver(helperconst.browserType.Chrome);
            Testlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        }
    ////[Ignore]
    
        [Priority(1)]
        [TestMethod]
        public void search()
        {
            Testlog.Info("Test ID:001, This test will select the criteria and update the customer dropdown");
    
            Testlog.Info("Step 1/1 : Select customer from cutomer dropdown");
            var dc = gmethods.GetSelectElement(WDriver, dpo.customermenu);
            dc.SelectByText(dpc.customer);
        }
    ////[Ignore]
            [Priority(2)]
            [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
            public void TestRailCall()
            {
                TestRailpm a = new TestRailPM();
                a.testrail();
            }
    
        [TestCleanup]
        public void CleanUp()
        {
            WDriver.Close();
            WDriver.Quit();
        }
    }

    In dpo.cs file code is 

    public static class dpo
    {
        public const string customermenu = "[data]";
    }

    In dps.cs file code is

    public static class dpc
    {
        public const string customer = "A";
    }

    In Gmethods.cs code is 

    static public SelectElement GetSelectElement(IWebDriver drv, string elem)
    {
        SelectElement a = new SelectElement(drv.FindElement(By.CssSelector(elem)));
        return a;
    }

    In TestRailpo.cs file code is 

    namespace Automation.AppObj.TestRail
    {
        public class TestRailPO
        {
    
            public class TestResultKeeper
            {
                public static TestResult TestResult { get; set; }
                public static UnitTestOutcome TestOutcome => UnitTestOutcome.Failed;
    
                //TestResult ?? Outcome ?? UnitTestOutcome.Failed;
    
                public static bool IsPassed => TestOutcome == UnitTestOutcome.Passed;
                public static Exception Exception { get; set; }
    
                
            }
    
            public class TestMethodAttribute : Attribute
            {
                public virtual TestResult[] Execute(ITestMethod testMethod)
                {
                    return new TestResult[] { };
                }
            }
    
            public class LogTestTestMethod : TestMethodAttribute
            {
    
                public override TestResult[] Execute(ITestMethod testMethod)
                {
                    var testResult = base.Execute(testMethod)[0];
    
                    TestResultKeeper.TestResult = testResult;
                    //TestResultKeeper.Exception = testResult.TestFailureException;
    
                    return new[] { testResult };
    
    
                }
    
            }
        }
    
    
    
    
    

    In TestRailpm.cs file code is 

        public class TestRailpm
        {
     public void testrail()
            {
                string testRunID = CreateTestRun.CreateRun(testRailUrl, testRailUser, testRailPassowrd, projectId, suiteId, milestoneId, "Automation of TestCases", "Automation of TestCases", testRailUserId, false, testCaseIds);
                int testRunIdInInt = Convert.ToInt16(testRunID);
    
                int[] newTestRunCaseIds = GetTestCases.getTestCaseIds(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, true);
                int[] originalTestCaseIds = GetTestCases.getTestCaseIds(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, false);
    
    
    
                int singleTestCaseId = 33666;
                UpdateTestRun.updateSingleTestCaseInATestRun(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, singleTestCaseId, Resultoftestcase, "testCaseComments", testRailUserId);
               }
    
       }

    I am using MStest C#. All i want is to Run my main project file that is AutomationCode.cs and the result of testcase that would be "Pass/Fail" will get saved in a variable or attribute etc that is in TestRailpo.cs file testkeeperresult or any other attribute. Of course the result saved would be either pass or fail but here is the main thing. I need to pass that result in the form of numbers that is 1 or 5. 1 for pass and 5 for Fail. That result i need to pass in TestRailpm.cs file in 'Resultoftestcase'.

    UpdateTestRun.updateSingleTestCaseInATestRun(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, singleTestCaseId, Resultoftestcase, "testCaseComments", testRailUserId);
               }

    I have called the TestRail() method from TestRail.pm file to AutomationCode.cs before testcleanup because I want to update TestRail after my unit test cases are executed. Keeping in view of my detailed description kindly help me in code to pass the result in the form of 1 or 5 in testRail.pm file. Please guide me how can I do that and what changes would be required?


    • Edited by Mehwish 001 Tuesday, March 31, 2020 1:13 PM
    Tuesday, March 31, 2020 1:00 PM
  • Hi Amna,

    Thanks for your feedback.

    I think this codes could help you.

    if(TestResultKeeper.TestOutcome.ToString().Equals("Failed"))
    {
           //Testlog.Info(5); add other codes here.
    }
    else if(TestResultKeeper.TestOutcome.ToString().Equals("Passed"))
    {
           //Testlog.Info(1); I just use it to test, replace it to where you'd like to save(form maybe).
    }

    Hope this helps.

    Sincerely,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mehwish 001 Monday, April 6, 2020 11:31 AM
    Thursday, April 2, 2020 8:53 AM