-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathLifecycleAttributesVoidThreadingTests.cs
More file actions
151 lines (127 loc) · 4.57 KB
/
LifecycleAttributesVoidThreadingTests.cs
File metadata and controls
151 lines (127 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
using Microsoft.Testing.Platform.Helpers;
namespace MSTest.Acceptance.IntegrationTests;
[TestClass]
public sealed class LifecycleAttributesVoidThreadingTests : AcceptanceTestBase<LifecycleAttributesVoidThreadingTests.TestAssetFixture>
{
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
[DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))]
public async Task LifecycleAttributesVoidThreading_WhenMainIsNotSTA_RunsettingsAsksForSTA_OnWindows_ThreadIsSTA(string tfm)
{
var testHost = TestHost.LocateFrom(AssetFixture.TargetAssetPath, TestAssetFixture.ProjectName, tfm);
string runSettingsFilePath = Path.Combine(testHost.DirectoryName, "sta.runsettings");
TestHostResult testHostResult = await testHost.ExecuteAsync($"--settings {runSettingsFilePath}", environmentVariables: new()
{
["MSTEST_THREAD_STATE_IS_STA"] = "1",
}, cancellationToken: TestContext.CancellationToken);
testHostResult.AssertExitCodeIs(0);
testHostResult.AssertOutputContains("Passed!");
}
public sealed class TestAssetFixture() : TestAssetFixtureBase()
{
public const string ProjectName = "LifecycleAttributesVoid";
public string TargetAssetPath => GetAssetPath(ProjectName);
public override (string ID, string Name, string Code) GetAssetsToGenerate() => (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
private const string SourceCode = """
#file sta.runsettings
<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<RunConfiguration>
<ExecutionThreadApartmentState>STA</ExecutionThreadApartmentState>
</RunConfiguration>
</RunSettings>
#file mta.runsettings
<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<RunConfiguration>
<ExecutionThreadApartmentState>MTA</ExecutionThreadApartmentState>
</RunConfiguration>
</RunSettings>
#file LifecycleAttributesVoid.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<LangVersion>latest</LangVersion>
<GenerateTestingPlatformEntryPoint>true</GenerateTestingPlatformEntryPoint>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
</ItemGroup>
<ItemGroup>
<None Update="sta.runsettings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="mta.runsettings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
#file LifecycleAttributesVoid.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class LifecycleAttributesVoidTests
{
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
AssertCorrectThreadApartmentState();
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
AssertCorrectThreadApartmentState();
}
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
AssertCorrectThreadApartmentState();
}
[ClassCleanup]
public static void ClassCleanup()
{
AssertCorrectThreadApartmentState();
}
[TestInitialize]
public void TestInitialize()
{
AssertCorrectThreadApartmentState();
}
[TestCleanup]
public void TestCleanup()
{
AssertCorrectThreadApartmentState();
}
[TestMethod]
public void TestMethod()
{
}
private static void AssertCorrectThreadApartmentState()
{
var apartmentState = Thread.CurrentThread.GetApartmentState();
if (Environment.GetEnvironmentVariable("MSTEST_THREAD_STATE_IS_STA") == "1")
{
Assert.AreEqual(ApartmentState.STA, apartmentState);
}
else
{
Assert.AreNotEqual(ApartmentState.STA, apartmentState);
}
}
}
""";
}
public TestContext TestContext { get; set; }
}