Skip to content

Commit 4a7f6de

Browse files
committed
added tests for textreporter & fixed a bug with exception reporting
1 parent 647f460 commit 4a7f6de

5 files changed

Lines changed: 211 additions & 4 deletions

File tree

TestStack.BDDfy.Tests/Reporters/ReportTestData.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ public IEnumerable<Story> CreateMixContainingEachTypeOfOutcome()
4040
return stories;
4141
}
4242

43+
public IEnumerable<Story> CreateMixContainingEachTypeOfOutcomeWithOneScenarioPerStory()
44+
{
45+
var storyMetadata1 = new StoryMetadata(typeof(RegularAccountHolderStory), "As a person", "I want ice cream", "So that I can be happy", "Happiness");
46+
var storyMetadata2 = new StoryMetadata(typeof(GoldAccountHolderStory), "As an account holder", "I want to withdraw cash", "So that I can get money when the bank is closed", "Account holder withdraws cash");
47+
48+
const StoryMetadata testThatReportWorksWithNoStory = null;
49+
50+
var stories = new List<Story>()
51+
{
52+
new Story(storyMetadata1, new Scenario(typeof(HappyPathScenario), GetHappyExecutionSteps(), "Happy Path Scenario [for Happiness]")),
53+
new Story(storyMetadata1, new Scenario(typeof(SadPathScenario), GetFailingExecutionSteps(), "Sad Path Scenario [for Happiness]")),
54+
new Story(storyMetadata1, new Scenario(typeof(SadPathScenario), GetInconclusiveExecutionSteps(), "Inconclusive Scenario [for Happiness]")),
55+
new Story(storyMetadata1, new Scenario(typeof(SadPathScenario), GetNotImplementedExecutionSteps(), "Not Implemented Scenario [for Happiness]")),
56+
new Story(storyMetadata2, new Scenario(typeof(HappyPathScenario), GetHappyExecutionSteps(), "Happy Path Scenario [for Account holder withdraws cash]")),
57+
new Story(storyMetadata2, new Scenario(typeof(SadPathScenario), GetFailingExecutionSteps(), "Sad Path Scenario [for Account holder withdraws cash]")),
58+
new Story(storyMetadata2, new Scenario(typeof(SadPathScenario), GetInconclusiveExecutionSteps(), "Inconclusive Scenario [for Account holder withdraws cash]")),
59+
new Story(storyMetadata2, new Scenario(typeof(SadPathScenario), GetNotImplementedExecutionSteps(), "Not Implemented Scenario [for Account holder withdraws cash]")),
60+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(HappyPathScenario), GetHappyExecutionSteps(), "Happy Path Scenario [with no story]")),
61+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(SadPathScenario), GetFailingExecutionSteps(), "Sad Path Scenario [with no story]")),
62+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(SadPathScenario), GetInconclusiveExecutionSteps(), "Inconclusive Scenario [with no story]")),
63+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(SadPathScenario), GetNotImplementedExecutionSteps(), "Not Implemented Scenario [with no story]")),
64+
};
65+
66+
return stories;
67+
}
68+
4369
public IEnumerable<Story> CreateTwoStoriesEachWithOneFailingScenarioAndOnePassingScenarioWithThreeStepsOfFiveMillisecondsAndEachHasTwoExamples()
4470
{
4571
var storyMetadata1 = new StoryMetadata(typeof(RegularAccountHolderStory), "As a person", "I want ice cream", "So that I can be happy", "Happiness");
@@ -159,6 +185,31 @@ private List<Step> GetSadExecutionSteps()
159185
return steps;
160186
}
161187

