// 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 _mockTestMethodInfo; private readonly Mock _mockTestContext; public DesktopTestDataSourceTests() { _mockTestMethodInfo = new Mock(); _mockTestContext = new Mock(); } 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()) .Returns([dataSourceAttribute]); _mockTestMethodInfo.Setup(ds => ds.MethodInfo).Returns(methodInfo); TestDataSource testDataSource = new(); IEnumerable? dataRows = testDataSource.GetData(_mockTestMethodInfo.Object, _mockTestContext.Object); dataRows.Should().HaveCount(4); foreach (DataRow dataRow in dataRows.Cast()) { 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()) .Returns([dataSourceAttribute]); _mockTestMethodInfo.Setup(ds => ds.MethodInfo).Returns(methodInfo); TestDataSource testDataSource = new(); IEnumerable? dataRows = testDataSource.GetData(_mockTestMethodInfo.Object, _mockTestContext.Object); _mockTestContext.Verify(tc => tc.SetDataConnection(It.IsAny()), 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