【问题标题】:Specflow - TearDown failed for test fixture - System.ArgumentNullException : Value cannot be null. (Parameter 'key')Specflow - 测试夹具的 TearDown 失败 - System.ArgumentNullException:值不能为空。 (参数“键”)
【发布时间】:2019-11-06 18:48:45
【问题描述】:

我正在使用 SpecflowSelenium 进行测试自动化,但是当我尝试执行测试时,我遇到了以下错误消息:

  • 我的问题不是什么是 NullReferenceException,而是这个 Parameter 'key' 指出的异常原因是什么(以及在哪里)。

测试夹具 MyProject.Features.MyFeature 的 TearDown 失败 System.ArgumentNullException :值不能为空。 (参数“键”) TearDown : System.NullReferenceException : 对象引用未设置为对象的实例。

还有:

错误信息: OneTimeSetUp:System.ArgumentNullException:值不能为空。 (参数'key')

这是我的场景:

Scenario: Accessing the screen for the first time
    Given I accessed the screen for the first time
    Then the result grid should only show the phrase "Lorem ipsum"

这是我的 StepDefinition.cs 文件:

    [Binding]
    public class StepDefinition
    {
        PageObject pageObject = new PageObject();

        [Given(@"I accessed the screen for the first time")]
        public void GivenIAccessedTheScreenForTheFirstTime()
        {
            pageObject.NavigateToScreen();
        }

        [Then(@"the result grid should only show the phrase (.*)")]
        public void ThenTheResultGridShouldOnlyShowThePhrase(string phrase)
        {
            Assert.True(pageObject.isPhraseDisplayed(phrase));
        }        
    }

这是我的 PageObject.cs 文件:

        public PageObject() : base(){ } //I use a BasePageObject.cs file also

        public void NavigateToScreen()
        {
            driver.Navigate().GoToUrl(_urlHere_);
        }
        public bool isPhraseDisplayed(string phrase)
        {
            wait.Until(u => u.FindElement(_byIdOfWebElementHere_).Displayed);

            IWebElement element = driver.FindElement(_byIdOfWebElementHere_);
            return element.Displayed;
        }

这是BasePageObject.cs文件,它实现了IDisposable接口:

        protected IWebDriver driver { get; set; }
        protected WebDriverWait wait { get; set; }

        public BasePageObject()
        {
            driver = new ChromeDriver();
        }

        [AfterScenario]
        public void Dispose()
        {
            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
    }

异常堆栈跟踪:

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
TearDown failed for test fixture MyProject.Features.MyFeature
System.ArgumentNullException : Value cannot be null. (Parameter 'key')
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.FindAttributeConstructorArg(ParameterInfo parameterInfo, Dictionary`2 namedAttributeValues)
   at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.<>c__DisplayClass8_0.<CreateAttribute>b__7(ParameterInfo p)
   at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.CreateAttribute(Attribute attribute)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.GetAttributes(IEnumerable`1 customAttributes)
   at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.CreateBindingSourceMethod(MethodInfo methodDefinition)
   at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.BuildBindingsFromType(Type type)
   at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.BuildBindingsFromAssembly(Assembly assembly)
   at TechTalk.SpecFlow.TestRunnerManager.BuildBindingRegistry(IEnumerable`1 bindingAssemblies)
   at TechTalk.SpecFlow.TestRunnerManager.InitializeBindingRegistry(ITestRunner testRunner)
   at TechTalk.SpecFlow.TestRunnerManager.CreateTestRunner(Int32 threadId)
   at TechTalk.SpecFlow.TestRunnerManager.GetTestRunnerWithoutExceptionHandling(Int32 threadId)
   at TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(Int32 threadId)
   at TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(Assembly testAssembly, Nullable`1 managedThreadId)
   at MyProject.Features.MyFeature.FeatureSetup()
--TearDown
   at MyProject.Features.MyFeature.FeatureTearDown()
  X AcessandoATelaDeConsultaPelaPrimeiraVez [< 1ms]
  Error Message:
   OneTimeSetUp: System.ArgumentNullException : Value cannot be null. (Parameter 'key')

Results File: C:\Users\FAMG\AppData\Local\Temp\test-explorer-GvcYR7\0.trx

Total tests: 1
     Failed: 1
 Total time: 3,7733 Seconds

我认为需要指出的重点:

  • 我使用的是 VSCode,而不是 Visual Studio。
  • 我在项目中安装的测试运行器是 NUnit。

【问题讨论】:

  • 谢谢,但这不是我的问题,对不起。我编辑了帖子并重新编写了它。
  • 你能发布异常的堆栈跟踪吗?否则我认为我们也无法分辨。
  • 当然!刚刚在问题上添加了它

标签: c# visual-studio-code automated-tests nunit specflow


【解决方案1】:

我刚刚跑到这个确切的地方。
错误的根源来自对类型的反射:System.Runtime.CompilerServices.NullableContextAttribute

看起来某种预期的构造函数 MethodInfo Name 为 NULL。

长话短说:.NET Core 2.1、2.2 工作。 SpecFlow (3.0.225) 似乎不适用于 .Net Core 3.0。

好消息:开启包含 Nuget 的预发布,并将 beta SpecFlow 和相关软件包(Nunit 和朋友)更新到(3.1.52-beta)最新作品。

SpecFlow 3.1 主版本可能会支持 .Net Core 3.0。

【讨论】:

  • 所以我应该在我的 PC 上安装 .NET Core 2.1 或 2.2 并更改我的 .csproj 文件上的 &lt;TargetFramework&gt; 属性?
  • 是的,我已经这样做了,这个异常停止发生了。谢谢! :)
  • SpecFlow 3.1 将支持 .NET Core 3.0。由于可以为空的引用类型,方法上的新属性会发生错误。
猜你喜欢
  • 2021-10-11
  • 2019-01-02
  • 1970-01-01
  • 2019-08-17
  • 2021-07-02
  • 2018-06-03
  • 2010-11-11
  • 2020-03-27
  • 1970-01-01
相关资源
最近更新 更多