-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathDataSourceTests.cs
More file actions
110 lines (94 loc) · 3.65 KB
/
DataSourceTests.cs
File metadata and controls
110 lines (94 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 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;
[TestClass]
public sealed class DataSourceTests : AcceptanceTestBase<NopAssetFixture>
{
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, MSTest.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
""";
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
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",
retryCount: 0,
cancellationToken: TestContext.CancellationToken);
var testHost = TestHost.LocateFrom(generator.TargetAssetPath, "DataSourceTests", "net472");
TestHostResult result = await testHost.ExecuteAsync(cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed);
result.AssertOutputContainsSummary(failed: 1, passed: 4, skipped: 0);
}
public TestContext TestContext { get; set; }
}