You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加[DataSource]属性后MSTest无法发现CSV数据源测试求助

添加[DataSource]属性后MSTest无法发现单元测试

我有一个MSTest单元测试,未添加[DataSource]属性时运行正常,但添加该属性以读取CSV文件后,MSTest无法发现该测试。

添加[DataSource]前的测试代码

[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);
}

添加的[DataSource]属性

添加以下属性后测试不再被发现:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", 
            @"|DataDirectory|\Data\ProdLotsInput.csv", 
            "ProdLotsInput#csv", 
            DataAccessMethod.Sequential)]

SDK风格项目文件(已从旧项目风格迁移)

<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>

测试输出日志(视图->输出->测试)

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

已尝试的解决步骤

  • 确认CSV文件路径正确(|DataDirectory|\Data\ProdLotsInput.csv)
  • 验证CSV文件的生成操作设置为Content,复制到输出目录设置为“较新则复制”
  • 尝试用CSV文件的绝对路径替代|DataDirectory|
  • 向测试类添加TestContext属性

以上尝试均未解决问题,请问添加[DataSource]属性后,是什么原因导致MSTest无法发现测试?恳请提供帮助或建议!

内容的提问来源于stack exchange,提问作者Pulkit Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 06:22:33