From 998f1e0a51166a38e235d20a80f0cf8f945e040d Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Wed, 13 Nov 2024 06:03:02 +0100 Subject: [PATCH] Fix DataSourceAttribute not being discovered with TestDataSourceDiscoveryOption.DuringDiscovery --- .../Discovery/AssemblyEnumerator.cs | 4 +- .../DataSourceTests.cs | 111 ++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs diff --git a/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs b/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs index c820ee0e9c..5dcae8fb17 100644 --- a/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs +++ b/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs @@ -403,8 +403,10 @@ private static bool TryProcessTestDataSourceTests(UnitTestElement test, TestMeth private static bool ProcessTestDataSourceTests(UnitTestElement test, ReflectionTestMethodInfo methodInfo, IEnumerable testDataSources, List tests) { + bool hasAtLeastOneITestDataSource = false; foreach (ITestDataSource dataSource in testDataSources) { + hasAtLeastOneITestDataSource = true; IEnumerable? data; // This code is to discover tests. To run the tests code is in TestMethodRunner.ExecuteDataSourceBasedTests. @@ -476,6 +478,6 @@ private static bool ProcessTestDataSourceTests(UnitTestElement test, ReflectionT tests.AddRange(discoveredTests); } - return true; + return hasAtLeastOneITestDataSource; } } diff --git a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs new file mode 100644 index 0000000000..e4ca8aba2b --- /dev/null +++ b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs @@ -0,0 +1,111 @@ +// 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; +using Microsoft.Testing.Platform.Helpers; + +namespace MSTest.Acceptance.IntegrationTests; + +[TestGroup] +public class DataSourceTests : AcceptanceTestBase +{ + private const string SourceCode = """ +#file DataSourceTests.csproj + + + net472 + enable + Exe + preview + true + + + + + + + + PreserveNewest + + + + +#file App.config + + + +
+ + + + + + + + + + + + + + + +#file MyTestClass.cs +using Microsoft.VisualStudio.TestTools.UnitTesting; + +[TestClass] +public class MyTestClass +{ + public TestContext TestContext { get; set; } + + [DataTestMethod] + [DataSource("TestData")] + public void TestSum() + { + int expected = (int)TestContext.DataRow["expectedSum"]; + int num1 = (int)TestContext.DataRow["num1"]; + int num2 = (int)TestContext.DataRow["num2"]; + Assert.AreEqual(expected, num1 + num2); + } + + [TestMethod] + public void MyTest() + { + } +} + +#file TestData.csv +num1,num2,expectedSum +1,1,2 +5,6,11 +10,30,40 +1,1,1 +"""; + + private readonly AcceptanceFixture _acceptanceFixture; + + public DataSourceTests(ITestExecutionContext testExecutionContext, AcceptanceFixture acceptanceFixture) + : base(testExecutionContext) => _acceptanceFixture = acceptanceFixture; + + public async Task TestDataSourceFromAppConfig() + { + using TestAsset generator = await TestAsset.GenerateAssetAsync( + "DataSourceTests", + SourceCode + .PatchCodeWithReplace("$MSTestVersion$", MSTestVersion) + .PatchCodeWithReplace("$MicrosoftNETTestSdkVersion$", MicrosoftNETTestSdkVersion), + addPublicFeeds: true); + + await DotnetCli.RunAsync( + $"build {generator.TargetAssetPath} -c Release", + _acceptanceFixture.NuGetGlobalPackagesFolder.Path, + retryCount: 0); + + var testHost = TestHost.LocateFrom(generator.TargetAssetPath, "DataSourceTests", "net472"); + + TestHostResult result = await testHost.ExecuteAsync(); + result.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed); + result.AssertOutputContainsSummary(failed: 1, passed: 4, skipped: 0); + } +}