Pulkit Sharma
Pulkit Sharma

Reputation: 324

MSTest Not Discovering Test with [DataSource] Attribute for CSV File

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="Data\ProdLotsInput.csv">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

Output from view -> output -> test

Building Test Projects Starting test discovery No test is available in TestProject1.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again. 
========== Test discovery finished: 0 Tests found in 1.9 sec ========== ========== 
Starting test run ==========
Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms  
Building Test Projects 
========== Starting test run ========== 
Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms

Here are the steps I've already tried:

  1. Ensured that the CSV file is in the correct location (|DataDirectory|\Data\ProdLotsInput.csv).

  2. Verified that the CSV file's Build Action is set to Content and Copy to Output Directory is set to Copy if newer.

  3. Tried using a fully qualified path for the CSV file instead of using |DataDirectory|.

  4. 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!

Upvotes: 0

Views: 79

Answers (1)

Youssef13
Youssef13

Reputation: 4954

This was a bug in MSTest that was fixed very recently in https://github.com/microsoft/testfx/pull/4058.

A temporary workaround you can use is:

[assembly: TestDataSourceDiscovery(TestDataSourceDiscoveryOption.DuringExecution]

The fix will be shipped in MSTest 3.7

Upvotes: 0

Related Questions