Skip to content

Commit 7681f8a

Browse files
committed
add custom formatter example
1 parent f7c618f commit 7681f8a

File tree

9 files changed

+232
-1
lines changed

9 files changed

+232
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Text;
2+
using Io.Cucumber.Messages.Types;
3+
using Reqnroll.Formatters;
4+
using Reqnroll.Formatters.Configuration;
5+
using Reqnroll.Formatters.RuntimeSupport;
6+
using Reqnroll.Utils;
7+
8+
namespace ReqnrollFormatters.Custom;
9+
10+
public class CustomFormatter(IFormattersConfigurationProvider configurationProvider, IFormatterLog logger, IFileSystem fileSystem)
11+
: FileWritingFormatterBase(configurationProvider, logger, fileSystem, "custom", ".txt", "output.txt")
12+
{
13+
private TextWriter? _outputWriter;
14+
private readonly Dictionary<string, Pickle> _picklesById = new();
15+
private readonly Dictionary<string, TestCase> _testCasesById = new();
16+
private readonly Dictionary<string, TestCaseStarted> _testCaseStartsById = new();
17+
private readonly Dictionary<string, TestStepResultStatus> _testCaseStatusesByStartId = new();
18+
19+
protected override void OnTargetFileStreamInitialized(Stream targetFileStream)
20+
{
21+
// We use AutoFlush to ensure that the output is written immediately.
22+
// Without this setting, we would need to override FlushTargetFileStream
23+
// to ensure that the output is written at the end of the test run.
24+
_outputWriter = new StreamWriter(targetFileStream) { AutoFlush = true };
25+
}
26+
27+
protected override void OnTargetFileStreamDisposing()
28+
{
29+
_outputWriter?.Dispose();
30+
_outputWriter = null;
31+
}
32+
33+
protected override async Task WriteToFile(Envelope? envelope, CancellationToken cancellationToken)
34+
{
35+
if (_outputWriter == null)
36+
return;
37+
38+
if (envelope?.Pickle != null)
39+
{
40+
_picklesById[envelope.Pickle.Id] = envelope.Pickle;
41+
}
42+
if (envelope?.TestCase != null)
43+
{
44+
_testCasesById[envelope.TestCase.Id] = envelope.TestCase;
45+
}
46+
if (envelope?.TestCaseStarted != null)
47+
{
48+
_testCaseStartsById[envelope.TestCaseStarted.Id] = envelope.TestCaseStarted;
49+
_testCaseStatusesByStartId[envelope.TestCaseStarted.Id] = TestStepResultStatus.UNKNOWN;
50+
}
51+
if (envelope?.TestStepFinished != null)
52+
{
53+
TestStepResultStatus AggregateStatus(TestStepResultStatus previous, TestStepResultStatus next)
54+
{
55+
if (previous == TestStepResultStatus.UNKNOWN)
56+
return next;
57+
if (next != TestStepResultStatus.UNKNOWN && next != TestStepResultStatus.SKIPPED)
58+
return next;
59+
return (TestStepResultStatus)Math.Max((int)previous, (int)next);
60+
}
61+
62+
_testCaseStatusesByStartId[envelope.TestStepFinished.TestCaseStartedId] =
63+
AggregateStatus(_testCaseStatusesByStartId[envelope.TestStepFinished.TestCaseStartedId], envelope.TestStepFinished.TestStepResult.Status);
64+
}
65+
if (envelope?.TestCaseFinished != null)
66+
{
67+
if (_testCaseStartsById.TryGetValue(envelope.TestCaseFinished.TestCaseStartedId, out var testCaseStarted) &&
68+
_testCasesById.TryGetValue(testCaseStarted.TestCaseId, out var testCase) &&
69+
_picklesById.TryGetValue(testCase.PickleId, out var pickle))
70+
{
71+
var output = new StringBuilder($"TEST: {pickle.Name}, RESULT: {_testCaseStatusesByStartId[envelope.TestCaseFinished.TestCaseStartedId]}");
72+
await _outputWriter.WriteLineAsync(output, cancellationToken);
73+
}
74+
}
75+
}
76+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Reqnroll.Formatters;
2+
using Reqnroll.Plugins;
3+
using Reqnroll.UnitTestProvider;
4+
using ReqnrollFormatters.Custom;
5+
6+
[assembly: RuntimePlugin(typeof(CustomFormatterPlugin))]
7+
8+
namespace ReqnrollFormatters.Custom;
9+
10+
public class CustomFormatterPlugin : IRuntimePlugin
11+
{
12+
public void Initialize(RuntimePluginEvents runtimePluginEvents, RuntimePluginParameters runtimePluginParameters, UnitTestProviderConfiguration unitTestProviderConfiguration)
13+
{
14+
runtimePluginEvents.RegisterGlobalDependencies += (_, args) =>
15+
{
16+
args.ObjectContainer.RegisterTypeAs<CustomFormatter, ICucumberMessageFormatter>("custom");
17+
};
18+
}
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@attachment
2+
Feature: Attachments
3+
4+
A short summary of the feature
5+
6+
Scenario: A scenario with an attachment
7+
When the step generates an attachment
8+
And the step generates a test output
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Feature: Basics
2+
3+
This is the description of the **"basics" feature**.
4+
5+
* It includes scenarios to show *basic examples* for formatters.
6+
* This description is displayed as [Markdown](https://en.wikipedia.org/wiki/Markdown) in the formatter output.
7+
8+
@basic
9+
Scenario: Passing scenario
10+
Given the first parameter is "foo bar"
11+
And 42 is the second parameter
12+
When I do something
13+
Then the scenario passes
14+
15+
Scenario: Failing scenario
16+
When I do something
17+
Then the scenario fails
18+
And nothing else matters
19+
20+
Scenario Outline: Outline with multiple examples
21+
Given the first parameter is "<param>"
22+
And <other param> is the second parameter
23+
When I do something
24+
Then the scenario <result>
25+
Examples:
26+
| param | other param | result |
27+
| foo bar | 12 | passes |
28+
| baz | 23 | fails |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
11+
<PackageReference Include="Reqnroll.MsTest" Version="3.0.0-local" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="3.9.3" />
13+
<PackageReference Include="MSTest.TestFramework" Version="3.9.3" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update="sample-image.png">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Reqnroll;
2+
3+
namespace ReqnrollFormatters.Custom.StepDefinitions;
4+
5+
[Binding]
6+
public sealed class ProjectStepDefinitions(IReqnrollOutputHelper outputHelper)
7+
{
8+
[When("I do something")]
9+
public void WhenIDoSomething()
10+
{
11+
}
12+
13+
[Then("the scenario fails")]
14+
public void ThenTheScenarioFails()
15+
{
16+
throw new Exception("simulated error");
17+
}
18+
19+
[Then("nothing else matters")]
20+
public void ThenNothingElseMatters()
21+
{
22+
}
23+
24+
25+
[Given("the first parameter is {string}")]
26+
public void GivenTheFirstParameterIs(string firstParam)
27+
{
28+
}
29+
30+
[Given("{int} is the second parameter")]
31+
public void WhenIsTheSecondParameter(int secondParam)
32+
{
33+
}
34+
35+
[Then("the scenario passes")]
36+
public void ThenTheScenarioPasses()
37+
{
38+
}
39+
40+
[When("the step generates an attachment")]
41+
public void WhenTheStepGeneratesAnAttachment()
42+
{
43+
outputHelper.AddAttachment("sample-image.png");
44+
}
45+
46+
[When("the step generates a test output")]
47+
public void WhenTheStepGeneratesATestOutput()
48+
{
49+
outputHelper.WriteLine("this is a text" + Environment.NewLine + "this is a second line");
50+
}
51+
52+
[BeforeScenario("@attachment")]
53+
public void BeforeAttachmentScenario()
54+
{
55+
outputHelper.WriteLine("before attachment scenario hook executed");
56+
}
57+
58+
[AfterScenario("@attachment")]
59+
public void AfterAttachmentScenario()
60+
{
61+
outputHelper.WriteLine("after attachment scenario hook executed");
62+
}
63+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://schemas.reqnroll.net/reqnroll-config-latest.json",
3+
4+
"formatters": {
5+
"custom": {
6+
"outputFilePath": "reqnroll_report.txt"
7+
}
8+
}
9+
}
745 Bytes
Loading

ReqnrollFormatters/ReqnrollFormatters.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
4-
VisualStudioVersion = 17.14.36203.30 d17.14
4+
VisualStudioVersion = 17.14.36203.30
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReqnrollFormatters.Standard", "ReqnrollFormatters.Standard\ReqnrollFormatters.Standard.csproj", "{463D753E-74D1-4373-AE79-D824EEE8EFBE}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReqnrollFormatters.Custom", "ReqnrollFormatters.Custom\ReqnrollFormatters.Custom.csproj", "{FE6BE948-0314-11F3-BDA9-AE209CB7EC28}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{463D753E-74D1-4373-AE79-D824EEE8EFBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{463D753E-74D1-4373-AE79-D824EEE8EFBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{463D753E-74D1-4373-AE79-D824EEE8EFBE}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{FE6BE948-0314-11F3-BDA9-AE209CB7EC28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{FE6BE948-0314-11F3-BDA9-AE209CB7EC28}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{FE6BE948-0314-11F3-BDA9-AE209CB7EC28}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{FE6BE948-0314-11F3-BDA9-AE209CB7EC28}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)