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