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

时间:2017-03-24 12:14:16

标签: c# selenium specflow xunit

我有一个奇怪的问题,到处搜索,并指出我已经做过的事情,

我有一个简单的specflow来测试登录,但是在浏览器打开后,我收到了这个错误。

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>

奇怪的是,如果我运行正常的硒测试(非specflow),它会直接起作用。

这也是生成的功能文件

的一部分
 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 working\r\nas 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();
        }

任何想法我可以错过什么?

提前致谢。

3 个答案:

答案 0 :(得分:2)

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

功能文字: Given I have a widget id "123"

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

确保所有功能文本和步骤签名均匹配,从而为我解决了这个问题。

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

答案 1 :(得分:1)

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

Install-Package SpecFlow.xUnit -Version 2.1.0

<强>更新 如果你这样做,试着添加nunit包

答案 2 :(得分:0)

所以,我发生了这种情况。事实证明,这是一个自我伤害的伤口。

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

复制“ $(ProjectDir)” \ App.config“ $(SolutionDir)” \ ReleaseImagesWreckersTests \ App.Config

从被测应用程序复制的配置文件中没有包含告诉SpecFlow使用xUnit的行。复制是在构建SpecFlow测试之前进行的。 最终,我找到了答案,并将这些行添加到了我的app.config中,用于测试中的应用程序(程序集):

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

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

相关问题