Skip to content

Commit

Permalink
Add TestData property to the TestContext (#4015)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink authored Nov 7, 2024
1 parent 766e40e commit 34d3d66
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ private bool ExecuteDataSourceBasedTests(List<TestResult> results)
private TestResult[] ExecuteTestWithDataSource(UTF.ITestDataSource? testDataSource, object?[]? data)
{
var stopwatch = Stopwatch.StartNew();

_testMethodInfo.SetArguments(data);
_testContext.SetTestData(data);
TestResult[] testResults = ExecuteTest(_testMethodInfo);
stopwatch.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ public void SetDataRow(object? dataRow)
#endif
}

/// <inheritdoc/>
public void SetTestData(object?[]? data) => TestData = data;

/// <summary>
/// Set connection for TestContext.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
#nullable enable
Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestData.get -> object?[]?
Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestData.set -> void
2 changes: 2 additions & 0 deletions src/TestFramework/TestFramework.Extensions/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ public TestContextTests(ITestExecutionContext testExecutionContext, TestAssetFix
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)
{
Expand Down Expand Up @@ -56,6 +66,7 @@ public sealed class TestAssetFixture(AcceptanceFixture acceptanceFixture) : Test
</Project>

#file UnitTest1.cs
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand Down Expand Up @@ -126,6 +137,106 @@ public void DerivedTestMethod()
_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" };
}
}
""";
}
}

0 comments on commit 34d3d66

Please sign in to comment.