I have an MSTest unit test that runs fine without the [DataSource] attribute. However, when I add a [DataSource] attribute to read from a CSV file, MSTest fails to discover the test. Here’s the test code:
[TestMethod]
[TestCategory("BVT")]
public void GetLotsByBillingAccount()
{
string requestUrl = string.Empty;
if (!string.IsNullOrEmpty(BillingProfileId))
requestUrl = string.Format(TestConstants.URL_Lots, CCMEndpoint, BillingAccountId, BillingProfileId);
else if (!string.IsNullOrEmpty(Source) && !string.IsNullOrEmpty(Status))
requestUrl = string.Format(TestConstants.URL_LotsByBillingAccountWithFilters, CCMEndpoint, BillingAccountId, Status, Source);
else
requestUrl = string.Format(TestConstants.URL_LotsByBillingAccount, CCMEndpoint, BillingAccountId);
var result = TestHelper.ProcessRequest(requestUrl, PrincipalId, null, DefaultCertificate, TenantId, ObjectId, accessToken: GtmAccessToken, armSignedToken: GtmAccessToken);
}
When I add the [DataSource] attribute as shown below, the test is no longer discovered by MSTest:
DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
"|DataDirectory|\Data\ProdLotsInput.csv",
"ProdLotsInput#csv",
DataAccessMethod.Sequential)]
Below is the sdk style project file ( recently I migrated it from old project syle)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<TargetFramework>net472</TargetFramework>
<FileAlignment>512</FileAlignment>
<BinariesBuildTypeArchDirectory>$(REPOROOT)out$(BuildType)-$(BuildArchitecture)</BinariesBuildTypeArchDirectory>
<OutputPath>$(BinariesBuildTypeArchDirectory)$(AssemblyName)</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory"/>
<PackageReference Include="Newtonsoft.Json"/>
<PackageReference Include="MSTest"/>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<None Include="DataProdLotsInput.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Here are the steps I’ve already tried:
Ensured that the CSV file is in the correct location (|DataDirectory|DataProdLotsInput.csv).
Verified that the CSV file’s Build Action is set to Content and Copy to Output Directory is set to Copy if newer.
Tried using a fully qualified path for the CSV file instead of using |DataDirectory|.
Added the TestContext property to the test class:
-
Ensured that the CSV file is in the correct location
(|DataDirectory|DataProdLotsInput.csv). -
Verified that the CSV file’s Build Action is set to Content and Copy
to Output Directory is set to Copy if newer. -
Tried using a fully qualified path for the CSV file instead of using
|DataDirectory|. -
Added the TestContext property to the test class.
However, none of these attempts have resolved the issue. What could be preventing MSTest from discovering the test when the [DataSource] attribute is added? Any help or suggestions would be appreciated!