Skip to content

Commit 5dc3ec9

Browse files
committed
Merge pull request #123 from MehdiK/text-reporter
Adds tests for textreporter & fixes a bug with exception reporting
2 parents 42c2e22 + 681d69d commit 5dc3ec9

8 files changed

Lines changed: 204 additions & 7 deletions

TestStack.BDDfy.Tests/Reporters/Html/ClassicReportBuilderTests.ShouldProduceExpectedHtml.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ body{margin:0;padding:0;padding-bottom:40px;max-width:100%;background-color:#fff
104104
<li class='step Failed Assertion canToggle' data-toggle-target='step-3-3' >
105105
<span>Then no money is dispensed [Exception Message: 'Boom']</span>
106106
<div class='step FailedException' id='step-3-3'>
107-
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 65</code>
107+
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 90</code>
108108
</div>
109109
</li>
110110
</ul>

TestStack.BDDfy.Tests/Reporters/Html/ClassicReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ body{margin:0;padding:0;padding-bottom:40px;max-width:100%;background-color:#fff
113113
<td>
114114
<span class='canToggle' data-toggle-target='step-1-19'>Boom</span>
115115
<div class='step' id='step-1-19'>
116-
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 88</code>
116+
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 113</code>
117117
</div>
118118
</td>
119119
</tr>

TestStack.BDDfy.Tests/Reporters/Html/MetroReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abb
124124
<td>
125125
<span class='canToggle' data-toggle-target='step-1-19'>Boom</span>
126126
<div class='step FailedException' id='step-1-19'>
127-
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 88</code>
127+
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 113</code>
128128
</div>
129129
</td>
130130
</tr>

TestStack.BDDfy.Tests/Reporters/ReportTestData.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@ 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 unhappy examples story", "I want to see failed steps", "So that I can diagnose what's wrong", "Unhappy examples");
47+
var storyMetadata3 = new StoryMetadata(typeof(PlatinumAccountHolderStory), "As a happy examples story", "I want a clean report with examples", "So that the report is clean and readable", "Happy Examples");
48+
49+
const StoryMetadata testThatReportWorksWithNoStory = null;
50+
51+
var stories = new List<Story>()
52+
{
53+
new Story(storyMetadata1, new Scenario(typeof(HappyPathScenario), GetHappyExecutionSteps(), "Happy Path Scenario [for Happiness]")),
54+
new Story(storyMetadata1, new Scenario(typeof(SadPathScenario), GetFailingExecutionSteps(), "Sad Path Scenario [for Happiness]")),
55+
new Story(storyMetadata1, new Scenario(typeof(SadPathScenario), GetInconclusiveExecutionSteps(), "Inconclusive Scenario [for Happiness]")),
56+
new Story(storyMetadata1, new Scenario(typeof(SadPathScenario), GetNotImplementedExecutionSteps(), "Not Implemented Scenario [for Happiness]")),
57+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(HappyPathScenario), GetHappyExecutionSteps(), "Happy Path Scenario [with no story]")),
58+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(SadPathScenario), GetFailingExecutionSteps(), "Sad Path Scenario [with no story]")),
59+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(SadPathScenario), GetInconclusiveExecutionSteps(), "Inconclusive Scenario [with no story]")),
60+
new Story(testThatReportWorksWithNoStory, new Scenario(typeof(SadPathScenario), GetNotImplementedExecutionSteps(), "Not Implemented Scenario [with no story]")),
61+
new Story(storyMetadata2, GetScenarios(true, true)),
62+
new Story(storyMetadata3, GetScenarios(false, true)),
63+
};
64+
65+
return stories;
66+
}
67+
4368
public IEnumerable<Story> CreateTwoStoriesEachWithOneFailingScenarioAndOnePassingScenarioWithThreeStepsOfFiveMillisecondsAndEachHasTwoExamples()
4469
{
4570
var storyMetadata1 = new StoryMetadata(typeof(RegularAccountHolderStory), "As a person", "I want ice cream", "So that I can be happy", "Happiness");
@@ -159,6 +184,31 @@ private List<Step> GetSadExecutionSteps()
159184
return steps;
160185
}
161186

