/// <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; } } }