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

Refactor acceptance tests to reduce duplication #1940

Merged
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 @@ -9,15 +9,15 @@
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;

[TestGroup]
public class AbortionTests : BaseAcceptanceTests
public class AbortionTests : AcceptanceTestBase
{
private const string AssetName = "Abort";
private readonly AbortionTestsFixture _abortionTestsFixture;
private readonly TestAssetFixture _testAssetFixture;

public AbortionTests(ITestExecutionContext testExecutionContext, AcceptanceFixture acceptanceFixture, AbortionTestsFixture abortionTestsFixture)
: base(testExecutionContext, acceptanceFixture)
public AbortionTests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture)
: base(testExecutionContext)
{
_abortionTestsFixture = abortionTestsFixture;
_testAssetFixture = testAssetFixture;
}

[ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))]
Expand All @@ -30,10 +30,10 @@ public async Task AbortWithCTRLPlusC_TestHost_Succeeded(string tfm)
return;
}

TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_abortionTestsFixture.TargetAssetPath, AssetName, tfm);
TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_testAssetFixture.TargetAssetPath, AssetName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync();

testHostResult.AssertHasExitCode(ExitCodes.TestSessionAborted);
testHostResult.AssertExitCodeIs(ExitCodes.TestSessionAborted);

// We check only in netcore for netfx is now showing in CI every time, the same behavior in local something works sometime nope.
// Manual test works pretty always as expected, looks like the implementation is different, we care more on .NET Core.
Expand All @@ -46,40 +46,27 @@ public async Task AbortWithCTRLPlusC_TestHost_Succeeded(string tfm)
}

[TestFixture(TestFixtureSharingStrategy.PerTestGroup)]
public sealed class AbortionTestsFixture : IAsyncInitializable, IDisposable
public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : TestAssetFixtureBase(acceptanceFixture.NuGetGlobalPackagesFolder)
{
private readonly AcceptanceFixture _acceptanceFixture;
private TestAsset? _testAsset;
public string TargetAssetPath => GetAssetPath(AssetName);

public AbortionTestsFixture(AcceptanceFixture acceptanceFixture)
public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
{
_acceptanceFixture = acceptanceFixture;
}

public string TargetAssetPath => _testAsset is null ? throw new ArgumentNullException(nameof(TestAsset)) : _testAsset.TargetAssetPath;

public async Task InitializeAsync(InitializationContext context)
{
_testAsset = await TestAsset.GenerateAssetAsync(AssetName, Sources.PatchCodeWithRegularExpression("tfms", TargetFrameworks.All.ToMSBuildTargetFrameworks()));

// We expect the same semantic for Linux, the test setup is not cross and we're using specific
// Windows API because this gesture is not easy xplat.
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
yield break;
}

await DotnetCli.RunAsync($"build -nodeReuse:false {_testAsset.TargetAssetPath} -c Release", _acceptanceFixture.NuGetGlobalPackagesFolder);
yield return (AssetName, AssetName, Sources.PatchTargetFrameworks(TargetFrameworks.All));
}

public void Dispose() => _testAsset?.Dispose();
}

private const string Sources = """
private const string Sources = """
#file Abort.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>tfms</TargetFrameworks>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<OutputType>Exe</OutputType>
<UseAppHost>true</UseAppHost>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -179,6 +166,6 @@ internal class Capabilities : ITestFrameworkCapabilities
{
ITestFrameworkCapability[] ICapabilities<ITestFrameworkCapability>.Capabilities => Array.Empty<ITestFrameworkCapability>();
}

