Skip to content

Commit

Permalink
Add links to scenario files in the test report
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Dec 29, 2023
1 parent abe20a8 commit 373c64b
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 411 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ There's also an option to generate a nice html report for all test scenarios. Ju

![image](https://github.com/cezarypiatek/NScenario/assets/7759991/13c501d6-d26b-4406-93fb-1da29dca9bff)


This report browser supports links to scenario steps definition. To make it work, you need to set the following msbuild properties (or environment variables):
- RepositoryUrl
- SourceRevisionId

## 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 always override the default title by setting it explicitly during test scenario creation (especially useful for parametrized test methods):
Expand Down
17 changes: 16 additions & 1 deletion src/NScenario/ReportExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;

namespace NScenario;
Expand All @@ -8,8 +10,21 @@ public static class ReportExtensions
{
public static void SaveAsReport(this IReadOnlyList<ScenarioInfo> scenarios, string outputPath)
{
var callingAssembly = Assembly.GetCallingAssembly();
var sourceControlInfo = RepoPathResolver.GetSourceControlInfo(callingAssembly);
var repositoryRootDir = RepoPathResolver.FindRepoRootDirectory(scenarios.FirstOrDefault()?.FilePath);
var template = ResourceExtractor.GetEmbeddedResourceContent("NScenario.report-browser-template.html");
var report = template.Replace("//[DATA_PLACEHOLDER]", JsonSerializer.Serialize(scenarios));
var scenariosPayload = JsonSerializer.Serialize(new
{
SourceControlInfo = new
{
RepositoryUrl = sourceControlInfo?.RepositoryUrl,
Revision = sourceControlInfo?.Revision,
RepositoryRootDir = repositoryRootDir
},
Scenarios = scenarios
});
var report = template.Replace("//[DATA_PLACEHOLDER]", scenariosPayload);
File.WriteAllText(outputPath, report);
}
}
40 changes: 40 additions & 0 deletions src/NScenario/Reporting/RepoPathResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.IO;
using System.Linq;
using System.Reflection;

namespace NScenario;

internal static class RepoPathResolver
{
public static string? FindRepoRootDirectory(string? fileInTheRepo)
{
if (fileInTheRepo == null)
return null;

var currentDirectory = new DirectoryInfo(Path.GetDirectoryName(fileInTheRepo) ?? string.Empty);

while (currentDirectory is { Exists: true })
{
if (Directory.Exists(Path.Combine(currentDirectory.FullName, ".git")))
{
return currentDirectory.FullName;
}
currentDirectory = currentDirectory.Parent;
}

return null;
}

public static SourceControlInfo? GetSourceControlInfo(Assembly assembly)
{
var repositoryUrl = assembly.GetCustomAttributes<AssemblyMetadataAttribute>().FirstOrDefault(x => x.Key == "RepositoryUrl")?.Value;
var revision = assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion?.Split('+').LastOrDefault();

if (string.IsNullOrWhiteSpace(repositoryUrl) == false && string.IsNullOrWhiteSpace(revision) == false)
{
return new SourceControlInfo(repositoryUrl, revision);
}

return null;
}
}
3 changes: 3 additions & 0 deletions src/NScenario/Reporting/SourceControlInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace NScenario;

internal record SourceControlInfo(string RepositoryUrl, string Revision);
4 changes: 4 additions & 0 deletions src/NScenario/report-browser-template.html

Large diffs are not rendered by default.

Loading

0 comments on commit 373c64b

Please sign in to comment.