/// <summary>
        /// TestCase results
        /// </summary>
        /// <param name="testStatus"></param>
        public void TestCaseResults(TestContext testContext)
        {
            TestStatus    testStatus    = testContext.Result.Outcome.Status;
            ResultAdapter resultAdapter = testContext.Result;

            object[]      endTestCaseParameterArray;
            List <string> testResults = new List <string>();
            TestResult    testResult  = TestResult.Inconclusive;

            Type                 myClass = Assembly.GetCallingAssembly().GetType(className);
            MethodInfo           EndOfTestCaseExecution = myClass.GetMethod("EndOfTestCaseExecution");
            TestBaseClassService myObj = (TestBaseClassService)Activator.CreateInstance(myClass);

            try
            {
                if (testStatus == TestStatus.Passed)
                {
                    if (Initialize.DisplayExecutionTimeInLogger)
                    {
                        watch.Stop();
                        timeElapsed = watch.ElapsedMilliseconds.ToString();
                    }

                    //Logging testcase details after pass
                    ContextLogger.LogAfterTestCasePass(moduleName, className, testCaseName, timeElapsed, testStatus.ToString());
                    endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, true };
                    EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);
                    testResult = TestResult.Passed;
                }

                else if (testStatus == TestStatus.Failed)
                {
                    //Logging if test case fails
                    if (Initialize.DisplayExecutionTimeInLogger)
                    {
                        watch.Stop();
                        timeElapsed = watch.ElapsedMilliseconds.ToString();
                    }

                    //Logging testCase details after fails
                    ContextLogger.LogAfterTestCaseFails(moduleName, className, testCaseName, testDataService.TestData, timeElapsed, testStatus.ToString());
                    endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                    EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);
                    testResult = TestResult.Failed;
                    testResults.Add(resultAdapter.Message);
                    testResults.Add(resultAdapter.StackTrace);
                }
                else if (testStatus == TestStatus.Skipped)
                {
                    //Logging if test case fails
                    if (Initialize.DisplayExecutionTimeInLogger)
                    {
                        watch.Stop();
                        timeElapsed = watch.ElapsedMilliseconds.ToString();
                    }

                    //Logging testCase details after fails
                    ContextLogger.LogAfterTestCaseSkipped(moduleName, className, testCaseName, timeElapsed, testStatus.ToString());
                    endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                    EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);
                    testResult = TestResult.Skipped;
                    testResults.Add(resultAdapter.Message);
                }
                else if (testStatus == TestStatus.Inconclusive)
                {
                    if (Initialize.DisplayExecutionTimeInLogger)
                    {
                        watch.Stop();
                        timeElapsed = watch.ElapsedMilliseconds.ToString();
                    }

                    //Logging testCase details after fails
                    ContextLogger.LogAfterTestCaseInConclusive(moduleName, className, testCaseName, timeElapsed, testStatus.ToString());
                    endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                    EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);
                    testResult = TestResult.Inconclusive;
                }
            }
            catch (IgnoreException ex)
            {
                //Logging testCase details after fails
                ContextLogger.LogAfterTestCaseSkipped(moduleName, className, testCaseName, timeElapsed, "Skipped" + ex.Message);
                endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);
                testResult = TestResult.Skipped;
            }
            catch (Exception ex)
            {
                ContextLogger.LogIfException(moduleName, className, testCaseName, ex.Message);
                endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);
                testResult = TestResult.Failed;
                if (testResults.Count == 0)
                {
                    testResults.Add("Test is executed with:" + testResult.ToString() + "status");
                }

                testResultService.InsertTestResults(testCaseName, testResult, testResults.ToArray());
            }
            finally
            {
                if (testResults.Count == 0)
                {
                    testResults.Add("Test is executed with:" + testResult.ToString() + "status");
                }

                testResultService.InsertTestResults(testCaseName, testResult, testResults.ToArray());
            }
        }
        /// <summary>
        /// Execute test cases
        /// </summary>
        /// <param name="className"></param>
        /// <param name="testCaseName"></param>
        /// <param name="testDataOrder"></param>
        public void ExecuteTestCase(string className, string testCaseName, int testDataOrder)
        {
            object[] endTestCaseParameterArray;

            //Get ModulName
            ObjectId moduleId = mongoRepository.GetTestClassRepository.GetByName(className).Module_id;

            moduleName = mongoRepository.GetTestModuleRepository.GetById(moduleId).ModuleType;

            Type                 myClass                = Assembly.GetCallingAssembly().GetType(className);
            MethodInfo           ExecuteMethod          = myClass.GetMethod(testCaseName);
            MethodInfo           EndOfTestCaseExecution = myClass.GetMethod("EndOfTestCaseExecution");
            TestBaseClassService myObj = (TestBaseClassService)Activator.CreateInstance(myClass);

            try
            {
                /* Initialize the className and testCaseName to static variable
                 * for access out side of the method */
                TestCaseService.className    = className;
                TestCaseService.testCaseName = testCaseName;

                // Get Global TestData for the project
                testDataService.FillGlobalTestData(mongoRepository.GetTestProjectRepository.GetId(Initialize.ProjectName));
                // Get test data for the current testCase
                testDataService.FillTestData(mongoRepository.GetTestCaseRepository.GetId(testCaseName), testDataOrder);

                //Is dependent testcase is executed and if is null ? true : false
                bool isDependentTestCaseSuccess = true;

                if (Initialize.IsDependencyCheckEnabled)
                {
                    isDependentTestCaseSuccess = IsDependentTestCaseExecuted(testCaseName);
                }

                if (isDependentTestCaseSuccess)
                {
                    //Logging before testCase Execute
                    ContextLogger.LogBeforeTestCase(moduleName, className, testCaseName, testDataService.TestData);

                    //Execution Timein Logger Condition
                    if (Initialize.DisplayExecutionTimeInLogger)
                    {
                        watch = Stopwatch.StartNew();
                    }

                    ExecuteMethod.Invoke(myObj, null);
                    TestStatus result = TestExecutionContext.CurrentContext.CurrentResult.ResultState.Status;
                }
                else
                {
                    Assert.Ignore("TestCase is skipped:" + testCaseName + ",due to parent testcase is failed/not executed");
                }
            }
            catch (TargetInvocationException exception) when(exception.InnerException is Exception)
            {
                ContextLogger.LogIfException
                    (moduleName, className, testCaseName, exception.Message);
                if (Initialize.RetryOnException)
                {
                    IList <Type> attributes  = GetAvailableAttributes(ExecuteMethod);
                    bool         isAvailable = attributes.Any(attribute => attribute.Name.Equals("RetryByAttribute"));
                    if (isAvailable)
                    {
                        if (Initialize.DisplayExecutionTimeInLogger)
                        {
                            watch.Stop();
                            timeElapsed = watch.ElapsedMilliseconds.ToString();
                            ContextLogger.LogAfterTestCaseFails
                                (moduleName, className, testCaseName, testDataService.TestData, timeElapsed, "Failed On Exception");
                        }

                        //Execute end of testcase execution if any object needs to be disposed
                        endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                        EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);

                        //Retry attribute object
                        RetryByAttribute retryAttribute = ExecuteMethod.GetRetryByAttribute();
                        lock (lockReTestExecutionObj)
                        {
                            //Re-Execution of Exception TestCase
                            ReExecutionOfTestCase
                                (moduleName, className, testCaseName, testDataOrder, retryAttribute._tryCount, myClass, myObj);
                        }
                    }
                    else
                    {
                        throw exception;
                    }
                }
                else
                {
                    throw exception;
                }
            }
            catch (TargetInvocationException exception) when(exception.InnerException is AssertionException)
            {
                AssertionException assertException = exception.InnerException as AssertionException;

                ContextLogger.LogIfException(moduleName, className, testCaseName, exception.Message);
                if (Initialize.RetryOnException)
                {
                    IList <Type> attributes  = GetAvailableAttributes(ExecuteMethod);
                    bool         isAvailable = attributes.Any(attribute => attribute.Name.Equals("RetryByAttribute"));
                    if (isAvailable)
                    {
                        if (Initialize.DisplayExecutionTimeInLogger)
                        {
                            watch.Stop();
                            timeElapsed = watch.ElapsedMilliseconds.ToString();
                            ContextLogger.LogAfterTestCaseFails
                                (moduleName, className, testCaseName, testDataService.TestData, timeElapsed, assertException.ResultState.Status);
                        }
                        //Execute end of testcase execution if any object needs to be disposed
                        endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                        EndOfTestCaseExecution.Invoke(myObj, endTestCaseParameterArray);

                        //Retry attribute object
                        RetryByAttribute retryAttribute = ExecuteMethod.GetRetryByAttribute();
                        lock (lockReTestExecutionObj)
                        {
                            //Re-Execution of Exception TestCase
                            ReExecutionOfTestCase
                                (moduleName, className, testCaseName, testDataOrder, retryAttribute._tryCount, myClass, myObj);
                        }
                    }
                    else
                    {
                        throw exception;
                    }
                }
                else
                {
                    throw exception;
                }
            }
        }