""";
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;

[TestGroup]
public sealed class EnvironmentVariablesConfigurationProviderTests : BaseAcceptanceTests
public sealed class EnvironmentVariablesConfigurationProviderTests : AcceptanceTestBase
{
private readonly EnvironmentVariablesConfigurationProviderFixture _environmentVariablesConfigurationProviderFixture;
private readonly TestAssetFixture _testAssetFixture;

public EnvironmentVariablesConfigurationProviderTests(ITestExecutionContext testExecutionContext, AcceptanceFixture acceptanceFixture,
EnvironmentVariablesConfigurationProviderFixture environmentVariablesConfigurationProviderFixture)
: base(testExecutionContext, acceptanceFixture)
public EnvironmentVariablesConfigurationProviderTests(ITestExecutionContext testExecutionContext, TestAssetFixture testAssetFixture)
: base(testExecutionContext)
{
_environmentVariablesConfigurationProviderFixture = environmentVariablesConfigurationProviderFixture;
_testAssetFixture = testAssetFixture;
}

private const string AssetName = "EnvironmentVariablesConfigurationProvider";

[ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))]
public async Task DefaultEnabledEnvironmentVariablesConfiguration_SetEnvironmentVariable_ShouldSucceed(string currentTfm)
{
TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_environmentVariablesConfigurationProviderFixture.TargetAssetPath, AssetName, currentTfm);
TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_testAssetFixture.TargetAssetPath, AssetName, currentTfm);
TestHostResult testHostResult = await testHost.ExecuteAsync(
null,
new Dictionary<string, string>()
Expand All @@ -32,13 +31,13 @@ public async Task DefaultEnabledEnvironmentVariablesConfiguration_SetEnvironment
{ "MyValue", "MyVal" },
{ "MYENVVAR__MYPROP1__MYPROP2", "MyVal" },
});
testHostResult.AssertHasExitCode(ExitCodes.Success);
testHostResult.AssertExitCodeIs(ExitCodes.Success);
}

[ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))]
public async Task EnabledEnvironmentVariablesConfiguration_SetEnvironmentVariable_ShouldSucceed(string currentTfm)
{
TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_environmentVariablesConfigurationProviderFixture.TargetAssetPath, AssetName, currentTfm);
TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_testAssetFixture.TargetAssetPath, AssetName, currentTfm);
TestHostResult testHostResult = await testHost.ExecuteAsync(
null,
new Dictionary<string, string>()
Expand All @@ -47,50 +46,39 @@ public async Task EnabledEnvironmentVariablesConfiguration_SetEnvironmentVariabl
{ "MyValue", "MyVal" },
{ "MYENVVAR__MYPROP1__MYPROP2", "MyVal" },
});
testHostResult.AssertHasExitCode(ExitCodes.Success);
testHostResult.AssertExitCodeIs(ExitCodes.Success);
}

[ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))]
public async Task DisabledEnvironmentVariablesConfiguration_SetEnvironmentVariable_ShouldSucceed(string currentTfm)
{
TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_environmentVariablesConfigurationProviderFixture.TargetAssetPath, AssetName, currentTfm);
TestInfrastructure.TestHost testHost = TestInfrastructure.TestHost.LocateFrom(_testAssetFixture.TargetAssetPath, AssetName, currentTfm);
TestHostResult testHostResult = await testHost.ExecuteAsync(
null,
new Dictionary<string, string>()
{
{ "EnableConfigurationSource", "false" },
});
testHostResult.AssertHasExitCode(ExitCodes.Success);
testHostResult.AssertExitCodeIs(ExitCodes.Success);
}

[TestFixture(TestFixtureSharingStrategy.PerTestGroup)]
public sealed class EnvironmentVariablesConfigurationProviderFixture : IAsyncInitializable, IDisposable
public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture)
: TestAssetFixtureBase(acceptanceFixture.NuGetGlobalPackagesFolder)
{
private readonly AcceptanceFixture _acceptanceFixture;
private TestAsset? _testAsset;
public string TargetAssetPath => GetAssetPath(AssetName);

public string TargetAssetPath => _testAsset is null ? throw new ArgumentNullException(nameof(TestAsset)) : _testAsset.TargetAssetPath;

public EnvironmentVariablesConfigurationProviderFixture(AcceptanceFixture acceptanceFixture)
{
_acceptanceFixture = acceptanceFixture;
}

public async Task InitializeAsync(InitializationContext context)
public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
{
_testAsset = await TestAsset.GenerateAssetAsync(AssetName, Sources.PatchCodeWithRegularExpression("tfms", TargetFrameworks.All.ToMSBuildTargetFrameworks()));
await DotnetCli.RunAsync($"build -nodeReuse:false {_testAsset.TargetAssetPath} -c Release", _acceptanceFixture.NuGetGlobalPackagesFolder);
yield return (AssetName, AssetName, Sources.PatchTargetFrameworks(TargetFrameworks.All));
}

public void Dispose() => _testAsset?.Dispose();
}

private const string Sources = """
private const string Sources = """
#file EnvironmentVariablesConfigurationProvider.csproj

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>tfms</TargetFrameworks>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<OutputType>Exe</OutputType>
<UseAppHost>true</UseAppHost>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -219,4 +207,5 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)
}
}
""";
}
}
Loading