Skip to content

Commit f655647

Browse files
author
Richard Werkman
committed
setup mtp demo
1 parent 3550fd6 commit f655647

File tree

11 files changed

+283
-0
lines changed

11 files changed

+283
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{6B47BF30-C01E-47AE-B3C9-87A377648755}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TargetProject", "TargetProject\TargetProject.csproj", "{AF9F4468-EC92-4C35-A546-8AF84267E51C}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{6B47BF30-C01E-47AE-B3C9-87A377648755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{6B47BF30-C01E-47AE-B3C9-87A377648755}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{6B47BF30-C01E-47AE-B3C9-87A377648755}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{6B47BF30-C01E-47AE-B3C9-87A377648755}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{AF9F4468-EC92-4C35-A546-8AF84267E51C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{AF9F4468-EC92-4C35-A546-8AF84267E51C}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{AF9F4468-EC92-4C35-A546-8AF84267E51C}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{AF9F4468-EC92-4C35-A546-8AF84267E51C}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace TargetProject;
2+
3+
public class Class
4+
{
5+
public int Age { get; set; }
6+
7+
public bool IsExpiredBool() => Age >= 99;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using TargetProject;
2+
3+
namespace UnitTests
4+
{
5+
[TestClass]
6+
public class Constructs
7+
{
8+
9+
[TestMethod]
10+
[DataRow(99, false)]
11+
[DataRow(100, true)]
12+
public void TestAgeExplicit(int age, bool expired)
13+
{
14+
var sut = new Class { Age = age };
15+
Assert.IsTrue(expired == sut.IsExpiredBool());
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Microsoft.Testing.Platform.Builder;
2+
using Microsoft.Testing.Platform.Extensions;
3+
using Microsoft.Testing.Platform.Extensions.Messages;
4+
using Microsoft.Testing.Platform.Extensions.TestHost;
5+
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
6+
using System.Reflection;
7+
8+
namespace UnitTests
9+
{
10+
public class Injected
11+
{
12+
public static async Task Main(string[] args)
13+
{
14+
await MainAsync(args);
15+
}
16+
17+
public static async Task MainAsync(string[] args)
18+
{
19+
Console.WriteLine("Custom stryker testrunner starting...");
20+
21+
var testAssembly = Assembly.GetExecutingAssembly();
22+
23+
Console.WriteLine("Starting testrun");
24+
await RunTestsAsync(args, testAssembly);
25+
}
26+
27+
private static async Task RunTestsAsync(string[] args, Assembly testAssembly)
28+
{
29+
var testApplicationBuilder = await TestApplication.CreateBuilderAsync(args);
30+
// Register the testing framework
31+
testApplicationBuilder.AddMSTest(() => new[] { testAssembly });
32+
var factory = new CompositeExtensionFactory<StrykerExtension>(serviceProvider => new StrykerExtension());
33+
testApplicationBuilder.TestHost.AddDataConsumer(factory);
34+
var testApplication = await testApplicationBuilder.BuildAsync();
35+
36+
await testApplication.RunAsync();
37+
}
38+
}
39+
}
40+
41+
internal class StrykerExtension : IDataConsumer
42+
{
43+
public StrykerExtension()
44+
{
45+
}
46+
47+
string IExtension.Uid => "StrykerMsTestExtension";
48+
49+
string IExtension.Version => "1.0";
50+
51+
string IExtension.DisplayName => "StrykerMsTestExtension";
52+
53+
string IExtension.Description => "StrykerMsTestExtension";
54+
public Type[] DataTypesConsumed => new[] { typeof(TestNodeUpdateMessage) };
55+
56+
public Task ConsumeAsync(
57+
IDataProducer dataProducer,
58+
IData value,
59+
CancellationToken cancellationToken)
60+
{
61+
var testNodeUpdateMessage = (TestNodeUpdateMessage)value;
62+
63+
switch (testNodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>())
64+
{
65+
case InProgressTestNodeStateProperty _:
66+
{
67+
//Console.WriteLine("InProgressTestNodeStateProperty");
68+
Console.WriteLine("StrykerActiveMutation: " + Environment.GetEnvironmentVariable("StrykerActiveMutation"));
69+
break;
70+
}
71+
case PassedTestNodeStateProperty passed:
72+
{
73+
//Console.WriteLine("PassedTestNodeStateProperty");
74+
//Console.WriteLine(passed.Explanation);
75+
break;
76+
}
77+
case FailedTestNodeStateProperty failedTestNodeStateProperty:
78+
{
79+
//Console.WriteLine("FailedTestNodeStateProperty");
80+
break;
81+
}
82+
case SkippedTestNodeStateProperty _:
83+
{
84+
//Console.WriteLine("SkippedTestNodeStateProperty");
85+
break;
86+
}
87+
}
88+
89+
return Task.CompletedTask;
90+
}
91+
//public Task UpdateAsync(IEnvironmentVariables environmentVariables) => throw new NotImplementedException();
92+
public Task<ValidationResult> ValidateTestHostEnvironmentVariablesAsync(IReadOnlyEnvironmentVariables environmentVariables) => throw new NotImplementedException();
93+
Task<bool> IExtension.IsEnabledAsync() => Task.FromResult(true);
94+
public Task BeforeRunAsync(CancellationToken cancellationToken) => throw new NotImplementedException();
95+
public Task AfterRunAsync(int exitCode, CancellationToken cancellation) => throw new NotImplementedException();
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: Parallelize(Scope = ExecutionScope.ClassLevel)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="MSTest.Sdk/3.6.3">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
9+
<StartupObject>UnitTests.Injected</StartupObject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\TargetProject\TargetProject.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MsTestRunnerDemo", "MsTestRunnerDemo\MsTestRunnerDemo.csproj", "{2F38B092-5F27-4356-A5F3-28479CFD0D9E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2F38B092-5F27-4356-A5F3-28479CFD0D9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2F38B092-5F27-4356-A5F3-28479CFD0D9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2F38B092-5F27-4356-A5F3-28479CFD0D9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2F38B092-5F27-4356-A5F3-28479CFD0D9E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="MSTest.Sdk/3.6.3">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Reflection;
2+
3+
string testAssemblyPath = Path.GetFullPath("..\\..\\..\\..\\..\\InjectedTestProject\\UnitTests\\bin\\Debug\\net9.0\\UnitTests.dll");
4+
var exits = File.Exists(testAssemblyPath);
5+
6+
// this works, as long as the target framework is the same
7+
var testAssembly = Assembly.LoadFrom(testAssemblyPath);
8+
9+
var entryPoint = testAssembly.GetExportedTypes()
10+
.Where(t => t.IsClass && !t.IsAbstract)
11+
.FirstOrDefault(t => t.Name == "Injected");
12+
13+
var instance = Activator.CreateInstance(entryPoint);
14+
var method = entryPoint.GetMethod("Main");
15+
16+
// call the test method multiple times, works fast!
17+
Environment.SetEnvironmentVariable("StrykerActiveMutation", "1");
18+
var result = method.Invoke(instance, new object[] { new string[] { } });
19+
Console.WriteLine(result);
20+
21+
Environment.SetEnvironmentVariable("StrykerActiveMutation", "2");
22+
var result2 = method.Invoke(instance, new object[] { new string[] { } });
23+
Console.WriteLine(result2);
24+
25+
Environment.SetEnvironmentVariable("StrykerActiveMutation", "3");
26+
27+
var result3 = method.Invoke(instance, new object[] { new string[] { } });
28+
Console.WriteLine(result3);
29+
30+
Environment.SetEnvironmentVariable("StrykerActiveMutation", "4");
31+
var result4 = method.Invoke(instance, new object[] { new string[] { } });
32+
Console.WriteLine(result4);
33+
34+
// We don't want this, because it's slow, and doesn't support reruns... But it works for any target framework (as long as it's installed on the host)
35+
//var process = new System.Diagnostics.Process();
36+
//process.StartInfo.FileName = "dotnet";
37+
//process.StartInfo.Arguments = $"exec \"{testAssemblyPath}\"";
38+
//process.Start();
39+
//process.WaitForExit();
40+
//Console.WriteLine($"Process exited with code {process.ExitCode}");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Testing.Platform.Capabilities.TestFramework;
2+
using Microsoft.Testing.Platform.Extensions.TestHost;
3+
4+
internal class TestApplicationLifecycleCallbacks : ITestApplicationLifecycleCallbacks
5+
{
6+
public TestApplicationLifecycleCallbacks(IServiceProvider serviceProvider)
7+
{
8+
ServiceProvider = serviceProvider;
9+
}
10+
11+
public string Uid => throw new NotImplementedException();
12+
13+
public string Version => throw new NotImplementedException();
14+
15+
public string DisplayName => throw new NotImplementedException();
16+
17+
public string Description => throw new NotImplementedException();
18+
19+
public IServiceProvider ServiceProvider { get; }
20+
21+
public async Task AfterRunAsync(int exitCode, CancellationToken cancellation)
22+
{
23+
Console.WriteLine("AfterRunAsync");
24+
}
25+
26+
public async Task BeforeRunAsync(CancellationToken cancellationToken)
27+
{
28+
Console.WriteLine("BeforeRunAsync");
29+
}
30+
31+
public Task<bool> IsEnabledAsync()
32+
{
33+
throw new NotImplementedException();
34+
}
35+
}

0 commit comments

Comments
 (0)