0

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!

3
  • What does the "Tests" output panel show in Visual Studio: Output panel --> choose "Tests" in the "show output from" drop down. Commented Sep 23, 2024 at 15:36
  • @GregBurghardt 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 Commented Sep 23, 2024 at 16:09
  • Please edit your question to include this in your post. It is too difficult to read in a comment. Commented Sep 23, 2024 at 17:26

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.