求助:将Specflow+Runner的配置文件转换适配Specflow+xUnit
从Specflow+Runner迁移到Specflow+xUnit的配置转换方案
问题描述
我正尝试把现有测试从Specflow+Runner迁移到Specflow+xUnit,但不知道怎么把现有的.runsettings和.srprofile文件转换成适配xUnit的配置,有没有人做过这个转换或者能提供帮助?
现有.runsettings文件:
<?xml version="1.0" encoding="utf-8"?> <RunSettings> <!-- Configurations for SpecRun --> <SpecRun> <Profile>DEV.srprofile</Profile> <!-- SpecRun uses VisualStudo.srprofile or TFS.srprofile by default, or the Default.srprofile if these don't exist. <Profile>MyProfile.srprofile</Profile> --> <!-- SpecRun generates a report file based on the project name and the current timestamp by default. <ReportFile>CustomReport.html</ReportFile>--> <GenerateSpecRunTrait>false</GenerateSpecRunTrait> <!-- Set this to "true" to generate a "SpecRun" trait for all tests discovered by SpecRun (useful when the solution contains other tests too) --> <GenerateFeatureTrait>false</GenerateFeatureTrait> <!-- Set this to "true" to generate a feature trait for all scenarios (othervise the feature goruping can be used as "class") --> </SpecRun> </RunSettings>
现有.srprofile文件:
<?xml version="1.0" encoding="utf-8"?> <TestProfile xmlns="http://www.specflow.org/schemas/plus/TestProfile/1.5"> <Settings projectName="example" /> <Execution stopAfterFailures="3" testThreadCount="1" testSchedulingMode="Sequential" /> <!-- For collecting by a SpecRun server update and enable the following element. For using the collected statistics, set testSchedulingMode="Adaptive" attribute on the <Execution> element. <Server serverUrl="http://specrunserver:6365" publishResults="true" /> --> <TestAssemblyPaths> <TestAssemblyPath>example.dll</TestAssemblyPath> </TestAssemblyPaths> <DeploymentTransformation> <Steps> <ConfigFileTransformation configFile="App.config"> <Transformation> <![CDATA[<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="baseUrl" value="<sample URL>" xdt:Locator="Match(key)" xdt:Transform="SetAttributes" /> <add key="shortWait" value="15" xdt:Locator="Match(key)" xdt:Transform="SetAttributes" /> <add key="longWait" value="25" xdt:Locator="Match(key)" xdt:Transform="SetAttributes" /> </appSettings> </configuration> ]]> </Transformation> </ConfigFileTransformation> </Steps> </DeploymentTransformation> </TestProfile>
配置转换步骤
1. 转换.runsettings文件
Specflow+xUnit不再需要<SpecRun>节点,你可以根据需求保留或添加xUnit和Specflow的相关配置:
<?xml version="1.0" encoding="utf-8"?> <RunSettings> <!-- Specflow 配置 --> <SpecFlow> <!-- 生成报告(替代原来的SpecRun报告) --> <Report> <Output>CustomReport.html</Output> <Format>Html</Format> </Report> <!-- xUnit会自动将Feature作为测试类分组,无需额外配置生成Feature Trait --> </SpecFlow> <!-- xUnit 执行配置 --> <xUnit> <!-- 对应srprofile里的testThreadCount="1",强制单线程执行 --> <MaxParallelThreads>1</MaxParallelThreads> <!-- 禁用并行测试,确保顺序执行(对应testSchedulingMode="Sequential") --> <DisableParallelization>true</DisableParallelization> </xUnit> </RunSettings>
2. 迁移.srprofile中的核心配置
(1)执行控制配置
stopAfterFailures="3":xUnit没有内置的失败终止功能,可通过Specflow钩子实现:[Binding] public class FailureHooks { private static int _failureCount = 0; private readonly ITestExecutionEngine _testExecutionEngine; public FailureHooks(ITestExecutionEngine testExecutionEngine) { _testExecutionEngine = testExecutionEngine; } [AfterScenario(Order = 100)] public void CheckFailureCount() { if (ScenarioContext.Current.TestError != null) { _failureCount++; if (_failureCount >= 3) { _testExecutionEngine.Stop(); } } } }testThreadCount="1"和testSchedulingMode="Sequential":已在上述.runsettings的<xUnit>节点中配置完成。
(2)App.config转换
原来的DeploymentTransformation中的XDT转换,可改用MSBuild内置的XDT转换功能:
- 在项目文件(
.csproj)中添加配置:<ItemGroup> <None Include="App.config"> <TransformOnBuild>true</TransformOnBuild> </None> <None Include="App.DEV.config"> <DependentUpon>App.config</DependentUpon> <IsTransformFile>true</IsTransformFile> </None> </ItemGroup> - 创建
App.DEV.config文件,内容为原srprofile中的转换代码:<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="baseUrl" value="<sample URL>" xdt:Locator="Match(key)" xdt:Transform="SetAttributes" /> <add key="shortWait" value="15" xdt:Locator="Match(key)" xdt:Transform="SetAttributes" /> <add key="longWait" value="25" xdt:Locator="Match(key)" xdt:Transform="SetAttributes" /> </appSettings> </configuration> - 构建项目时,MSBuild会自动将
App.config转换为适配DEV环境的配置文件。
(3)测试程序集指定
xUnit会自动发现项目中的测试程序集,无需在配置文件中指定。如果需要在CI环境中运行特定程序集,可直接用xUnit命令行工具指定:
dotnet xunit -f net6.0 -a .\bin\Debug\net6.0\example.dll
内容的提问来源于stack exchange,提问作者Rishabh Thakur
相关产品推荐
相关产品推荐

