diff --git a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs index 606f9f1467..f420f861fe 100644 --- a/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs +++ b/test/Microsoft.TestPlatform.AcceptanceTests/MultitargetingTestHostTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Collections.Generic; + using Microsoft.TestPlatform.TestUtilities; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -17,14 +19,22 @@ public class MultitargetingTestHostTests : AcceptanceTestBase // xUnit supports net452 onwards, so that is why this starts at net452, I also don't test all framework versions [NetCoreRunner(NETFX452_48)] [NetFrameworkRunner(NETFX452_48)] - public void RunningTestWithAFailingDebugAssertDoesNotCrashTheHostingProcess(RunnerInfo runnerInfo) + public void TestRunInATesthostThatTargetsTheirChosenNETFramework(RunnerInfo runnerInfo) { SetTestEnvironment(_testEnvironment, runnerInfo); using var tempDir = new TempDirectory(); var assemblyPath = BuildMultipleAssemblyPath("MultitargetedNetFrameworkProject.dll").Trim('\"'); var arguments = PrepareArguments(assemblyPath, null, null, FrameworkArgValue, runnerInfo.InIsolationValue, resultsDirectory: tempDir.Path); - InvokeVsTest(arguments); + + // Tell the test project which target framework we are expecting it to run as. + // It has this value conditionally compiled, so it can compare it. + var env = new Dictionary + { + ["EXPECTED_TARGET_FRAMEWORK"] = runnerInfo.TargetFramework + }; + + InvokeVsTest(arguments, env); ValidateSummaryStatus(passedTestsCount: 1, failedTestsCount: 0, 0); } diff --git a/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs b/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs index 37516d123a..35a6b30809 100644 --- a/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs +++ b/test/TestAssets/MultitargetedNetFrameworkProject/UnitTest1.cs @@ -2,7 +2,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics; using System.IO; +using System.Linq; using System.Net.Security; using System.Security.Authentication; @@ -48,43 +50,12 @@ public class UnitTest1 #if NET48 public string TargetFramework { get; } = "NET48"; #endif - - // Using xUnit here because MSTest uses AppDomains by default and fixes this problem for us - // as long as the appdomains are enabled and modern .NET Framework is installed. [Fact] public void FailsUntilNet462ButPassesOnNewerNetFramework() { - Exception exception = null; - try - { - MemoryStream stream = new MemoryStream(); - SslStream sslStream = new SslStream(stream); - - // this throws SSLException on net451-net462, on net471 onwards it passes so we can use it to test that we target correctly - sslStream.BeginAuthenticateAsClient("microsoft.com", null, SslProtocols.None, false, new AsyncCallback(ProcessInformation), null); - } - catch (Exception ex) - { - exception = ex; - } + var expected = Environment.GetEnvironmentVariable("EXPECTED_TARGET_FRAMEWORK"); - switch (TargetFramework) - { - case "NET451": - case "NET452": - case "NET46": - case "NET461": - case "NET462": - Assert.NotNull(exception); - break; - default: - Assert.Null(exception); - break; - } - } - - static void ProcessInformation(IAsyncResult result) - { + Assert.Equal(expected, TargetFramework, ignoreCase: true); } } }