188+
private List<Step> GetFailingExecutionSteps()
189+
{
190+
var steps = new List<Step>
191+
{
192+
new Step(null, new StepTitle("Given a negative account balance"), true, ExecutionOrder.Assertion, true),
193+
new Step(null, new StepTitle("When the account holder requests money"), true, ExecutionOrder.Assertion, true),
194+
new Step(null, new StepTitle("Then no money is dispensed"), true, ExecutionOrder.Assertion, true),
195+
};
196+
197+
SetAllStepResults(steps, Result.Passed);
198+
199+
var last = steps.Last();
200+
last.Result = Result.Failed;
201+
try
202+
{
203+
throw new InvalidOperationException("Boom");
204+
}
205+
catch (Exception ex)
206+
{
207+
last.Exception = ex;
208+
}
209+
210+
return steps;
211+
}
212+
162213
private List<Step> GetInconclusiveExecutionSteps()
163214
{
164215
var steps = new List<Step>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
Story: Happiness
2+
As a person
3+
I want ice cream
4+
So that I can be happy
5+
6+
Scenario: Happy Path Scenario [for Happiness]
7+
Given a positive account balance
8+
When the account holder requests money
9+
Then money is dispensed
10+
11+
12+
Story: Happiness
13+
As a person
14+
I want ice cream
15+
So that I can be happy
16+
17+
Scenario: Sad Path Scenario [for Happiness]
18+
Given a negative account balance [Passed]
19+
When the account holder requests money [Passed]
20+
Then no money is dispensed [Failed] [Boom] [Details at 1 below]
21+
22+
Exceptions:
23+
1. Boom
24+
at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetFailingExecutionSteps() in c:\_mine\Code\TestStack\TestStack.BDDfy\TestStack.BDDfy.Tests\Reporters\ReportTestData.cs:line 203
25+
26+
27+
Story: Happiness
28+
As a person
29+
I want ice cream
30+
So that I can be happy
31+
32+
Scenario: Inconclusive Scenario [for Happiness]
33+
Given a negative account balance [Passed]
34+
When the account holder requests money [Passed]
35+
Then no money is dispensed [Inconclusive]
36+
37+
38+
Story: Happiness
39+
As a person
40+
I want ice cream
41+
So that I can be happy
42+
43+
Scenario: Not Implemented Scenario [for Happiness]
44+
Given a negative account balance [Passed]
45+
When the account holder requests money [Passed]
46+
Then no money is dispensed [Not implemented]
47+
48+
49+
Story: Account holder withdraws cash
50+
As an account holder
51+
I want to withdraw cash
52+
So that I can get money when the bank is closed
53+
54+
Scenario: Happy Path Scenario [for Account holder withdraws cash]
55+
Given a positive account balance
56+
When the account holder requests money
57+
Then money is dispensed
58+
59+
60+
Story: Account holder withdraws cash
61+
As an account holder
62+
I want to withdraw cash
63+
So that I can get money when the bank is closed
64+
65+
Scenario: Sad Path Scenario [for Account holder withdraws cash]
66+
Given a negative account balance [Passed]
67+
When the account holder requests money [Passed]
68+
Then no money is dispensed [Failed] [Boom] [Details at 1 below]
69+
70+
Exceptions:
71+
1. Boom
72+
at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetFailingExecutionSteps() in c:\_mine\Code\TestStack\TestStack.BDDfy\TestStack.BDDfy.Tests\Reporters\ReportTestData.cs:line 203
73+
74+
75+
Story: Account holder withdraws cash
76+
As an account holder
77+
I want to withdraw cash
78+
So that I can get money when the bank is closed
79+
80+
Scenario: Inconclusive Scenario [for Account holder withdraws cash]
81+
Given a negative account balance [Passed]
82+
When the account holder requests money [Passed]
83+
Then no money is dispensed [Inconclusive]
84+
85+
86+
Story: Account holder withdraws cash
87+
As an account holder
88+
I want to withdraw cash
89+
So that I can get money when the bank is closed
90+
91+
Scenario: Not Implemented Scenario [for Account holder withdraws cash]
92+
Given a negative account balance [Passed]
93+
When the account holder requests money [Passed]
94+
Then no money is dispensed [Not implemented]
95+
96+
97+
98+
Scenario: Happy Path Scenario [with no story]
99+
Given a positive account balance
100+
When the account holder requests money
101+
Then money is dispensed
102+
103+
104+
105+
Scenario: Sad Path Scenario [with no story]
106+
Given a negative account balance [Passed]
107+
When the account holder requests money [Passed]
108+
Then no money is dispensed [Failed] [Boom] [Details at 1 below]
109+
110+
Exceptions:
111+
1. Boom
112+
at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetFailingExecutionSteps() in c:\_mine\Code\TestStack\TestStack.BDDfy\TestStack.BDDfy.Tests\Reporters\ReportTestData.cs:line 203
113+
114+
115+
116+
Scenario: Inconclusive Scenario [with no story]
117+
Given a negative account balance [Passed]
118+
When the account holder requests money [Passed]
119+
Then no money is dispensed [Inconclusive]
120+
121+
122+
123+
Scenario: Not Implemented Scenario [with no story]
124+
Given a negative account balance [Passed]
125+
When the account holder requests money [Passed]
126+
Then no money is dispensed [Not implemented]
127+
128+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Text;
3+
using ApprovalTests;
4+
using NUnit.Framework;
5+
using TestStack.BDDfy.Reporters;
6+
7+
namespace TestStack.BDDfy.Tests.Reporters.MarkDown
8+
{
9+
[TestFixture]
10+
public class TextReporterTests
11+
{
12+
[Test]
13+
[MethodImpl(MethodImplOptions.NoInlining)]
14+
public void ShouldProduceExpectedReport()
15+
{
16+
var stories = new ReportTestData().CreateMixContainingEachTypeOfOutcomeWithOneScenarioPerStory();
17+
var actual = new StringBuilder();
18+
19+
foreach (var story in stories)
20+
{
21+
var textReporter = new TextReporter();
22+
textReporter.Process(story);
23+
actual.AppendLine(textReporter.ToString());
24+
}
25+
26+
Approvals.Verify(actual);
27+
}
28+
}
29+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="Arguments\MultipleArgumentsProvidedForTheSameStep.cs" />
7878
<Compile Include="Configuration\SequentialKeyGeneratorTests.cs" />
7979
<Compile Include="Configuration\TestRunnerTests.cs" />
80+
<Compile Include="Reporters\TextReporter\TextReporterTests.cs" />
8081
<Compile Include="Reporters\Html\MetroReportBuilderTests.cs" />
8182
<Compile Include="Processors\TestRunnerTests.cs" />
8283
<Compile Include="Reporters\Html\ClassicReportBuilderTests.cs" />
@@ -173,6 +174,7 @@
173174
<Content Include="Reporters\Html\ClassicReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt" />
174175
<Content Include="Reporters\Html\MetroReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt" />
175176
<Content Include="Reporters\MarkDown\MarkDownReportBuilderTests.ShouldProduceExpectedMarkdown.approved.txt" />
177+
<Content Include="Reporters\TextReporter\TextReporterTests.ShouldProduceExpectedReport.approved.txt" />
176178
</ItemGroup>
177179
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
178180
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

TestStack.BDDfy/Reporters/TextReporter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ void ReportOnStep(Scenario scenario, Step step, bool includeResults)
160160
message = "\t" + PrefixWithSpaceIfRequired(step);
161161

162162
if (step.Exception != null)
163-
{
164-
message = CreateExceptionMessage(step);
165-
}
163+
message += CreateExceptionMessage(step);
166164

167165
if (step.Result == Result.Inconclusive || step.Result == Result.NotImplemented)
168166
ForegroundColor = ConsoleColor.Yellow;
@@ -213,7 +211,6 @@ void ReportExceptions()
213211

214212
static string FlattenExceptionMessage(string message)
215213
{
216-
// ToDo: if gets complex will change it with a regex
217214
return message
218215
.Replace("\t", " ") // replace tab with one space
219216
.Replace(Environment.NewLine, ", ") // replace new line with one space

0 commit comments

Comments
 (0)