.NET Framework 4.7.2项目如何指定TestExecution.json输出路径?
如何让.NET Framework 4.x项目的SpecFlow TestExecution.json生成在构建产物目录
问题场景
使用SpecFlow活文档命令行工具时,.NET Framework 4.7.2项目的TestExecution.json会生成在随机临时文件夹(示例路径:C:\Users\c338129\AppData\Local\Temp\...\TestExecution.json),且测试结束几秒后文件就会被自动删除。而切换到.NET 6框架时,文件能正常生成在构建产物目录(如bin\Debug\net5\TestExecution.json)。项目暂无法升级框架,需调整配置让文件固定生成到构建产物目录。
解决方案
通过在项目文件中添加SpecFlow插件的输出路径属性,强制指定TestExecution.json的生成位置。
修改项目文件(.csproj)
在项目的<PropertyGroup>节点中添加SpecFlowPlusLivingDocPlugin_OutputPath属性,使用MSBuild内置变量$(OutputPath)指向构建产物目录:
<SpecFlowPlusLivingDocPlugin_OutputPath>$(OutputPath)</SpecFlowPlusLivingDocPlugin_OutputPath>
如果需要自定义路径,也可以指定相对或绝对路径,例如:
<SpecFlowPlusLivingDocPlugin_OutputPath>$(SolutionDir)\TestResults\SpecFlow\</SpecFlowPlusLivingDocPlugin_OutputPath>
修改后的完整项目文件示例:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net472</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <LangVersion>preview</LangVersion> <!-- 指定TestExecution.json输出到构建产物目录 --> <SpecFlowPlusLivingDocPlugin_OutputPath>$(OutputPath)</SpecFlowPlusLivingDocPlugin_OutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" /> <PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.57" /> <PackageReference Include="SpecFlow.xUnit" Version="3.9.74" /> <PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"/> <PackageReference Include="FluentAssertions" Version="6.8.0" /> </ItemGroup> <ItemGroup> <Folder Include="Drivers\" /> <Folder Include="Support\" /> </ItemGroup> </Project>
验证步骤
- 重新构建项目并运行测试
- 进入项目的构建产物目录(如
bin\Debug\net472\),确认TestExecution.json已生成且不会被自动删除
原因说明
.NET Framework项目中,测试运行器默认会在临时目录执行测试,SpecFlow插件默认在当前工作目录生成TestExecution.json,测试结束后临时目录会被系统清理,导致文件消失。通过显式指定输出路径到持久化的构建产物目录,即可避免文件被删除。
内容的提问来源于stack exchange,提问作者Luke Hammer
相关产品推荐
相关产品推荐

