private static void FailScenario(Exception ex)
 {
     AllureLifecycle.Instance.UpdateTestCase(
         x =>
     {
         x.status        = x.status != Status.none ? x.status : Status.failed;
         x.statusDetails = PluginHelper.GetStatusDetails(ex);
     });
 }
Exemplo n.º 2
0
        public override object InvokeBinding(IBinding binding, IContextManager contextManager, object[] arguments,
                                             ITestTracer testTracer, out TimeSpan duration)
        {
            // process hook
            if (binding is HookBinding hook)
            {
                var featureContainerId = PluginHelper.GetFeatureContainerId(contextManager.FeatureContext?.FeatureInfo);

                switch (hook.HookType)
                {
                case HookType.BeforeFeature:
                    if (hook.HookOrder == int.MinValue)
                    {
                        // starting point
                        var featureContainer = new TestResultContainer
                        {
                            uuid = PluginHelper.GetFeatureContainerId(contextManager.FeatureContext?.FeatureInfo)
                        };
                        AllureLifecycle.Instance.StartTestContainer(featureContainer);

                        contextManager.FeatureContext.Set(new HashSet <TestResultContainer>());
                        contextManager.FeatureContext.Set(new HashSet <TestResult>());

                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    else
                    {
                        try
                        {
                            StartFixture(hook, featureContainerId);
                            var result = base.InvokeBinding(binding, contextManager, arguments, testTracer,
                                                            out duration);
                            AllureLifecycle.Instance.StopFixture(x => x.status = Status.passed);
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            AllureLifecycle.Instance.StopFixture(x => x.status = Status.broken);

                            // if BeforeFeature is failed execution is stopped. We need to create, update, stop and write everything here.

                            // create fake scenario container
                            var scenarioContainer =
                                PluginHelper.StartTestContainer(contextManager.FeatureContext, null);

                            // start fake scenario
                            var scenario = PluginHelper.StartTestCase(scenarioContainer.uuid,
                                                                      contextManager.FeatureContext, null);

                            // update, stop and write
                            AllureLifecycle.Instance
                            .StopTestCase(x =>
                            {
                                x.status        = Status.broken;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            })
                            .WriteTestCase(scenario.uuid)
                            .StopTestContainer(scenarioContainer.uuid, true)
                            .WriteTestContainer(scenarioContainer.uuid)
                            .StopTestContainer(featureContainerId, true)
                            .WriteTestContainer(featureContainerId);

                            throw;
                        }
                    }

                case HookType.BeforeStep:
                case HookType.AfterStep:
                {
                    var scenario = PluginHelper.GetCurrentTestCase(contextManager.ScenarioContext);

                    try
                    {
                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    catch (Exception ex)
                    {
                        AllureLifecycle.Instance
                        .UpdateTestCase(scenario.uuid,
                                        x =>
                            {
                                x.status        = Status.broken;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            });
                        throw;
                    }
                }

                case HookType.BeforeScenario:
                case HookType.AfterScenario:
                    if (hook.HookOrder == int.MinValue || hook.HookOrder == int.MaxValue)
                    {
                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    else
                    {
                        var scenarioContainer = PluginHelper.GetCurrentTestConainer(contextManager.ScenarioContext);

                        try
                        {
                            StartFixture(hook, scenarioContainer.uuid);
                            var result = base.InvokeBinding(binding, contextManager, arguments, testTracer,
                                                            out duration);
                            AllureLifecycle.Instance.StopFixture(x => x.status = Status.passed);
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            var status = ex.GetType().Name.Contains(PluginHelper.IgnoreException)
                                    ? Status.skipped
                                    : Status.broken;

                            AllureLifecycle.Instance.StopFixture(x => x.status = status);

                            // get or add new scenario
                            var scenario = PluginHelper.GetCurrentTestCase(contextManager.ScenarioContext) ??
                                           PluginHelper.StartTestCase(scenarioContainer.uuid,
                                                                      contextManager.FeatureContext, contextManager.ScenarioContext);

                            AllureLifecycle.Instance.UpdateTestCase(scenario.uuid,
                                                                    x =>
                            {
                                x.status        = status;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            });
                            throw;
                        }
                    }

                case HookType.AfterFeature:
                    if (hook.HookOrder == int.MaxValue)
                    // finish point
                    {
                        WriteScenarios(contextManager);
                        AllureLifecycle.Instance
                        .StopTestContainer(featureContainerId, true)
                        .WriteTestContainer(featureContainerId);

                        return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                    }
                    else
                    {
                        try
                        {
                            StartFixture(hook, featureContainerId);
                            var result = base.InvokeBinding(binding, contextManager, arguments, testTracer,
                                                            out duration);
                            AllureLifecycle.Instance.StopFixture(x => x.status = Status.passed);
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            var scenario = contextManager.FeatureContext.Get <HashSet <TestResult> >().Last();
                            AllureLifecycle.Instance
                            .StopFixture(x => x.status = Status.broken)
                            .UpdateTestCase(scenario.uuid,
                                            x =>
                            {
                                x.status        = Status.broken;
                                x.statusDetails = PluginHelper.GetStatusDetails(ex);
                            });

                            WriteScenarios(contextManager);

                            AllureLifecycle.Instance
                            .StopTestContainer(featureContainerId, true)
                            .WriteTestContainer(featureContainerId);

                            throw;
                        }
                    }

                case HookType.BeforeScenarioBlock:
                case HookType.AfterScenarioBlock:
                case HookType.BeforeTestRun:
                case HookType.AfterTestRun:
                default:
                    return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
                }
            }

            return(base.InvokeBinding(binding, contextManager, arguments, testTracer, out duration));
        }