Пример #1
0
        internal async Task Run()
        {
            if (Task == null && Action == null)
            {
                ConsoleReporter.WriteLine($"{nameof(Task)} and {nameof(Action)} are both null in {nameof(Runnable)}");
                return;
            }

            if (Task != null)
            {
                await Task.Compile().Invoke();

                return;
            }

            var compiledAction = Action.Compile();

            if (IsThisAsync(compiledAction))
            {
                await System.Threading.Tasks.Task.Run(compiledAction);

                return;
            }

            compiledAction.Invoke();
        }
Пример #2
0
        private void WriteStoryAndScenario(Scenario scenario)
        {
            ConsoleReporter.WriteStory(scenario.StoryText);

            ConsoleReporter.WriteScenario(scenario.ScenarioText);

            ConsoleReporter.WriteLine(Environment.NewLine);
        }
Пример #3
0
        private void WriteCustomTestInformation(Scenario scenario)
        {
            foreach (var testInformationAttribute in scenario.CustomTestInformation ?? Array.Empty <TestInformationAttribute>())
            {
                ConsoleReporter.WriteLine(testInformationAttribute.Print());
            }

            ConsoleReporter.WriteLine(Environment.NewLine);
        }
Пример #4
0
        private static string GetExpressionValue(Expression argument)
        {
            try
            {
                var argumentValue = argument is ConstantExpression constantExpression
                    ? constantExpression.Value?.ToString()
                    : Expression.Lambda(Expression.Convert(argument, argument.Type)).Compile().DynamicInvoke();

                if (argumentValue == null)
                {
                    return("null");
                }

                var stepTextStringConverter = BDTestSettings.CustomStringConverters
                                              .FirstOrDefault(type =>
                                                              type.GetType().GetInterfaces().FirstOrDefault()?.Name.StartsWith(nameof(IStepTextStringConverter <object>)) == true &&
                                                              type.GetType().GetInterfaces().FirstOrDefault()?.GetGenericArguments().FirstOrDefault() == argumentValue.GetType());

                if (stepTextStringConverter != null)
                {
                    var method = stepTextStringConverter.GetType().GetMethod(nameof(IStepTextStringConverter <object> .ConvertToString));
                    if (method != null)
                    {
                        return(method.Invoke(stepTextStringConverter, new[] { argumentValue }) as string);
                    }
                }

                if (TypeHelper.IsFuncOrAction(argumentValue.GetType()))
                {
                    var func = (Delegate)argumentValue;

                    return(func.DynamicInvoke()?.ToString());
                }

                if (TypeHelper.IsIEnumerable(argumentValue) || argumentValue.GetType().IsArray)
                {
                    return(string.Join(", ", (IEnumerable <object>)argumentValue));
                }

                if (TypeHelper.IsIDictionary(argumentValue))
                {
                    return(string.Join(",",
                                       ((IDictionary <object, object>)argumentValue).Select(kv => $"{kv.Key}={kv.Value}")
                                       ));
                }

                return(argumentValue.ToString());
            }
            catch (Exception e)
            {
                ConsoleReporter.WriteLine($"BDTest Exception:{Environment.NewLine}Class: {nameof(StepTextHelper)}{Environment.NewLine}Method: {nameof(GetExpressionValue)}{Environment.NewLine}Exception: {e.Message}{Environment.NewLine}{e.StackTrace}");
                return("null");
            }
        }
Пример #5
0
        private async Task ResetData(Scenario scenario)
        {
            SetRetryData(scenario);

            ResetStepData(scenario);

            scenario.Status = Status.Inconclusive;

            await RunRetryTestHooks(scenario).ConfigureAwait(false);

            ConsoleReporter.WriteLine($"{Environment.NewLine}Retrying test...{Environment.NewLine}");
        }
Пример #6
0
        public async Task ExecuteAsync(Scenario scenario)
        {
            await _scenarioRetryManager.CheckIfAlreadyExecuted(scenario).ConfigureAwait(false);

            await Task.Run(async() =>
            {
                try
                {
                    scenario.StartTime = DateTime.Now;

                    TestOutputData.ClearCurrentTaskData();

                    if (scenario.RetryCount == 0)
                    {
                        WriteTestInformation(scenario);
                    }

                    if (ShouldSkip(scenario))
                    {
                        scenario.Status = Status.Skipped;
                        scenario.Steps.ForEach(step => step.Status = Status.Skipped);
                        return;
                    }

                    foreach (var step in scenario.Steps)
                    {
                        await step.Execute();
                    }

                    scenario.Status = Status.Passed;
                }
                catch (NotImplementedException)
                {
                    scenario.Status = Status.NotImplemented;
                    throw;
                }
                catch (Exception e) when(BDTestSettings.CustomExceptionSettings.SuccessExceptionTypes.Contains(e.GetType()))
                {
                    scenario.Status = Status.Passed;
                    throw;
                }
                catch (Exception e) when(BDTestSettings.CustomExceptionSettings.InconclusiveExceptionTypes.Contains(e.GetType()))
                {
                    scenario.Status = Status.Inconclusive;
                    throw;
                }
                catch (Exception e)
                {
                    var validRetryRules = BDTestSettings.GlobalRetryTestRules.Rules.Where(rule => rule.Condition(e)).ToList();
                    if (validRetryRules.Any() && scenario.RetryCount < validRetryRules.Max(x => x.RetryLimit))
                    {
                        scenario.ShouldRetry = true;
                        return;
                    }

                    var bdTestRetryAttribute = RetryAttributeHelper.GetBDTestRetryAttribute(scenario);
                    if (bdTestRetryAttribute != null && scenario.RetryCount < bdTestRetryAttribute.Count)
                    {
                        scenario.ShouldRetry = true;
                        return;
                    }

                    scenario.Status = Status.Failed;

                    ConsoleReporter.WriteLine($"{Environment.NewLine}Exception: {e.Message}{Environment.NewLine}{e.StackTrace}{Environment.NewLine}");

                    throw;
                }
                finally
                {
                    if (scenario.ShouldRetry)
                    {
                        await ExecuteAsync(scenario).ConfigureAwait(false);
                    }
                    else
                    {
                        foreach (var notRunStep in scenario.Steps.Where(step => step.Status == Status.Inconclusive))
                        {
                            notRunStep.SetStepText();
                        }

                        ConsoleReporter.WriteLine($"{Environment.NewLine}Test Summary:{Environment.NewLine}");

                        scenario.Steps.ForEach(step => ConsoleReporter.WriteLine($"{step.StepText} > [{step.Status}]"));

                        ConsoleReporter.WriteLine($"{Environment.NewLine}Test Result: {scenario.Status}{Environment.NewLine}");

                        scenario.EndTime   = DateTime.Now;
                        scenario.TimeTaken = scenario.EndTime - scenario.StartTime;

                        scenario.Output = string.Join(Environment.NewLine,
                                                      scenario.Steps.Where(step => !string.IsNullOrWhiteSpace(step.Output)).Select(step => step.Output));
                    }
                }
            });
        }