Esempio n. 3
0
        private void ReExecutionOfTestCase
            (string moduleName, string className, string testCaseName,
            int testDataOrder, int retryCount, Type myClass, TestBaseClassService testBaseClassServiceObj)
        {
            object[]   endTestCaseParameterArray;
            TestResult testResult    = TestResult.Inconclusive;
            Exception  exceptionType = null;

            int        trackRetryCount        = 0;
            MethodInfo ExecuteMethod          = myClass.GetMethod(testCaseName);
            MethodInfo EndOfTestCaseExecution = myClass.GetMethod("EndOfTestCaseExecution");

            try
            {
                //Intial value of i is 1
                for (int i = 1; i <= retryCount; i++)
                {
                    try
                    {
                        trackRetryCount = i;
                        // Get Global TestData for the project
                        testDataService.FillGlobalTestData(mongoRepository.GetTestProjectRepository.GetId(Initialize.ProjectName));
                        // Get test data for the current testCase
                        testDataService.FillTestData(mongoRepository.GetTestCaseRepository.GetId(testCaseName), testDataOrder);

                        //Logging before Re-testCase Execute
                        ContextLogger.LogRetryStart(moduleName, className, testCaseName, testDataService.TestData);

                        //STOPWATCH FOR CALCULATING EXECUTION TIME OF TESTCASE.
                        dynamic clock = null;

                        //Execution Timein Logger Condition
                        if (Initialize.DisplayExecutionTimeInLogger)
                        {
                            clock = Stopwatch.StartNew();
                        }

                        ExecuteMethod.Invoke(testBaseClassServiceObj, null);
                        testResult = TestResult.Passed;
                    }
                    catch (Exception exception)
                    {
                        exceptionType = exception;
                        ContextLogger.LogIfException(moduleName, className, testCaseName, exception.Message);

                        //Logging if test case fails
                        if (Initialize.DisplayExecutionTimeInLogger)
                        {
                            watch.Stop();
                            timeElapsed = watch.ElapsedMilliseconds.ToString();
                        }

                        //Logging testCase details after fails
                        ContextLogger.LogAfterRetryTestCaseFails(moduleName, className, testCaseName, testDataService.TestData, timeElapsed, "Failed On Exception");
                        if (trackRetryCount != retryCount)
                        {
                            endTestCaseParameterArray = new object[] { moduleName, className, testCaseName, false };
                            EndOfTestCaseExecution.Invoke(testBaseClassServiceObj, endTestCaseParameterArray);
                        }
                        testResult = TestResult.Failed;
                    }
                }
            }
            catch (Exception exception)
            {
                ContextLogger.LogIfException(moduleName, className, testCaseName, exception.Message);
                throw exception;
            }
            finally
            {
                if (testResult.Equals(TestResult.Failed))
                {
                    throw exceptionType;
                }
            }
        }