【问题标题】:Run Specflow with Xunit but gives error nunit.framework使用 Xunit 运行 Specflow 但给出错误 nunit.framework
【发布时间】:2017-08-17 09:02:07
【问题描述】:

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

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

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();
        }

任何想法我可能会错过什么?

提前致谢。

【问题讨论】:

  • 在配置中更改 unitTestProvider 后,您必须重新生成所有代码隐藏文件。它们可能包含引用 NUnit 类的静态代码。你已经这样做了吗?

标签: c# selenium specflow xunit


【解决方案1】:

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

Install-Package SpecFlow.xUnit -Version 2.1.0

更新 如果你已经这样做了,请尝试添加 nunit 包

【讨论】:

    【解决方案2】:

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

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

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

    确保所有特征文本和步骤签名匹配解决了我的问题。

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

    【讨论】:

      【解决方案3】:

      所以,这件事发生在我身上。事实证明,这是一个自残的伤口。

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2017-04-11
        • 1970-01-01
        • 1970-01-01
        • 2016-07-21
        • 1970-01-01
        • 2019-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多