-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix DataSourceAttribute not being discovered with TestDataSourceDisco…
…veryOption.DuringDiscovery
- Loading branch information
1 parent
be23aac
commit 998f1e0
Showing
2 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net472</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<OutputType>Exe</OutputType> | ||
<LangVersion>preview</LangVersion> | ||
<EnableMSTestRunner>true</EnableMSTestRunner> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$MicrosoftNETTestSdkVersion$" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" /> | ||
|
||
<None Include="TestData.csv"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> | ||
|
||
#file App.config | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<configSections> | ||
<section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions"/> | ||
</configSections> | ||
<connectionStrings> | ||
<add name="ConnString" connectionString="TestData.csv" providerName="Microsoft.VisualStudio.TestTools.DataSource.CSV"/> | ||
</connectionStrings> | ||
<microsoft.visualstudio.testtools> | ||
<dataSources> | ||
<add name="TestData" connectionString="ConnString" dataTableName="TestData#csv" dataAccessMethod="Sequential"/> | ||
</dataSources> | ||
</microsoft.visualstudio.testtools> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/> | ||
</startup> | ||
</configuration> | ||
|
||
|
||
#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); | ||
} | ||
} |