Skip to content

Commit 373c64b

Browse files
committed
Add links to scenario files in the test report
1 parent abe20a8 commit 373c64b

File tree

8 files changed

+494
-411
lines changed

8 files changed

+494
-411
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ There's also an option to generate a nice html report for all test scenarios. Ju
201201

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

204+
205+
This report browser supports links to scenario steps definition. To make it work, you need to set the following msbuild properties (or environment variables):
206+
- RepositoryUrl
207+
- SourceRevisionId
208+
204209
## Test scenario title
205210

206211
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):

src/NScenario/ReportExtensions.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Collections.Generic;
22
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
35
using System.Text.Json;
46

57
namespace NScenario;
@@ -8,8 +10,21 @@ public static class ReportExtensions
810
{
911
public static void SaveAsReport(this IReadOnlyList<ScenarioInfo> scenarios, string outputPath)
1012
{
13+
var callingAssembly = Assembly.GetCallingAssembly();
14+
var sourceControlInfo = RepoPathResolver.GetSourceControlInfo(callingAssembly);
15+
var repositoryRootDir = RepoPathResolver.FindRepoRootDirectory(scenarios.FirstOrDefault()?.FilePath);
1116
var template = ResourceExtractor.GetEmbeddedResourceContent("NScenario.report-browser-template.html");
12-
var report = template.Replace("//[DATA_PLACEHOLDER]", JsonSerializer.Serialize(scenarios));
17+
var scenariosPayload = JsonSerializer.Serialize(new
18+
{
19+
SourceControlInfo = new
20+
{
21+
RepositoryUrl = sourceControlInfo?.RepositoryUrl,
22+
Revision = sourceControlInfo?.Revision,
23+
RepositoryRootDir = repositoryRootDir
24+
},
25+
Scenarios = scenarios
26+
});
27+
var report = template.Replace("//[DATA_PLACEHOLDER]", scenariosPayload);
1328
File.WriteAllText(outputPath, report);
1429
}
1530
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Reflection;
4+
5+
namespace NScenario;
6+
7+
internal static class RepoPathResolver
8+
{
9+
public static string? FindRepoRootDirectory(string? fileInTheRepo)
10+
{
11+
if (fileInTheRepo == null)
12+
return null;
13+
14+
var currentDirectory = new DirectoryInfo(Path.GetDirectoryName(fileInTheRepo) ?? string.Empty);
15+
16+
while (currentDirectory is { Exists: true })
17+
{
18+
if (Directory.Exists(Path.Combine(currentDirectory.FullName, ".git")))
19+
{
20+
return currentDirectory.FullName;
21+
}
22+
currentDirectory = currentDirectory.Parent;
23+
}
24+
25+
return null;
26+
}
27+
28+
public static SourceControlInfo? GetSourceControlInfo(Assembly assembly)
29+
{
30+
var repositoryUrl = assembly.GetCustomAttributes<AssemblyMetadataAttribute>().FirstOrDefault(x => x.Key == "RepositoryUrl")?.Value;
31+
var revision = assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion?.Split('+').LastOrDefault();
32+
33+
if (string.IsNullOrWhiteSpace(repositoryUrl) == false && string.IsNullOrWhiteSpace(revision) == false)
34+
{
35+
return new SourceControlInfo(repositoryUrl, revision);
36+
}
37+
38+
return null;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace NScenario;
2+
3+
internal record SourceControlInfo(string RepositoryUrl, string Revision);

src/NScenario/report-browser-template.html

+4
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)