Skip to content

Commit e50e1f7

Browse files
authored
Enabling tcm properties flow to test context (#472)
* Enabling association for mstest v2 * review comments
1 parent d03a20d commit e50e1f7

File tree

8 files changed

+365
-4
lines changed

8 files changed

+365
-4
lines changed

src/Adapter/MSTest.CoreAdapter/Constants.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,36 @@ internal static class Constants
5050

5151
internal static readonly TestProperty InnerResultsCountProperty = TestProperty.Register("InnerResultsCount", InnerResultsCountLabel, typeof(int), TestPropertyAttributes.Hidden, typeof(TestResult));
5252

53+
internal static readonly TestProperty TestRunIdProperty = TestProperty.Register(TestRunId, TestRunId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));
54+
55+
internal static readonly TestProperty TestPlanIdProperty = TestProperty.Register(TestPlanId, TestPlanId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));
56+
57+
internal static readonly TestProperty TestCaseIdProperty = TestProperty.Register(TestCaseId, TestCaseId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));
58+
59+
internal static readonly TestProperty TestPointIdProperty = TestProperty.Register(TestPointId, TestPointId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));
60+
61+
internal static readonly TestProperty TestConfigurationIdProperty = TestProperty.Register(TestConfigurationId, TestConfigurationId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));
62+
63+
internal static readonly TestProperty TestConfigurationNameProperty = TestProperty.Register(TestConfigurationName, TestConfigurationName, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
64+
65+
internal static readonly TestProperty IsInLabEnvironmentProperty = TestProperty.Register(IsInLabEnvironment, IsInLabEnvironment, typeof(bool), TestPropertyAttributes.Hidden, typeof(TestCase));
66+
67+
internal static readonly TestProperty BuildConfigurationIdProperty = TestProperty.Register(BuildConfigurationId, BuildConfigurationId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));
68+
69+
internal static readonly TestProperty BuildDirectoryProperty = TestProperty.Register(BuildDirectory, BuildDirectory, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
70+
71+
internal static readonly TestProperty BuildFlavorProperty = TestProperty.Register(BuildFlavor, BuildFlavor, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
72+
73+
internal static readonly TestProperty BuildNumberProperty = TestProperty.Register(BuildNumber, BuildNumber, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
74+
75+
internal static readonly TestProperty BuildPlatformProperty = TestProperty.Register(BuildPlatform, BuildPlatform, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
76+
77+
internal static readonly TestProperty BuildUriProperty = TestProperty.Register(BuildUri, BuildUri, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
78+
79+
internal static readonly TestProperty TfsServerCollectionUrlProperty = TestProperty.Register(TfsServerCollectionUrl, TfsServerCollectionUrl, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
80+
81+
internal static readonly TestProperty TfsTeamProjectProperty = TestProperty.Register(TfsTeamProject, TfsTeamProject, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));
82+
5383
#endregion
5484

5585
#region Private Constants
@@ -69,6 +99,22 @@ internal static class Constants
6999
private const string ParentExecIdLabel = "ParentExecId";
70100
private const string InnerResultsCountLabel = "InnerResultsCount";
71101

102+
private const string TestRunId = "__Tfs_TestRunId__";
103+
private const string TestPlanId = "__Tfs_TestPlanId__";
104+
private const string TestCaseId = "__Tfs_TestCaseId__";
105+
private const string TestPointId = "__Tfs_TestPointId__";
106+
private const string TestConfigurationId = "__Tfs_TestConfigurationId__";
107+
private const string TestConfigurationName = "__Tfs_TestConfigurationName__";
108+
private const string IsInLabEnvironment = "__Tfs_IsInLabEnvironment__";
109+
private const string BuildConfigurationId = "__Tfs_BuildConfigurationId__";
110+
private const string BuildDirectory = "__Tfs_BuildDirectory__";
111+
private const string BuildFlavor = "__Tfs_BuildFlavor__";
112+
private const string BuildNumber = "__Tfs_BuildNumber__";
113+
private const string BuildPlatform = "__Tfs_BuildPlatform__";
114+
private const string BuildUri = "__Tfs_BuildUri__";
115+
private const string TfsServerCollectionUrl = "__Tfs_TfsServerCollectionUrl__";
116+
private const string TfsTeamProject = "__Tfs_TeamProject__";
117+
72118
#endregion
73119
}
74120
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution
5+
{
6+
using System.Collections.Generic;
7+
using TestPlatformObjectModel = Microsoft.VisualStudio.TestPlatform.ObjectModel;
8+
9+
/// <summary>
10+
/// Reads and parses the TcmTestProperties in order to populate them in TestRunParameters.
11+
/// </summary>
12+
internal static class TcmTestPropertiesProvider
13+
{
14+
/// <summary>
15+
/// Gets tcm properties from test case.
16+
/// </summary>
17+
/// <param name="testCase">Test case.</param>
18+
/// <returns>Tcm properties.</returns>
19+
public static IDictionary<TestPlatformObjectModel.TestProperty, object> GetTcmProperties(TestPlatformObjectModel.TestCase testCase)
20+
{
21+
var tcmProperties = new Dictionary<TestPlatformObjectModel.TestProperty, object>();
22+
23+
// Return empty properties when testCase is null or when test case id is zero.
24+
if (testCase == null ||
25+
testCase.GetPropertyValue<int>(Constants.TestCaseIdProperty, default(int)) == 0)
26+
{
27+
return tcmProperties;
28+
}
29+
30+
// Step 1: Add common properties.
31+
tcmProperties[Constants.TestRunIdProperty] = testCase.GetPropertyValue<int>(Constants.TestRunIdProperty, default(int));
32+
tcmProperties[Constants.TestPlanIdProperty] = testCase.GetPropertyValue<int>(Constants.TestPlanIdProperty, default(int));
33+
tcmProperties[Constants.BuildConfigurationIdProperty] = testCase.GetPropertyValue<int>(Constants.BuildConfigurationIdProperty, default(int));
34+
tcmProperties[Constants.BuildDirectoryProperty] = testCase.GetPropertyValue<string>(Constants.BuildDirectoryProperty, default(string));
35+
tcmProperties[Constants.BuildFlavorProperty] = testCase.GetPropertyValue<string>(Constants.BuildFlavorProperty, default(string));
36+
tcmProperties[Constants.BuildNumberProperty] = testCase.GetPropertyValue<string>(Constants.BuildNumberProperty, default(string));
37+
tcmProperties[Constants.BuildPlatformProperty] = testCase.GetPropertyValue<string>(Constants.BuildPlatformProperty, default(string));
38+
tcmProperties[Constants.BuildUriProperty] = testCase.GetPropertyValue<string>(Constants.BuildUriProperty, default(string));
39+
tcmProperties[Constants.TfsServerCollectionUrlProperty] = testCase.GetPropertyValue<string>(Constants.TfsServerCollectionUrlProperty, default(string));
40+
tcmProperties[Constants.TfsTeamProjectProperty] = testCase.GetPropertyValue<string>(Constants.TfsTeamProjectProperty, default(string));
41+
tcmProperties[Constants.IsInLabEnvironmentProperty] = testCase.GetPropertyValue<bool>(Constants.IsInLabEnvironmentProperty, default(bool));
42+
43+
// Step 2: Add test case specific properties.
44+
tcmProperties[Constants.TestCaseIdProperty] = testCase.GetPropertyValue<int>(Constants.TestCaseIdProperty, default(int));
45+
tcmProperties[Constants.TestConfigurationIdProperty] = testCase.GetPropertyValue<int>(Constants.TestConfigurationIdProperty, default(int));
46+
tcmProperties[Constants.TestConfigurationNameProperty] = testCase.GetPropertyValue<string>(Constants.TestConfigurationNameProperty, default(string));
47+
tcmProperties[Constants.TestPointIdProperty] = testCase.GetPropertyValue<int>(Constants.TestPointIdProperty, default(int));
48+
49+
return tcmProperties;
50+
}
51+
}
52+
}

src/Adapter/MSTest.CoreAdapter/Execution/TestExecutionManager.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ private void ExecuteTestsWithTestRunner(
371371
"Executing test {0}",
372372
unitTestElement.TestMethod.Name);
373373

374-
var unitTestResult = testRunner.RunSingleTest(unitTestElement.TestMethod, sourceLevelParameters);
374+
// Run single test passing test context properties to it.
375+
var tcmProperties = TcmTestPropertiesProvider.GetTcmProperties(currentTest);
376+
var testContextProperties = this.GetTestContextProperties(tcmProperties, sourceLevelParameters);
377+
var unitTestResult = testRunner.RunSingleTest(unitTestElement.TestMethod, testContextProperties);
375378

376379
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo(
377380
"Executed test {0}",
@@ -383,6 +386,31 @@ private void ExecuteTestsWithTestRunner(
383386
}
384387
}
385388

389+
/// <summary>
390+
/// Get test context properties.
391+
/// </summary>
392+
/// <param name="tcmProperties">Tcm properties.</param>
393+
/// <param name="sourceLevelParameters">Source level parameters.</param>
394+
/// <returns>Test context properties.</returns>
395+
private IDictionary<string, object> GetTestContextProperties(IDictionary<TestProperty, object> tcmProperties, IDictionary<string, object> sourceLevelParameters)
396+
{
397+
var testContextProperties = new Dictionary<string, object>();
398+
399+
// Add tcm properties.
400+
foreach (var propertyPair in tcmProperties)
401+
{
402+
testContextProperties[propertyPair.Key.Id] = propertyPair.Value;
403+
}
404+
405+
// Add source level parameters.
406+
foreach (var propertyPair in sourceLevelParameters)
407+
{
408+
testContextProperties[propertyPair.Key] = propertyPair.Value;
409+
}
410+
411+
return testContextProperties;
412+
}
413+
386414
private void RunCleanup(
387415
ITestExecutionRecorder testExecutionRecorder,
388416
UnitTestRunner testRunner)

src/Adapter/MSTest.CoreAdapter/Execution/UnitTestRunner.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public override object InitializeLifetimeService()
6262
/// Runs a single test.
6363
/// </summary>
6464
/// <param name="testMethod"> The test Method. </param>
65-
/// <param name="testRunParameters"> The test Run Parameters. </param>
65+
/// <param name="testContextProperties"> The test context properties. </param>
6666
/// <returns> The <see cref="UnitTestResult"/>. </returns>
67-
internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<string, object> testRunParameters)
67+
internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<string, object> testContextProperties)
6868
{
6969
if (testMethod == null)
7070
{
@@ -75,7 +75,7 @@ internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<strin
7575
{
7676
using (var writer = new ThreadSafeStringWriter(CultureInfo.InvariantCulture))
7777
{
78-
var properties = new Dictionary<string, object>(testRunParameters);
78+
var properties = new Dictionary<string, object>(testContextProperties);
7979
var testContext = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties);
8080
testContext.SetOutcome(TestTools.UnitTesting.UnitTestOutcome.InProgress);
8181

src/Adapter/MSTest.CoreAdapter/MSTest.CoreAdapter.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<Compile Include="Discovery\AssemblyEnumeratorWrapper.cs" />
3535
<Compile Include="Discovery\TestMethodValidator.cs" />
3636
<Compile Include="Execution\RunCleanupResult.cs" />
37+
<Compile Include="Execution\TcmTestPropertiesProvider.cs" />
3738
<Compile Include="Extensions\TestContextExtensions.cs" />
3839
<Compile Include="Extensions\TestResultExtensions.cs" />
3940
<Compile Include="Extensions\UnitTestOutcomeExtensions.cs" />

0 commit comments

Comments
 (0)