使用 Xunit 运行 Specflow,但给出错误 nunit.framework



我遇到了一个奇怪的问题,到处都用谷歌搜索,它指出我已经做了什么,

我有一个简单的规范流来测试登录,但是在浏览器打开后,我收到此错误。

Test Name:  Check if Login is working
Test FullName:  SpecFlow.GeneratedTests.UITest.Smoke.Features.LoginFeature.CheckIfLoginIsWorking
Test Source:    C:**Login.feature : line 7
Test Outcome:   Failed
Test Duration:  0:00:02,676
Result StackTrace:  
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at TechTalk.SpecFlow.UnitTestProvider.UnitTestRuntimeProviderHelper.GetAssertMethodWithFormattedMessage(String assemblyName, String typeName, String methodName)
at TechTalk.SpecFlow.UnitTestProvider.NUnitRuntimeProvider.TestInconclusive(String message)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
at SpecFlow.GeneratedTests.UITest.Smoke.Features.LoginFeature.ScenarioCleanup()
at SpecFlow.GeneratedTests.UITest.Smoke.Features.LoginFeature.CheckIfLoginIsWorking() in C:**Login.feature:line 17
Result Message: Could not load file or assembly 'nunit.framework' or one of its dependencies. The system cannot find the file specified.

我还app.config文件更改为

<?xml version="1.0" encoding="utf-8"?>
<configuration>,
<appSettings>
<add key="xunit.diagnosticMessages" value="true"/>
</appSettings>
<configSections>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
</configSections>
<specFlow>
<unitTestProvider name="xUnit"/>
<!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
<stepAssemblies>
<stepAssembly assembly="SpecFlow.Assist.Dynamic" />
</stepAssemblies>
</specFlow>
</configuration>

奇怪的是,如果我运行正常的硒测试(不是规范流),它直接工作。

这也是生成的特征文件的一部分

using TechTalk.SpecFlow;

[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "2.1.0.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public partial class LoginFeature : Xunit.IClassFixture<LoginFeature.FixtureData>, System.IDisposable
{
private static TechTalk.SpecFlow.ITestRunner testRunner;
#line 1 "Login.feature"
#line hidden
public LoginFeature()
{
this.TestInitialize();
}
public static void FeatureSetup()
{
testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Login", "Check if Login functionality is workingrnas expected and see if correct username " +
"is poping up", ProgrammingLanguage.CSharp, ((string[])(null)));
testRunner.OnFeatureStart(featureInfo);
}
public static void FeatureTearDown()
{
testRunner.OnFeatureEnd();
testRunner = null;
}
public virtual void TestInitialize()
{
}
public virtual void ScenarioTearDown()
{
testRunner.OnScenarioEnd();
}
public virtual void ScenarioSetup(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
{
testRunner.OnScenarioStart(scenarioInfo);
}
public virtual void ScenarioCleanup()
{
testRunner.CollectScenarioErrors();
}
public virtual void SetFixture(LoginFeature.FixtureData fixtureData)
{
}
void System.IDisposable.Dispose()
{
this.ScenarioTearDown();
}

知道我能错过什么吗?

提前谢谢。

这是正常行为,因为如果您安装 nuget 包Specflow它将基于 nunit 安装 所以你必须install nuget xunit for specflow

Install-Package SpecFlow.xUnit -Version 2.1.0

更新如果您已完成此操作,请尝试添加 NUNIT 包

我之前遇到过这个问题,但发现我的问题是步骤中的特征文本与签名不匹配,例如

专题文字:Given I have a widget id "123"

步骤签名:Given I have a widgetid "123"

确保所有功能文本和步骤签名匹配为我解决了问题。

我不知道为什么错误是关于缺少的 nunit 依赖项,因为我使用的是 xunit。

所以,我发生了这件事。 事实证明,这是自己造成的伤害。

问题是,在构建后事件中,我正在将配置文件从被测应用程序复制到测试项目的配置文件。 在应用程序的构建后事件中,我有这一行,它将配置复制到 Tests 文件夹。

copy "$(ProjectDir)"\App.config "$(SolutionDir)"\ReleaseImagesWreckersTests\App.Config

从受测应用程序复制的配置文件中没有指示 SpecFlow 使用 xUnit 的行。 复制发生在构建 SpecFlow 测试之前。 最终,我想通了,并将这些行添加到我的 app.config 中,用于正在测试的应用程序(程序集):

<specFlow>
<unitTestProvider name="xUnit" />
</specFlow></configuration>

这样做之后,一切正常。 受测应用成功构建,然后在测试程序集的配置顶部复制其配置(没有 specFlow 部分),该配置在被复制之前具有适当的 specFlow 部分。

相关内容

最新更新