Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling vsts properties flow to test context #472

Merged
merged 2 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Adapter/MSTest.CoreAdapter/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ internal static class Constants

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

internal static readonly TestProperty TestRunIdProperty = TestProperty.Register(TestRunId, TestRunId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty TestPlanIdProperty = TestProperty.Register(TestPlanId, TestPlanId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty TestCaseIdProperty = TestProperty.Register(TestCaseId, TestCaseId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty TestPointIdProperty = TestProperty.Register(TestPointId, TestPointId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty TestConfigurationIdProperty = TestProperty.Register(TestConfigurationId, TestConfigurationId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty TestConfigurationNameProperty = TestProperty.Register(TestConfigurationName, TestConfigurationName, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty IsInLabEnvironmentProperty = TestProperty.Register(IsInLabEnvironment, IsInLabEnvironment, typeof(bool), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty BuildConfigurationIdProperty = TestProperty.Register(BuildConfigurationId, BuildConfigurationId, typeof(int), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty BuildDirectoryProperty = TestProperty.Register(BuildDirectory, BuildDirectory, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty BuildFlavorProperty = TestProperty.Register(BuildFlavor, BuildFlavor, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty BuildNumberProperty = TestProperty.Register(BuildNumber, BuildNumber, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty BuildPlatformProperty = TestProperty.Register(BuildPlatform, BuildPlatform, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty BuildUriProperty = TestProperty.Register(BuildUri, BuildUri, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty TfsServerCollectionUrlProperty = TestProperty.Register(TfsServerCollectionUrl, TfsServerCollectionUrl, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

internal static readonly TestProperty TfsTeamProjectProperty = TestProperty.Register(TfsTeamProject, TfsTeamProject, typeof(string), TestPropertyAttributes.Hidden, typeof(TestCase));

#endregion

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

private const string TestRunId = "__Tfs_TestRunId__";
private const string TestPlanId = "__Tfs_TestPlanId__";
private const string TestCaseId = "__Tfs_TestCaseId__";
private const string TestPointId = "__Tfs_TestPointId__";
private const string TestConfigurationId = "__Tfs_TestConfigurationId__";
private const string TestConfigurationName = "__Tfs_TestConfigurationName__";
private const string IsInLabEnvironment = "__Tfs_IsInLabEnvironment__";
private const string BuildConfigurationId = "__Tfs_BuildConfigurationId__";
private const string BuildDirectory = "__Tfs_BuildDirectory__";
private const string BuildFlavor = "__Tfs_BuildFlavor__";
private const string BuildNumber = "__Tfs_BuildNumber__";
private const string BuildPlatform = "__Tfs_BuildPlatform__";
private const string BuildUri = "__Tfs_BuildUri__";
private const string TfsServerCollectionUrl = "__Tfs_TfsServerCollectionUrl__";
private const string TfsTeamProject = "__Tfs_TeamProject__";

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution
{
using System.Collections.Generic;
using TestPlatformObjectModel = Microsoft.VisualStudio.TestPlatform.ObjectModel;

/// <summary>
/// Reads and parses the TcmTestProperties in order to populate them in TestRunParameters.
/// </summary>
internal static class TcmTestPropertiesProvider
{
/// <summary>
/// Gets tcm properties from test case.
/// </summary>
/// <param name="testCase">Test case.</param>
/// <returns>Tcm properties.</returns>
public static IDictionary<TestPlatformObjectModel.TestProperty, object> GetTcmProperties(TestPlatformObjectModel.TestCase testCase)
{
var tcmProperties = new Dictionary<TestPlatformObjectModel.TestProperty, object>();

// Return empty proeprties when testCase is null or when test case id is zero.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (testCase == null ||
testCase.GetPropertyValue<int>(Constants.TestCaseIdProperty, default(int)) == 0)
{
return tcmProperties;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we prefer returning null here and declare tcmProperties variable after this check has passed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done so as to not having null check outside this method context. In case we return null, we can't iterate over it via foreach loop untill there is a null check.

}

// Step 1: Add common properties.
tcmProperties[Constants.TestRunIdProperty] = testCase.GetPropertyValue<int>(Constants.TestRunIdProperty, default(int));
tcmProperties[Constants.TestPlanIdProperty] = testCase.GetPropertyValue<int>(Constants.TestPlanIdProperty, default(int));
tcmProperties[Constants.BuildConfigurationIdProperty] = testCase.GetPropertyValue<int>(Constants.BuildConfigurationIdProperty, default(int));
tcmProperties[Constants.BuildDirectoryProperty] = testCase.GetPropertyValue<string>(Constants.BuildDirectoryProperty, default(string));
tcmProperties[Constants.BuildFlavorProperty] = testCase.GetPropertyValue<string>(Constants.BuildFlavorProperty, default(string));
tcmProperties[Constants.BuildNumberProperty] = testCase.GetPropertyValue<string>(Constants.BuildNumberProperty, default(string));
tcmProperties[Constants.BuildPlatformProperty] = testCase.GetPropertyValue<string>(Constants.BuildPlatformProperty, default(string));
tcmProperties[Constants.BuildUriProperty] = testCase.GetPropertyValue<string>(Constants.BuildUriProperty, default(string));
tcmProperties[Constants.TfsServerCollectionUrlProperty] = testCase.GetPropertyValue<string>(Constants.TfsServerCollectionUrlProperty, default(string));
tcmProperties[Constants.TfsTeamProjectProperty] = testCase.GetPropertyValue<string>(Constants.TfsTeamProjectProperty, default(string));
tcmProperties[Constants.IsInLabEnvironmentProperty] = testCase.GetPropertyValue<bool>(Constants.IsInLabEnvironmentProperty, default(bool));

// Step 2: Add test case specific properties.
tcmProperties[Constants.TestCaseIdProperty] = testCase.GetPropertyValue<int>(Constants.TestCaseIdProperty, default(int));
tcmProperties[Constants.TestConfigurationIdProperty] = testCase.GetPropertyValue<int>(Constants.TestConfigurationIdProperty, default(int));
tcmProperties[Constants.TestConfigurationNameProperty] = testCase.GetPropertyValue<string>(Constants.TestConfigurationNameProperty, default(string));
tcmProperties[Constants.TestPointIdProperty] = testCase.GetPropertyValue<int>(Constants.TestPointIdProperty, default(int));

return tcmProperties;
}
}
}
30 changes: 29 additions & 1 deletion src/Adapter/MSTest.CoreAdapter/Execution/TestExecutionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ private void ExecuteTestsWithTestRunner(
"Executing test {0}",
unitTestElement.TestMethod.Name);

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

PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo(
"Executed test {0}",
Expand All @@ -383,6 +386,31 @@ private void ExecuteTestsWithTestRunner(
}
}

/// <summary>
/// Get test context properties.
/// </summary>
/// <param name="tcmProperties">Tcm properties.</param>
/// <param name="sourceLevelParameters">Source level parameters.</param>
/// <returns>Test context properties.</returns>
private IDictionary<string, object> GetTestContextProperties(IDictionary<TestProperty, object> tcmProperties, IDictionary<string, object> sourceLevelParameters)
{
var testContextProperties = new Dictionary<string, object>();

// Add tcm properties.
foreach (var propertyPair in tcmProperties)
{
testContextProperties[propertyPair.Key.Id] = propertyPair.Value;
}

// Add source level parameters.
foreach (var propertyPair in sourceLevelParameters)
{
testContextProperties[propertyPair.Key] = propertyPair.Value;
}

return testContextProperties;
}

private void RunCleanup(
ITestExecutionRecorder testExecutionRecorder,
UnitTestRunner testRunner)
Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/MSTest.CoreAdapter/Execution/UnitTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public override object InitializeLifetimeService()
/// Runs a single test.
/// </summary>
/// <param name="testMethod"> The test Method. </param>
/// <param name="testRunParameters"> The test Run Parameters. </param>
/// <param name="testContextProperties"> The test context properties. </param>
/// <returns> The <see cref="UnitTestResult"/>. </returns>
internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<string, object> testRunParameters)
internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<string, object> testContextProperties)
{
if (testMethod == null)
{
Expand All @@ -75,7 +75,7 @@ internal UnitTestResult[] RunSingleTest(TestMethod testMethod, IDictionary<strin
{
using (var writer = new ThreadSafeStringWriter(CultureInfo.InvariantCulture))
{
var properties = new Dictionary<string, object>(testRunParameters);
var properties = new Dictionary<string, object>(testContextProperties);
var testContext = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties);
testContext.SetOutcome(TestTools.UnitTesting.UnitTestOutcome.InProgress);

Expand Down
1 change: 1 addition & 0 deletions src/Adapter/MSTest.CoreAdapter/MSTest.CoreAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="Discovery\AssemblyEnumeratorWrapper.cs" />
<Compile Include="Discovery\TestMethodValidator.cs" />
<Compile Include="Execution\RunCleanupResult.cs" />
<Compile Include="Execution\TcmTestPropertiesProvider.cs" />
<Compile Include="Extensions\TestContextExtensions.cs" />
<Compile Include="Extensions\TestResultExtensions.cs" />
<Compile Include="Extensions\UnitTestOutcomeExtensions.cs" />
Expand Down
Loading