Skip to content

Commit

Permalink
Add support for parallel exectution to MarkdownFormatterOutputWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Nov 14, 2022
1 parent 68fc8b0 commit 355dedd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ using NUnit.Framework;
[SetUpFixture]
public class AllTestsSetup
{
private readonly MarkdownFormatterOutputWriter _reportWriter = new MarkdownFormatterOutputWriter(title: "Sample tests with NScenario");
private readonly MarkdownFormatterOutputWriter _reportWriter = new (title: "Sample tests with NScenario", currentTestIdentifierAccessor: ()=> TestContext.CurrentContext.Test.ID);

[OneTimeSetUp]
public void GlobalSetup()
Expand Down Expand Up @@ -199,7 +199,7 @@ public class AllTestsSetup

## Test scenario title

Test scenario title is generated by removing underscores and splitting camel/pascalcase string from the test method name (`[CallerMemberName]` is used to retrieve that name). This allows for immediate review of the test name (I saw many, extremely long and totally ridiculous test method names. A good test method name should reveal the intention of the test case, not its details). You can alwasy override the default title by setting it explicitly during test scenario creation (especially useful for parametrized test methods):
Test scenario title is generated by removing underscores and splitting camel/pascalcase string from the test method name (`[CallerMemberName]` is used to retrieve that name). This allows for immediate review of the test name (I saw many, extremely long and totally ridiculous test method names. A good test method name should reveal the intention of the test case, not its details). You can always override the default title by setting it explicitly during test scenario creation (especially useful for parametrized test methods):

```cs
[TestCase(false)]
Expand Down
3 changes: 3 additions & 0 deletions src/NScenario/NScenario.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>logo.jpg</PackageIcon>
<LangVersion>8</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MarkdownSharp" Version="2.0.5" />
Expand Down
48 changes: 39 additions & 9 deletions src/NScenario/OutputWriters/MarkdownFormatterOutputWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using MarkdownSharp;
Expand All @@ -8,13 +9,34 @@ namespace NScenario.OutputWriters
{
public class MarkdownFormatterOutputWriter: IScenarioOutputWriter
{
private readonly StringBuilder _outputBuilder = new StringBuilder();
private readonly Func<string> _currentTestIdentifierAccessor;
private readonly Dictionary<string, StringBuilder> _outputBuilders = new Dictionary<string, StringBuilder>();

public MarkdownFormatterOutputWriter(string? title = null)
private StringBuilder OutputBuilder
{
get
{
var testId = _currentTestIdentifierAccessor();
if (_outputBuilders.TryGetValue(testId, out var builder))
{
return builder;
}

return _outputBuilders[testId] = new StringBuilder();
}
}

/// <summary>
/// Export test scenario transcript to markdown report
/// </summary>
/// <param name="title">Report title</param>
/// <param name="currentTestIdentifierAccessor">Function that returns unique identifier representing currently executed scenario. Should be provided when tests are executed in parallel mode</param>
public MarkdownFormatterOutputWriter(string? title = null, Func<string>? currentTestIdentifierAccessor = null)
{
_currentTestIdentifierAccessor = currentTestIdentifierAccessor ?? (() => "SHARED_ID");
if (title != null)
{
_outputBuilder.AppendLine($"# {title}");
OutputBuilder.AppendLine($"# {title}");
}
}

Expand All @@ -24,27 +46,35 @@ public void WriteStepDescription(string description)
if (prefixPosition >= 0)
{
var newDescription = description.Insert(prefixPosition, "* ");
_outputBuilder.AppendLine(newDescription);
OutputBuilder.AppendLine(newDescription);
}
else
{
_outputBuilder.AppendLine("* " + description);
OutputBuilder.AppendLine("* " + description);
}
}

public void WriteScenarioTitle(string scenarioTitle)
{
_outputBuilder.AppendLine($"\r\n## {scenarioTitle}\r\n");
OutputBuilder.AppendLine($"\r\n## {scenarioTitle}\r\n");
}

public string GetMarkdown() => _outputBuilder.ToString();
public string GetMarkdown()
{
var mergedMarkdown = new StringBuilder();
foreach (var builder in _outputBuilders.Values)
{
mergedMarkdown.Append(builder.ToString());
}
return mergedMarkdown.ToString();
}

public void Save(string path) => File.WriteAllText(path, _outputBuilder.ToString(), Encoding.UTF8);
public void Save(string path) => File.WriteAllText(path, GetMarkdown(), Encoding.UTF8);


public void ExportToHtml(string path)
{
var markdownContent = _outputBuilder.ToString();
var markdownContent = GetMarkdown();
var mm = new Markdown();
var htmlContent = mm.Transform(markdownContent);
File.WriteAllText(path, $"<html><head></head><body>{htmlContent}<body></html>", Encoding.UTF8);
Expand Down

0 comments on commit 355dedd

Please sign in to comment.