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

MSTest runner: allow overriding TestRunParameters #3106

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static void AddMSTest(this ITestApplicationBuilder testApplicationBuilder
MSTestExtension extension = new();
testApplicationBuilder.AddRunSettingsService(extension);
testApplicationBuilder.AddTestCaseFilterService(extension);
testApplicationBuilder.AddTestRunParametersService(extension);
testApplicationBuilder.RegisterTestFramework(
_ => new TestFrameworkCapabilities(new VSTestBridgeExtensionBaseCapabilities()),
(capabilities, serviceProvider) => new MSTestBridgedTestFramework(extension, getTestAssemblies, serviceProvider, capabilities));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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;

namespace MSTest.Acceptance.IntegrationTests;

[TestGroup]
public sealed class TestRunParametersTests : AcceptanceTestBase
{
private readonly TestAssetFixture _testAssetFixture;

public TestRunParametersTests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture)
: base(testExecutionContext)
{
_testAssetFixture = testAssetFixture;
}

[ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))]
public async Task TestRunParameters_WhenProvidingMultipleArgumentsToTheOption(string tfm)
{
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync("--settings my.runsettings --test-parameter MyParameter2=MyValue2 MyParameter3=MyValue3");

// Assert
testHostResult.AssertExitCodeIs(0);
testHostResult.AssertOutputContains("Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1");
}

[ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))]
public async Task TestRunParameters_WhenProvidingMultipleMultipleTimesTheOptionAndArgument(string tfm)
{
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync("--settings my.runsettings --test-parameter MyParameter2=MyValue2 --test-parameter MyParameter3=MyValue3");

// Assert
testHostResult.AssertExitCodeIs(0);
testHostResult.AssertOutputContains("Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1");
}

[TestFixture(TestFixtureSharingStrategy.PerTestGroup)]
public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : TestAssetFixtureBase(acceptanceFixture.NuGetGlobalPackagesFolder)
{
public const string ProjectName = "TestRunParameters";

public string ProjectPath => GetAssetPath(ProjectName);

public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
{
yield return (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}

private const string SourceCode = """
#file TestRunParameters.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
<PackageReference Include="Microsoft.Testing.Platform" Version="$MicrosoftTestingPlatformVersion$" />
</ItemGroup>

<ItemGroup>
<None Update="*.runsettings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

#file my.runsettings
<RunSettings>
<TestRunParameters>
<Parameter name="MyParameter1" value="MyValue1" />
<Parameter name="MyParameter2" value="" />
</TestRunParameters>
</RunSettings>

#file UnitTest1.cs
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
public TestContext TestContext { get; set; }

[TestMethod]
public void MyTestMethod()
{
Assert.AreEqual("MyValue1", TestContext.Properties["MyParameter1"]);
Assert.AreEqual("MyValue2", TestContext.Properties["MyParameter2"]);
Assert.AreEqual("MyValue3", TestContext.Properties["MyParameter3"]);
}
}
""";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ Execute a .NET Test Application\.
--minimum-expected-tests Specifies the minimum number of tests that are expected to run\.
--results-directory The directory where the test results are going to be placed\. If the specified directory doesn't exist, it's created\. The default is TestResults in the directory that contains the test application\.
Extension options:
--filter Filters tests using the given expression\. For more information, see the Filter option details section\. For more information and examples on how to use selective unit test filtering, see https://learn\.microsoft\.com/dotnet/core/testing/selective-unit-tests\.
--settings The path, relative or absolute, to the \.runsettings file\. For more information and examples on how to configure test run, see https://learn\.microsoft\.com/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file#the-runsettings-file
--filter Filters tests using the given expression\. For more information, see the Filter option details section\. For more information and examples on how to use selective unit test filtering, see https://learn\.microsoft\.com/dotnet/core/testing/selective-unit-tests\.
--settings The path, relative or absolute, to the \.runsettings file\. For more information and examples on how to configure test run, see https://learn\.microsoft\.com/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file#the-runsettings-file
--test-parameter Specify or override a key-value pair parameter\. For more information and examples, see https://learn\.microsoft\.com/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file#testrunparameters
""";

testHostResult.AssertOutputMatchesRegex(RegexMatchPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ public async Task Info_WhenMSTestExtensionRegistered_OutputInfoContentOfRegister
Arity: 1
Hidden: False
Description: Filters tests using the given expression\. For more information, see the Filter option details section\. For more information and examples on how to use selective unit test filtering, see https://learn\.microsoft\.com/dotnet/core/testing/selective-unit-tests\.
--test-parameter
Arity: 1\.\.2147483647
Hidden: False
Description: Specify or override a key-value pair parameter\. For more information and examples, see https://learn\.microsoft\.com/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file#testrunparameters
Registered tools:
There are no registered tools.
""";

testHostResult.AssertOutputMatchesRegex(RegexMatchPattern);
Expand Down