Repository navigation
Expand file tree
/
Copy pathDesktopTestDataSourceTests.cs
More file actions
89 lines (66 loc) · 3.24 KB
/
Copy pathDesktopTestDataSourceTests.cs
File metadata and controls
89 lines (66 loc) · 3.24 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if NETFRAMEWORK
using System.Data;
using AwesomeAssertions;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Moq;
using TestFramework.ForTestingMSTest;
using ITestMethod = Microsoft.VisualStudio.TestTools.UnitTesting.ITestMethod;
namespace MSTestAdapter.PlatformServices.UnitTests.Services;
public class DesktopTestDataSourceTests : TestContainer
{
private readonly Mock<ITestMethod> _mockTestMethodInfo;
private readonly Mock<ITestContext> _mockTestContext;
public DesktopTestDataSourceTests()
{
_mockTestMethodInfo = new Mock<ITestMethod>();
_mockTestContext = new Mock<ITestContext>();
}
public void GetDataShouldReadDataFromGivenDataSource()
{
System.Reflection.MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PassingTest");
DataSourceAttribute dataSourceAttribute = new(
"Microsoft.VisualStudio.TestTools.DataSource.XML", Path.Combine(AppContext.BaseDirectory, "DataTestSourceFile.xml"), "settings", DataAccessMethod.Sequential);
_mockTestMethodInfo.Setup(ds => ds.GetAttributes<DataSourceAttribute>())
.Returns([dataSourceAttribute]);
_mockTestMethodInfo.Setup(ds => ds.MethodInfo).Returns(methodInfo);
TestDataSource testDataSource = new();
IEnumerable<object>? dataRows = testDataSource.GetData(_mockTestMethodInfo.Object, _mockTestContext.Object);
dataRows.Should().HaveCount(4);
foreach (DataRow dataRow in dataRows.Cast<DataRow>())
{
dataRow[3].Should().Be("v1");
}
}
public void GetDataShouldSetDataConnectionInTestContextObject()
{
System.Reflection.MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PassingTest");
DataSourceAttribute dataSourceAttribute = new(
"Microsoft.VisualStudio.TestTools.DataSource.XML", Path.Combine(AppContext.BaseDirectory, "DataTestSourceFile.xml"), "settings", DataAccessMethod.Sequential);
_mockTestMethodInfo.Setup(ds => ds.GetAttributes<DataSourceAttribute>())
.Returns([dataSourceAttribute]);
_mockTestMethodInfo.Setup(ds => ds.MethodInfo).Returns(methodInfo);
TestDataSource testDataSource = new();
IEnumerable<object>? dataRows = testDataSource.GetData(_mockTestMethodInfo.Object, _mockTestContext.Object);
_mockTestContext.Verify(tc => tc.SetDataConnection(It.IsAny<object>()), Times.Once);
}
#region Dummy implementation
public class DummyTestClass
{
public TestContext TestContext { get; set; } = null!;
[TestMethod]
public void PassingTest()
{
TestContext.DataRow!["adapter"].ToString().Should().Be("v1");
TestContext.DataRow["targetPlatform"].ToString().Should().Be("x86");
TestContext.AddResultFile("C:\\temp.txt");
}
[TestMethod]
public void FailingTest()
=> TestContext.DataRow!["configuration"].ToString().Should().Be("Release");
}
#endregion
}
#endif