Skip to content

Commit a1ef199

Browse files
committed
Move TestContext.json from run to test method scope
1 parent e212571 commit a1ef199

3 files changed

Lines changed: 39 additions & 30 deletions

File tree

src/Dibix.Testing/Utilities/TestMethodTestResultFileComposer.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.IO.Compression;
5+
using System.Reflection;
56
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Serialization;
69

710
namespace Dibix.Testing
811
{
@@ -44,12 +47,39 @@ public void ZipTestOutput(string resultRunDirectory, IEnumerable<string> resultF
4447
RegisterResultFile(path);
4548
}
4649

50+
public void RegisterTextContextInfo(TestContext testContext) => AddResultFile("TestContext.json", JsonConvert.SerializeObject(testContext, new JsonSerializerSettings
51+
{
52+
Formatting = Formatting.Indented,
53+
ContractResolver = new TestContextContractResolver()
54+
}), testContext);
55+
4756
public string AddResultFile(string fileName) => AddResultFile(fileName, _testContext);
4857

4958
public string AddResultFile(string fileName, string content) => AddResultFile(fileName, content, _testContext);
5059

5160
public string ImportResultFile(string filePath) => ImportResultFile(filePath, _testContext);
5261

5362
public void RegisterResultFile(string path) => RegisterResultFile(path, _testContext);
63+
64+
private sealed class TestContextContractResolver : DefaultContractResolver, IContractResolver
65+
{
66+
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
67+
{
68+
JsonProperty property = base.CreateProperty(member, memberSerialization);
69+
70+
JsonProperty result = member.DeclaringType?.FullName switch
71+
{
72+
// Self referencing loop detected for property 'Context' with type 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation'. Path ''.
73+
"Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation" when property.PropertyName == "Context" => null,
74+
75+
// We don't need this property for debugging purposes
76+
"Microsoft.VisualStudio.TestTools.UnitTesting.TestContext" when property.PropertyName == "CancellationTokenSource" => null,
77+
78+
_ => property
79+
};
80+
81+
return result;
82+
}
83+
}
5484
}
5585
}

src/Dibix.Testing/Utilities/TestResultFileManager.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static TestResultFileManager FromTestContext(TestContext testContext, boo
4040
TestMethodTestResultFileComposer testMethodFileComposer = TestMethodTestResultFileComposer.Create(testContext, resultTestsDirectory);
4141
WinMergeComparisonComposer winMergeComparisonComposer = new WinMergeComparisonComposer(testMethodFileComposer, testRunFileComposer, testContext);
4242
TestResultFileManager resultFileComposer = new TestResultFileManager(testRunFileComposer, testMethodFileComposer, winMergeComparisonComposer, testContext, scope);
43+
resultFileComposer.Initialize();
4344
return resultFileComposer;
4445
}
4546

@@ -114,6 +115,14 @@ string FormatContent(EventLogEntry entry)
114115
.Each((x, i) => composer.AddResultFile($"EventLogEntry_{i + 1}_{x.EntryType}.txt", FormatContent(x), _testContext));
115116
}
116117

118+
private void Initialize()
119+
{
120+
if (_scope == TestClassInstanceScope.AssemblyInitialize)
121+
return;
122+
123+
_testMethodFileComposer.RegisterTextContextInfo(_testContext);
124+
}
125+
117126
private TestResultFileComposer GetTargetTestResultFileComposer(TestClassInstanceScope scope) => scope switch
118127
{
119128
TestClassInstanceScope.AssemblyInitialize => _testRunFileComposer,

src/Dibix.Testing/Utilities/TestRunTestResultFileComposer.cs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using System.Reflection;
88
using System.Text;
99
using Microsoft.VisualStudio.TestTools.UnitTesting;
10-
using Newtonsoft.Json;
11-
using Newtonsoft.Json.Serialization;
1210

1311
namespace Dibix.Testing
1412
{
@@ -57,7 +55,6 @@ public void CopyTestOutput(IEnumerable<string> resultFiles)
5755

5856
private void Initialize(TestContext testContext)
5957
{
60-
EnsureTestContextDump(testContext);
6158
EnsureEnvironmentDump(testContext);
6259
}
6360

@@ -67,12 +64,6 @@ private void ImportResultFiles(TestContext testContext)
6764
testContext.AddResultFile(resultFile);
6865
}
6966

70-
private void EnsureTestContextDump(TestContext testContext) => AddResultFile("TestContext.json", JsonConvert.SerializeObject(testContext, new JsonSerializerSettings
71-
{
72-
Formatting = Formatting.Indented,
73-
ContractResolver = new TestContextContractResolver()
74-
}), testContext);
75-
7667
private void EnsureEnvironmentDump(TestContext testContext)
7768
{
7869
IDictionary<string, object> environmentVariables = Environment.GetEnvironmentVariables()
@@ -134,26 +125,5 @@ private static string CreateTestRunDirectory(TestContext testContext)
134125
string path = Path.Combine(Path.GetTempPath(), "TestResults", directoryName, assemblyName);
135126
return path;
136127
}
137-
138-
private sealed class TestContextContractResolver : DefaultContractResolver, IContractResolver
139-
{
140-
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
141-
{
142-
JsonProperty property = base.CreateProperty(member, memberSerialization);
143-
144-
JsonProperty result = member.DeclaringType?.FullName switch
145-
{
146-
// Self referencing loop detected for property 'Context' with type 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation'. Path ''.
147-
"Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation" when property.PropertyName == "Context" => null,
148-
149-
// We don't need this property for debugging purposes
150-
"Microsoft.VisualStudio.TestTools.UnitTesting.TestContext" when property.PropertyName == "CancellationTokenSource" => null,
151-
152-
_ => property
153-
};
154-
155-
return result;
156-
}
157-
}
158128
}
159129
}

0 commit comments

Comments
 (0)