Skip to content

Commit fd62d0d

Browse files
committed
Config to stop execution on a failing then - fixes #34
1 parent 506d165 commit fd62d0d

File tree

6 files changed

+167
-3
lines changed

6 files changed

+167
-3
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
using TestStack.BDDfy.Configuration;
5+
using TestStack.BDDfy.Core;
6+
using TestStack.BDDfy.Scanners.StepScanners.ExecutableAttribute.GwtAttributes;
7+
using TestStack.BDDfy.Scanners.StepScanners.Fluent;
8+
9+
namespace TestStack.BDDfy.Tests.Configuration
10+
{
11+
public class TestRunnerTests
12+
{
13+
public class ScenarioWithFailingThen
14+
{
15+
[Given]
16+
public void PassingGiven()
17+
{
18+
}
19+
20+
[When]
21+
public void PassingWhen()
22+
{
23+
}
24+
25+
[Then]
26+
public void FailingThen()
27+
{
28+
throw new Exception();
29+
}
30+
31+
[AndThen]
32+
public void PassingAndThen()
33+
{
34+
}
35+
}
36+
37+
[TestFixture]
38+
public class When_StopExecutionOnFailingThen_IsSetToTrue
39+
{
40+
[Test]
41+
public void FailingThenStopsThePipelineWithReflectiveAPI()
42+
{
43+
Configurator.Processors.TestRunner.StopExecutionOnFailingThen = true;
44+
45+
try
46+
{
47+
var testRun = new ScenarioWithFailingThen().LazyBDDfy();
48+
Verify(testRun);
49+
}
50+
finally
51+
{
52+
Configurator.Processors.TestRunner.StopExecutionOnFailingThen = false;
53+
}
54+
}
55+
56+
[Test]
57+
public void FailingThenStopsThePipelineWithFluentAPI()
58+
{
59+
Configurator.Processors.TestRunner.StopExecutionOnFailingThen = true;
60+
61+
try
62+
{
63+
var testRun = new ScenarioWithFailingThen()
64+
.Given(x => x.PassingGiven())
65+
.When(x => x.PassingWhen())
66+
.Then(x => x.FailingThen())
67+
.And(x => x.PassingAndThen())
68+
.LazyBDDfy();
69+
70+
Verify(testRun);
71+
}
72+
finally
73+
{
74+
Configurator.Processors.TestRunner.StopExecutionOnFailingThen = false;
75+
}
76+
}
77+
78+
private static void Verify(Engine testRun)
79+
{
80+
Assert.Throws<Exception>(() => testRun.Run());
81+
var scenario = testRun.Story.Scenarios.First();
82+
Assert.AreEqual(StepExecutionResult.Failed, scenario.Result);
83+
var steps = scenario.Steps;
84+
85+
Assert.AreEqual(4, steps.Count);
86+
Assert.AreEqual(StepExecutionResult.Passed, steps[0].Result);
87+
Assert.AreEqual(ExecutionOrder.SetupState, steps[0].ExecutionOrder);
88+
Assert.AreEqual(StepExecutionResult.Passed, steps[1].Result);
89+
Assert.AreEqual(ExecutionOrder.Transition, steps[1].ExecutionOrder);
90+
Assert.AreEqual(StepExecutionResult.Failed, steps[2].Result);
91+
Assert.AreEqual(ExecutionOrder.Assertion, steps[2].ExecutionOrder);
92+
Assert.AreEqual(StepExecutionResult.NotExecuted, steps[3].Result);
93+
Assert.AreEqual(ExecutionOrder.ConsecutiveAssertion, steps[3].ExecutionOrder);
94+
}
95+
}
96+
97+
[TestFixture]
98+
public class When_StopExecutionOnFailingThen_IsLeftAsDefault
99+
{
100+
[Test]
101+
public void FailingThenDoesNotStopThePipelineWithReflectiveAPI()
102+
{
103+
var testRun = new ScenarioWithFailingThen().LazyBDDfy();
104+
Verify(testRun);
105+
}
106+
107+
[Test]
108+
public void FailingThenDoesNotStopThePipelineWithFluentAPI()
109+
{
110+
var testRun = new ScenarioWithFailingThen()
111+
.Given(x => x.PassingGiven())
112+
.When(x => x.PassingWhen())
113+
.Then(x => x.FailingThen())
114+
.And(x => x.PassingAndThen())
115+
.LazyBDDfy();
116+
117+
Verify(testRun);
118+
}
119+
120+
private static void Verify(Engine testRun)
121+
{
122+
Assert.Throws<Exception>(() => testRun.Run());
123+
var scenario = testRun.Story.Scenarios.First();
124+
Assert.AreEqual(StepExecutionResult.Failed, scenario.Result);
125+
var steps = scenario.Steps;
126+
127+
Assert.AreEqual(4, steps.Count);
128+
Assert.AreEqual(StepExecutionResult.Passed, steps[0].Result);
129+
Assert.AreEqual(ExecutionOrder.SetupState, steps[0].ExecutionOrder);
130+
Assert.AreEqual(StepExecutionResult.Passed, steps[1].Result);
131+
Assert.AreEqual(ExecutionOrder.Transition, steps[1].ExecutionOrder);
132+
Assert.AreEqual(StepExecutionResult.Failed, steps[2].Result);
133+
Assert.AreEqual(ExecutionOrder.Assertion, steps[2].ExecutionOrder);
134+
Assert.AreEqual(StepExecutionResult.Passed, steps[3].Result);
135+
Assert.AreEqual(ExecutionOrder.ConsecutiveAssertion, steps[3].ExecutionOrder);
136+
}
137+
}
138+
}
139+
}

TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="Arguments\ArgumentsProvidedForGiven.cs" />
6767
<Compile Include="Arguments\ArgumentsProvidedForThen.cs" />
6868
<Compile Include="Arguments\MultipleArgumentsProvidedForTheSameStep.cs" />
69+
<Compile Include="Configuration\TestRunnerTests.cs" />
6970
<Compile Include="Scanner\WhenStepScannerFactoryAsyncMethods.cs" />
7071
<Compile Include="Configuration\CustomProcessor.cs" />
7172
<Compile Include="Configuration\BatchProcessorsTests.cs" />

TestStack.BDDfy/Configuration/Processors.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ IEnumerable<IProcessor> _GetProcessors(Story story)
3333
}
3434
}
3535

36-
private readonly ProcessorFactory _testRunnerFactory = new ProcessorFactory(() => new TestRunner());
37-
public ProcessorFactory TestRunner { get { return _testRunnerFactory; } }
36+
private readonly TestRunnerFactory _testRunnerFactory = new TestRunnerFactory(() => new TestRunner());
37+
public TestRunnerFactory TestRunner { get { return _testRunnerFactory; } }
3838

3939
private readonly ProcessorFactory _consoleReportFactory = new ProcessorFactory(() => new ConsoleReporter());
4040
public ProcessorFactory ConsoleReport { get { return _consoleReportFactory; } }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using TestStack.BDDfy.Core;
3+
4+
namespace TestStack.BDDfy.Configuration
5+
{
6+
public class TestRunnerFactory : ProcessorFactory
7+
{
8+
internal TestRunnerFactory(Func<IProcessor> factory) : base(factory)
9+
{
10+
StopExecutionOnFailingThen = false;
11+
}
12+
13+
internal TestRunnerFactory(Func<IProcessor> factory, bool active) : base(factory, active)
14+
{
15+
StopExecutionOnFailingThen = false;
16+
}
17+
18+
/// <summary>
19+
/// Set to true if you want the execution pipleline to stop when a Then step fails. Defaulted to false
20+
/// </summary>
21+
public bool StopExecutionOnFailingThen { get; set; }
22+
}
23+
}

TestStack.BDDfy/Processors/TestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void Process(Story story)
1818
if (scenario.ExecuteStep(executionStep) == StepExecutionResult.Passed)
1919
continue;
2020

21-
if(!executionStep.Asserts)
21+
if (Configuration.Configurator.Processors.TestRunner.StopExecutionOnFailingThen || !executionStep.Asserts)
2222
break;
2323
}
2424
}

TestStack.BDDfy/TestStack.BDDfy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="Configuration\Processors.cs" />
7171
<Compile Include="Configuration\Scanners.cs" />
7272
<Compile Include="Configuration\StepScannerFactory.cs" />
73+
<Compile Include="Configuration\TestRunnerFactory.cs" />
7374
<Compile Include="Core\IBatchProcessor.cs" />
7475
<Compile Include="Processors\Reporters\ConsoleReporter.cs" />
7576
<Compile Include="Processors\Reporters\Diagnostics\DiagnosticsReportBuilder.cs" />

0 commit comments

Comments
 (0)