예제 #1
0
파일: TestRun.cs 프로젝트: fbondia/JazSharp
        private async Task <TestResultInfo> TestExecutionAsync(Test test, bool onlyFocused)
        {
            if (_cancellationTokenSource.Token.IsCancellationRequested)
            {
                return(null);
            }

            Stopwatch     stopwatch = new Stopwatch();
            StringBuilder output    = new StringBuilder();
            Exception     exception = null;
            TestResult    testResult;

            await _testSynchroniser.WaitAsync();

            if (test.IsExcluded)
            {
                testResult = TestResult.Skipped;
                output.Append("Test skipped due to having been defined with xDescribe or xIt.");
            }
            else if (onlyFocused && !test.IsFocused)
            {
                testResult = TestResult.Skipped;
                output.Append("Test skipped due to other tests being defined with fDescribe or fIt.");
            }
            else
            {
                try
                {
                    stopwatch.Start();

                    foreach (var method in test.Execution.GetDelegates())
                    {
                        switch (method)
                        {
                        case Action normal:
                            normal.Invoke();
                            break;

                        case Action <StringBuilder> normalWithOutput:
                            normalWithOutput.Invoke(output);
                            break;

                        case Func <Task> task:
                            await task.Invoke();

                            break;

                        case Func <StringBuilder, Task> taskWithOutput:
                            await taskWithOutput.Invoke(output);

                            break;
                        }
                    }

                    stopwatch.Stop();
                    output.Append(Environment.NewLine).Append("Test completed successfully.");
                    testResult = TestResult.Passed;
                }
                catch (Exception ex)
                {
                    var innermostException = ex;

                    while (innermostException.InnerException != null)
                    {
                        innermostException = innermostException.InnerException;
                    }

                    output.Append(Environment.NewLine).Append(innermostException.Message);
                    exception  = ex;
                    testResult = TestResult.Failed;
                }
            }

            _clearSpiesMethod.Invoke(null, new object[0]);
            _testSynchroniser.Release();

            var result = new TestResultInfo(test, testResult, output.ToString().Trim(), exception, stopwatch.Elapsed);

            try
            {
                TestCompleted?.Invoke(result);
            }
            catch
            {
            }

            return(result);
        }
예제 #2
0
        private async Task <TestResultInfo> TestExecutionAsync(Test test, bool onlyFocused)
        {
            if (_cancellationTokenSource.Token.IsCancellationRequested)
            {
                return(null);
            }

            Stopwatch     stopwatch = new Stopwatch();
            StringBuilder output    = new StringBuilder();
            Exception     exception = null;
            TestResult    testResult;

            await Jaz.CurrentTestSemaphore.WaitAsync();

            _setupTestExecutionContextMethod.Invoke(null, new object[] { test.FullName, output });

            if (test.IsExcluded)
            {
                testResult = TestResult.Skipped;
                output.Append("Test skipped due to having been defined with xDescribe or xIt.");
            }
            else if (onlyFocused && !test.IsFocused)
            {
                testResult = TestResult.Skipped;
                output.Append("Test skipped due to other tests being defined with fDescribe or fIt.");
            }
            else
            {
                try
                {
                    stopwatch.Start();

                    foreach (var method in test.Execution.GetDelegates())
                    {
                        if (method is Action action)
                        {
                            action.Invoke();
                        }
                        else
                        {
                            await((Func <Task>)method).Invoke();
                        }
                    }

                    stopwatch.Stop();
                    output.Append(Environment.NewLine).Append("Test completed successfully.");
                    testResult = TestResult.Passed;
                }
                catch (Exception ex)
                {
                    var innermostException = ex;

                    while (innermostException.InnerException != null)
                    {
                        innermostException = innermostException.InnerException;
                    }

                    output.Append(Environment.NewLine).Append(innermostException.Message);
                    exception  = ex;
                    testResult = TestResult.Failed;
                }
            }

            _clearTestExecutionContextMethod.Invoke(null, new object[0]);
            Jaz.CurrentTestSemaphore.Release();

            var result = new TestResultInfo(test, testResult, output.ToString().Trim(), exception, stopwatch.Elapsed);

            try
            {
                TestCompleted?.Invoke(result);
            }
            catch
            {
            }

            return(result);
        }