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

如何在Visual Studio 2022中通过EXE项目运行所有单元测试DLL?

实现思路:用EXE运行多个Visual Studio原生单元测试DLL

方案一:直接调用Visual Studio测试平台API

  • 新建一个控制台应用项目(目标框架与你的测试项目保持一致),作为执行测试的EXE载体。
  • 给控制台项目添加对应NuGet包:
    • .NET Framework项目:安装Microsoft.VisualStudio.TestPlatform.ObjectModel、Microsoft.VisualStudio.TestPlatform.TestFramework
    • .NET Core/.NET 5+项目:安装Microsoft.NET.Test.Sdk、MSTest.TestAdapter、MSTest.TestFramework、Microsoft.VisualStudio.TestPlatform.Client
  • 编写核心执行代码:
    1. 指定所有测试DLL的路径(可硬编码或通过命令行参数传入)
    2. 调用测试平台API完成测试发现与执行
      示例代码片段:
    using Microsoft.VisualStudio.TestPlatform.Client;
    using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
    
    // 测试DLL路径集合
    var testDllPaths = new List<string> 
    { 
        @"..\..\..\TestProject1\bin\Debug\net6.0\TestProject1.dll",
        @"..\..\..\TestProject2\bin\Debug\net6.0\TestProject2.dll"
    };
    
    // 初始化测试平台
    var testPlatform = TestPlatformFactory.CreateTestPlatform();
    
    // 发现测试用例
    var discoveryConfig = new DiscoveryConfiguration();
    var discoveryRequest = testPlatform.CreateDiscoveryRequest(discoveryConfig);
    discoveryRequest.DiscoveryComplete += (_, e) =>
    {
        foreach (var testCase in e.TestCases)
        {
            Console.WriteLine($"找到测试:{testCase.DisplayName}");
        }
    };
    discoveryRequest.Execute(testDllPaths, null);
    
    // 执行测试
    var runConfig = new RunConfiguration();
    var runRequest = testPlatform.CreateTestRunRequest(runConfig);
    runRequest.TestRunComplete += (_, e) =>
    {
        Console.WriteLine($"\n测试执行结果:");
        Console.WriteLine($"总用例数:{e.TestRunStatistics.Total}");
        Console.WriteLine($"通过:{e.TestRunStatistics.Passed} | 失败:{e.TestRunStatistics.Failed} | 跳过:{e.TestRunStatistics.Skipped}");
    };
    runRequest.Execute(testDllPaths, null);
    
  • 注意:需确保测试DLL及其依赖项(如被测试项目的DLL)能被EXE加载,可将依赖项复制到EXE输出目录,或设置AppDomain私有路径。

方案二:调用官方命令行工具(更简单)

利用Visual Studio自带的vstest.console.exe(.NET Framework/.NET通用)或dotnet test(.NET Core/.NET专属),在EXE中启动进程执行命令:

  • 控制台项目中编写进程调用代码:
    using System.Diagnostics;
    
    // 以vstest.console.exe为例,路径需根据VS版本/安装类型调整
    var vstestExePath = @"C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe";
    // 多个测试DLL用空格分隔
    var testDllArgs = @"..\..\..\TestProject1\bin\Debug\net6.0\TestProject1.dll ..\..\..\TestProject2\bin\Debug\net6.0\TestProject2.dll";
    
    var startInfo = new ProcessStartInfo(vstestExePath, testDllArgs)
    {
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false,
        CreateNoWindow = false // 设为true可隐藏命令行窗口
    };
    
    using var process = Process.Start(startInfo);
    process.WaitForExit();
    
    // 输出测试结果
    Console.WriteLine(process.StandardOutput.ReadToEnd());
    var errorMsg = process.StandardError.ReadToEnd();
    if (!string.IsNullOrEmpty(errorMsg))
    {
        Console.WriteLine("\n错误信息:");
        Console.WriteLine(errorMsg);
    }
    Console.WriteLine($"\n测试执行完成,退出码:{process.ExitCode}");
    
  • 优化点:可通过读取注册表或环境变量动态获取vstest.console.exe路径,避免硬编码;若用.NET Core/.NET,可替换为调用dotnet.exe并传入test命令及DLL路径。

通用注意事项

  • 确保EXE项目的目标框架与所有测试项目完全匹配,避免加载失败。
  • 若测试项目依赖自定义配置文件(如app.config、appsettings.json),需将这些文件复制到EXE输出目录。

内容的提问来源于stack exchange,提问作者Markus F

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 06:15:33