Beispiel #1
0
        /// <summary>
        /// Converts the parameter outcome to UTF outcome
        /// </summary>
        /// <param name="outcome">The UTF outcome.</param>
        /// <returns>test outcome</returns>
        private static UT.UnitTestOutcome ToUTF(UT.UnitTestOutcome outcome)
        {
            switch (outcome)
            {
            case UT.UnitTestOutcome.Error:
                return(UT.UnitTestOutcome.Error);

            case UT.UnitTestOutcome.Failed:
                return(UT.UnitTestOutcome.Failed);

            case UT.UnitTestOutcome.Inconclusive:
                return(UT.UnitTestOutcome.Inconclusive);

            case UT.UnitTestOutcome.Passed:
                return(UT.UnitTestOutcome.Passed);

            case UT.UnitTestOutcome.Timeout:
                return(UT.UnitTestOutcome.Timeout);

            case UT.UnitTestOutcome.InProgress:
                return(UT.UnitTestOutcome.InProgress);

            default:
                return(UT.UnitTestOutcome.Unknown);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Converts the test framework's UnitTestOutcome object to adapter's UnitTestOutcome object.
        /// </summary>
        /// <param name="frameworkTestOutcome">The test framework's UnitTestOutcome object.</param>
        /// <returns>The adapter's UnitTestOutcome object.</returns>
        public static UnitTestOutcome ToUnitTestOutcome(this UTF.UnitTestOutcome frameworkTestOutcome)
        {
            UnitTestOutcome outcome = UnitTestOutcome.Passed;

            switch (frameworkTestOutcome)
            {
            case UTF.UnitTestOutcome.Failed:
                outcome = UnitTestOutcome.Failed;
                break;

            case UTF.UnitTestOutcome.Inconclusive:
                outcome = UnitTestOutcome.Inconclusive;
                break;

            case UTF.UnitTestOutcome.InProgress:
                outcome = UnitTestOutcome.InProgress;
                break;

            case UTF.UnitTestOutcome.Passed:
                outcome = UnitTestOutcome.Passed;
                break;

            case UTF.UnitTestOutcome.Timeout:
                outcome = UnitTestOutcome.Timeout;
                break;

            case UTF.UnitTestOutcome.Unknown:
            default:
                outcome = UnitTestOutcome.Error;
                break;
            }

            return(outcome);
        }
Beispiel #3
0
        /// <summary>
        /// Returns more important outcome of two.
        /// </summary>
        /// <param name="outcome1"> First outcome that needs to be compared. </param>
        /// <param name="outcome2"> Second outcome that needs to be compared. </param>
        /// <returns> Outcome which has higher importance.</returns>
        internal static UTF.UnitTestOutcome GetMoreImportantOutcome(this UTF.UnitTestOutcome outcome1, UTF.UnitTestOutcome outcome2)
        {
            var unitTestOutcome1 = outcome1.ToUnitTestOutcome();
            var unitTestOutcome2 = outcome2.ToUnitTestOutcome();

            return(unitTestOutcome1 < unitTestOutcome2 ? outcome1 : outcome2);
        }
        /// <summary>
        /// Converts the parameter outcome to UTF outcome
        /// </summary>
        /// <param name="outcome">The UTF outcome.</param>
        /// <returns>test outcome</returns>
        private static UTF.UnitTestOutcome ToUTF(UTF.UnitTestOutcome outcome)
        {
            switch (outcome)
            {
            case UTF.UnitTestOutcome.Error:
            case UTF.UnitTestOutcome.Failed:
            case UTF.UnitTestOutcome.Inconclusive:
            case UTF.UnitTestOutcome.Passed:
            case UTF.UnitTestOutcome.Timeout:
            case UTF.UnitTestOutcome.InProgress:
                return(outcome);

            default:
                Debug.Fail("Unknown outcome " + outcome);
                return(UTF.UnitTestOutcome.Unknown);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestContextEx"/> class.
        /// </summary>
        /// <param name="testName">Gets the name of the test method currently being executed.</param>
        /// <param name="fullyQualifiedTestClassName">Gets the Fully-qualified name of the class containing the test method currently
        ///     being executed.
        /// </param>
        /// <param name="stringWriter">The writer where diagnostic messages are written to.</param>
        /// <param name="properties">Gets test properties for a test.</param>
        public TestContextEx(string testName, string fullyQualifiedTestClassName, StringWriter stringWriter, IDictionary properties)
        {
            if (String.IsNullOrEmpty(testName.Trim()))
            {
                throw new ArgumentNullException("testName", "cannot be null or empty.");
            }

            if (String.IsNullOrEmpty(fullyQualifiedTestClassName.Trim()))
            {
                throw new ArgumentNullException("fullyQualifiedTestClassName", "cannot be null or empty.");
            }

            _testName = testName;
            _fullyQualifiedTestClassName = fullyQualifiedTestClassName;
            _stringWriter = stringWriter;
            _properties   = new Dictionary <string, object>((IDictionary <string, object>)properties);
            _outcome      = UT.UnitTestOutcome.InProgress;
            InitializeProperties();
        }
Beispiel #6
0
        /// <summary>
        /// Checks whether exception is an Assert exception
        /// </summary>
        /// <param name="exception">An <see cref="Exception"/> instance.</param>
        /// <param name="outcome"> Framework's Outcome depending on type of assertion.</param>
        /// <param name="exceptionMessage">Exception message.</param>
        /// <param name="exceptionStackTrace">StackTraceInformation for the exception</param>
        /// <returns>True, if Assert exception. False, otherwise.</returns>
        internal static bool TryGetUnitTestAssertException(this Exception exception, out UTF.UnitTestOutcome outcome, out string exceptionMessage, out StackTraceInformation exceptionStackTrace)
        {
            if (exception is UTF.UnitTestAssertException)
            {
                outcome = exception is UTF.AssertInconclusiveException ?
                          UTF.UnitTestOutcome.Inconclusive : UTF.UnitTestOutcome.Failed;

                exceptionMessage    = exception.TryGetMessage();
                exceptionStackTrace = exception.TryGetStackTraceInformation();
                return(true);
            }
            else
            {
                outcome             = UTF.UnitTestOutcome.Failed;
                exceptionMessage    = null;
                exceptionStackTrace = null;
                return(false);
            }
        }
Beispiel #7
0
        internal UnitTestResult[] RunTestMethod()
        {
            Debug.Assert(this.test != null, "Test should not be null.");
            Debug.Assert(this.testMethodInfo.TestMethod != null, "Test method should not be null.");

            List <UTF.TestResult> results = new List <UTF.TestResult>();

            if (this.testMethodInfo.TestMethodOptions.Executor != null)
            {
                UTF.DataSourceAttribute[] dataSourceAttribute = this.testMethodInfo.GetAttributes <UTF.DataSourceAttribute>(false);
                if (dataSourceAttribute != null && dataSourceAttribute.Length == 1)
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Start();

                    try
                    {
                        IEnumerable <object> dataRows = PlatformServiceProvider.Instance.TestDataSource.GetData(this.testMethodInfo, this.testContext);

                        if (dataRows == null)
                        {
                            watch.Stop();
                            var inconclusiveResult = new UTF.TestResult();
                            inconclusiveResult.Outcome  = UTF.UnitTestOutcome.Inconclusive;
                            inconclusiveResult.Duration = watch.Elapsed;
                            results.Add(inconclusiveResult);
                        }
                        else
                        {
                            try
                            {
                                int rowIndex = 0;
                                foreach (object dataRow in dataRows)
                                {
                                    watch.Reset();
                                    watch.Start();

                                    this.testContext.SetDataRow(dataRow);
                                    UTF.TestResult[] testResults;

                                    try
                                    {
                                        testResults = this.testMethodInfo.TestMethodOptions.Executor.Execute(this.testMethodInfo);
                                    }
                                    catch (Exception ex)
                                    {
                                        testResults = new[]
                                        {
                                            new UTF.TestResult()
                                            {
                                                TestFailureException = new Exception(string.Format(CultureInfo.CurrentCulture, Resource.UTA_ExecuteThrewException, ex.Message), ex)
                                            }
                                        };
                                    }

                                    watch.Stop();
                                    foreach (var testResult in testResults)
                                    {
                                        testResult.DatarowIndex = rowIndex;
                                        testResult.Duration     = watch.Elapsed;
                                    }

                                    rowIndex++;

                                    results.AddRange(testResults);
                                }
                            }
                            finally
                            {
                                this.testContext.SetDataConnection(null);
                                this.testContext.SetDataRow(null);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        watch.Stop();
                        var failedResult = new UTF.TestResult();
                        failedResult.Outcome = UTF.UnitTestOutcome.Error;
                        failedResult.TestFailureException = ex;
                        failedResult.Duration             = watch.Elapsed;
                        results.Add(failedResult);
                    }
                }
                else
                {
                    UTF.ITestDataSource[] testDataSources = this.testMethodInfo.GetAttributes <Attribute>(true)?.Where(a => a is UTF.ITestDataSource).OfType <UTF.ITestDataSource>().ToArray();

                    if (testDataSources != null && testDataSources.Length > 0)
                    {
                        foreach (var testDataSource in testDataSources)
                        {
                            foreach (var data in testDataSource.GetData(this.testMethodInfo.MethodInfo))
                            {
                                this.testMethodInfo.SetArguments(data);
                                UTF.TestResult[] testResults;
                                try
                                {
                                    testResults = this.testMethodInfo.TestMethodOptions.Executor.Execute(this.testMethodInfo);
                                }
                                catch (Exception ex)
                                {
                                    testResults = new[]
                                    {
                                        new UTF.TestResult()
                                        {
                                            TestFailureException = new Exception(string.Format(CultureInfo.CurrentCulture, Resource.UTA_ExecuteThrewException, ex.Message), ex)
                                        }
                                    };
                                }

                                foreach (var testResult in testResults)
                                {
                                    testResult.DisplayName = testDataSource.GetDisplayName(this.testMethodInfo.MethodInfo, data);
                                }

                                results.AddRange(testResults);
                                this.testMethodInfo.SetArguments(null);
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            results.AddRange(this.testMethodInfo.TestMethodOptions.Executor.Execute(this.testMethodInfo));
                        }
                        catch (Exception ex)
                        {
                            results.Add(new UTF.TestResult()
                            {
                                TestFailureException = new Exception(string.Format(CultureInfo.CurrentCulture, Resource.UTA_ExecuteThrewException, ex.Message), ex)
                            });
                        }
                    }
                }
            }
            else
            {
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogError(
                    "Not able to get executor for method {0}.{1}",
                    this.testMethodInfo.TestClassName,
                    this.testMethodInfo.TestMethodName);
            }

            if (results != null && results.Count > 0)
            {
                // aggregate for data driven tests
                UTF.UnitTestOutcome aggregateOutcome = UTF.UnitTestOutcome.Passed;

                foreach (var result in results)
                {
                    if (result.Outcome != UTF.UnitTestOutcome.Passed)
                    {
                        if (aggregateOutcome != UTF.UnitTestOutcome.Failed)
                        {
                            if (result.Outcome == UTF.UnitTestOutcome.Failed ||
                                aggregateOutcome != UTF.UnitTestOutcome.Timeout)
                            {
                                aggregateOutcome = result.Outcome;
                            }
                        }
                    }
                }

                this.testContext.SetOutcome(aggregateOutcome);
            }
            else
            {
                this.testContext.SetOutcome(UTF.UnitTestOutcome.Unknown);
                results.Add(new UTF.TestResult()
                {
                    Outcome = UTF.UnitTestOutcome.Unknown, TestFailureException = new TestFailedException(UnitTestOutcome.Error, Resource.UTA_NoTestResult)
                });
            }

            return(results.ToArray().ToUnitTestResults());
        }
 /// <summary>
 /// Set the unit-test outcome
 /// </summary>
 /// <param name="outcome">The test outcome.</param>
 public void SetOutcome(UTF.UnitTestOutcome outcome)
 {
     this.outcome = outcome;
 }
Beispiel #9
0
 /// <summary>
 /// Set the unit-test outcome
 /// </summary>
 /// <param name="outcome">The test outcome.</param>
 public void SetOutcome(UTF.UnitTestOutcome outcome)
 {
     this.outcome = ToUTF(outcome);
 }
Beispiel #10
0
        internal UnitTestResult[] RunTestMethod()
        {
            Debug.Assert(this.test != null, "Test should not be null.");
            Debug.Assert(this.testMethodInfo.TestMethod != null, "Test method should not be null.");

            UTF.TestResult[] results = null;

            if (this.testMethodInfo.TestMethodOptions.Executor != null)
            {
                try
                {
                    bool isDataDriven = PlatformServiceProvider.Instance.TestDataSource.HasDataDrivenTests(this.testMethodInfo);
                    if (isDataDriven)
                    {
                        results = PlatformServiceProvider.Instance.TestDataSource.RunDataDrivenTest(this.testContext.Context, this.testMethodInfo, this.test, this.testMethodInfo.TestMethodOptions.Executor);
                    }
                    else
                    {
                        results = this.testMethodInfo.TestMethodOptions.Executor.Execute(this.testMethodInfo);
                    }
                }
                catch (Exception ex)
                {
                    results = new[] { new UTF.TestResult()
                                      {
                                          TestFailureException = new Exception(string.Format(CultureInfo.CurrentCulture, Resource.UTA_ExecuteThrewException, ex.Message), ex)
                                      } };
                }
            }
            else
            {
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogError(
                    "Not able to get executor for method {0}.{1}",
                    this.testMethodInfo.TestClassName,
                    this.testMethodInfo.TestMethodName);
            }

            if (results != null && results.Length > 0)
            {
                // aggregate for data driven tests
                UTF.UnitTestOutcome aggregateOutcome = UTF.UnitTestOutcome.Passed;

                foreach (var result in results)
                {
                    if (result.Outcome != UTF.UnitTestOutcome.Passed)
                    {
                        if (aggregateOutcome != UTF.UnitTestOutcome.Failed)
                        {
                            if (result.Outcome == UTF.UnitTestOutcome.Failed ||
                                aggregateOutcome != UTF.UnitTestOutcome.Timeout)
                            {
                                aggregateOutcome = result.Outcome;
                            }
                        }
                    }
                }

                this.testContext.SetOutcome(aggregateOutcome);
            }
            else
            {
                this.testContext.SetOutcome(UTF.UnitTestOutcome.Unknown);
                results = new[] { new UTF.TestResult()
                                  {
                                      Outcome = UTF.UnitTestOutcome.Unknown, TestFailureException = new TestFailedException(UnitTestOutcome.Error, Resource.UTA_NoTestResult)
                                  } };
            }

            return(results.ToUnitTestResults());
        }
Beispiel #11
0
 /// <summary>
 /// Returns more important outcome of two.
 /// </summary>
 /// <param name="outcome1"> First outcome that needs to be compared. </param>
 /// <param name="outcome2"> Second outcome that needs to be compared. </param>
 /// <returns> Outcome which has higher importance.</returns>
 internal static UTF.UnitTestOutcome GetMoreImportantOutcome(this UTF.UnitTestOutcome outcome1, UTF.UnitTestOutcome outcome2)
 {
     return(outcome1 < outcome2 ? outcome1 : outcome2);
 }
Beispiel #12
0
 /// <summary>
 /// Set the unit-test outcome
 /// </summary>
 /// <param name="outcome">The test outcome.</param>
 public void SetOutcome(UT.UnitTestOutcome outcome)
 {
     _outcome = ToUTF(outcome);
 }
 /// <summary>
 /// Set the unit-test outcome
 /// </summary>
 internal void SetOutcome(UTF.UnitTestOutcome outcome)
 {
     this.outcome = outcome;
 }