Add TestData property to the TestContext (#4015)

This commit is contained in:
Amaury Levé 2024-11-07 15:19:29 +01:00 коммит произвёл GitHub
Родитель 766e40e96f
Коммит 34d3d66155
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
7 изменённых файлов: 130 добавлений и 3 удалений

Просмотреть файл

@ -354,8 +354,8 @@ internal class TestMethodRunner
private TestResult[] ExecuteTestWithDataSource(UTF.ITestDataSource? testDataSource, object?[]? data)
{
var stopwatch = Stopwatch.StartNew();
_testMethodInfo.SetArguments(data);
_testContext.SetTestData(data);
TestResult[] testResults = ExecuteTest(_testMethodInfo);
stopwatch.Stop();

Просмотреть файл

@ -42,6 +42,13 @@ public interface ITestContext
/// <param name="dataRow">data row.</param>
void SetDataRow(object? dataRow);
/// <summary>
/// Sets the data that will be provided to the test.
/// For non-parameterized tests, the value will be <see langword="null"/>.
/// </summary>
/// <param name="data">The data.</param>
void SetTestData(object?[]? data);
/// <summary>
/// Set connection for TestContext.
/// </summary>

Просмотреть файл

@ -1 +1,3 @@
#nullable enable
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ITestContext.SetTestData(object?[]? data) -> void
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.SetTestData(object?[]? data) -> void

Просмотреть файл

@ -269,6 +269,9 @@ public class TestContextImplementation : TestContext, ITestContext
#endif
}
/// <inheritdoc/>
public void SetTestData(object?[]? data) => TestData = data;
/// <summary>
/// Set connection for TestContext.
/// </summary>

Просмотреть файл

@ -1 +1,3 @@
#nullable enable
#nullable enable
Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestData.get -> object?[]?
Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestData.set -> void

Просмотреть файл

@ -53,6 +53,8 @@ public abstract class TestContext
/// </summary>
public virtual CancellationTokenSource CancellationTokenSource { get; protected set; } = new();
public object?[]? TestData { get; protected set; }
#if NETFRAMEWORK
/// <summary>
/// Gets the current data row when test is used for data driven testing.

Просмотреть файл

@ -17,13 +17,23 @@ public sealed class TestContextTests : AcceptanceTestBase
public async Task TestContextsAreCorrectlySet()
{
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, TargetFrameworks.NetCurrent.Arguments);
TestHostResult testHostResult = await testHost.ExecuteAsync();
TestHostResult testHostResult = await testHost.ExecuteAsync("--filter ClassName~TestContextCtor");
// Assert
testHostResult.AssertExitCodeIs(0);
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 4, skipped: 0);
}
public async Task TestContext_TestData_PropertyContainsExpectedValue()
{
var testHost = TestHost.LocateFrom(_testAssetFixture.ProjectPath, TestAssetFixture.ProjectName, TargetFrameworks.NetCurrent.Arguments);
TestHostResult testHostResult = await testHost.ExecuteAsync("--filter ClassName~TestContextData");
// Assert
testHostResult.AssertExitCodeIs(0);
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 3, skipped: 0);
}
[TestFixture(TestFixtureSharingStrategy.PerTestGroup)]
public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : TestAssetFixtureBase(acceptanceFixture.NuGetGlobalPackagesFolder)
{
@ -56,6 +66,7 @@ public sealed class TestContextTests : AcceptanceTestBase
</Project>
#file UnitTest1.cs
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
@ -126,6 +137,106 @@ public class TestContextCtorDerived : TestContextCtor
_testContext.WriteLine("Method TestContextCtorDerived.DerivedTestMethod() was called");
}
}
[TestClass]
public class TestContextDataFromNonParameterizedMethod
{
public TestContext TestContext { get; set; }
[TestInitialize]
public void TInit()
{
AssertTestContextData();
}
[TestMethod]
public void Test()
{
AssertTestContextData();
}
[TestCleanup]
public void TCleanup()
{
AssertTestContextData();
}
private void AssertTestContextData()
{
Assert.IsNull(TestContext.TestData);
}
}
[TestClass]
public class TestContextDataFromDataRow
{
public TestContext TestContext { get; set; }
[TestInitialize]
public void TInit()
{
AssertTestContextData();
}
[TestMethod]
[DataRow(1, "ok")]
public void Test(int i, string s)
{
AssertTestContextData();
}
[TestCleanup]
public void TCleanup()
{
AssertTestContextData();
}
private void AssertTestContextData()
{
Assert.IsNotNull(TestContext.TestData);
Assert.AreEqual(2, TestContext.TestData.Length);
Assert.AreEqual(1, TestContext.TestData[0]);
Assert.AreEqual("ok", TestContext.TestData[1]);
}
}
[TestClass]
public class TestContextDataFromDynamicData
{
public TestContext TestContext { get; set; }
[TestInitialize]
public void TInit()
{
AssertTestContextData();
}
[TestMethod]
[DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
public void Test(int i, string s)
{
AssertTestContextData();
}
[TestCleanup]
public void TCleanup()
{
AssertTestContextData();
}
private void AssertTestContextData()
{
Assert.IsNotNull(TestContext.TestData);
Assert.AreEqual(2, TestContext.TestData.Length);
Assert.AreEqual(1, TestContext.TestData[0]);
Assert.AreEqual("ok", TestContext.TestData[1]);
}
private static IEnumerable<object[]> GetData()
{
yield return new object[] { 1, "ok" };
}
}
""";
}
}