187+
private List<Step> GetFailingExecutionSteps()
188+
{
189+
var steps = new List<Step>
190+
{
191+
new Step(null, new StepTitle("Given a negative account balance"), true, ExecutionOrder.Assertion, true),
192+
new Step(null, new StepTitle("When the account holder requests money"), true, ExecutionOrder.Assertion, true),
193+
new Step(null, new StepTitle("Then no money is dispensed"), true, ExecutionOrder.Assertion, true),
194+
};
195+
196+
SetAllStepResults(steps, Result.Passed);
197+
198+
var last = steps.Last();
199+
last.Result = Result.Failed;
200+
try
201+
{
202+
throw new InvalidOperationException("Boom");
203+
}
204+
catch (Exception ex)
205+
{
206+
last.Exception = ex;
207+
}
208+
209+
return steps;
210+
}
211+
162212
private List<Step> GetInconclusiveExecutionSteps()
163213
{
164214
var steps = new List<Step>
@@ -202,6 +252,7 @@ private void SetAllStepResults(IEnumerable<Step> steps, Result result)
202252

203253
public class RegularAccountHolderStory { }
204254
public class GoldAccountHolderStory { }
255+
public class PlatinumAccountHolderStory { }
205256
public class ExampleScenario
206257
{
207258
public void GivenA__sign__AccountBalance() { }
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 ...\ReportTestData.cs
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+
50+
Scenario: Happy Path Scenario [with no story]
51+
Given a positive account balance
52+
When the account holder requests money
53+
Then money is dispensed
54+
55+
56+
57+
Scenario: Sad Path Scenario [with no story]
58+
Given a negative account balance [Passed]
59+
When the account holder requests money [Passed]
60+
Then no money is dispensed [Failed] [Boom] [Details at 1 below]
61+
62+
Exceptions:
63+
1. Boom
64+
at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetFailingExecutionSteps() in ...\ReportTestData.cs
65+
66+
67+
68+
Scenario: Inconclusive Scenario [with no story]
69+
Given a negative account balance [Passed]
70+
When the account holder requests money [Passed]
71+
Then no money is dispensed [Inconclusive]
72+
73+
74+
75+
Scenario: Not Implemented Scenario [with no story]
76+
Given a negative account balance [Passed]
77+
When the account holder requests money [Passed]
78+
Then no money is dispensed [Not implemented]
79+
80+
81+
Story: Unhappy examples
82+
As an unhappy examples story
83+
I want to see failed steps
84+
So that I can diagnose what's wrong
85+
86+
Scenario: Example Scenario
87+
Given a <sign> account balance
88+
When the account holder requests money
89+
Then money <action> dispensed
90+
91+
Examples:
92+
| sign | action | Result | Errors |
93+
| positive | is | Passed | |
94+
| negative | is not | Failed | Step: Then money <action> dispensed failed with exception: [Boom] [Details at 1 below] |
95+
96+
Exceptions:
97+
1. Boom
98+
at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs
99+
100+
101+
Story: Happy Examples
102+
As a happy examples story
103+
I want a clean report with examples
104+
So that the report is clean and readable
105+
106+
Scenario: Example Scenario
107+
Given a <sign> account balance
108+
When the account holder requests money
109+
Then money <action> dispensed
110+
111+
Examples:
112+
| sign | action |
113+
| positive | is |
114+
| negative | is not |
115+
116+
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.ToString(), StackTraceScrubber.Scrub);
27+
}
28+
}
29+
}

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

Lines changed: 4 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" />
@@ -171,6 +172,9 @@
171172
</ItemGroup>
172173
<ItemGroup>
173174
<Content Include="Reporters\Html\ClassicReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt" />
175+
<Content Include="Reporters\Html\MetroReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt" />
176+
<Content Include="Reporters\MarkDown\MarkDownReportBuilderTests.ShouldProduceExpectedMarkdown.approved.txt" />
177+
<Content Include="Reporters\TextReporter\TextReporterTests.ShouldProduceExpectedReport.approved.txt" />
174178
</ItemGroup>
175179
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
176180
<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)