[Specflow] Specflow 3 在 .NET Core 3 的開發設定

.NET Core 3 已經釋出一段時間了,昨天趁機追了一下進度,沒想到在 Specflow 3 卡關了一下關

開發環境

.NET Core 3

Specflow 3 form nuget

Install-Package SpecFlow.MsTest

它會一併安裝以下兩個相依套件

Install-Package SpecFlow
Install-Package SpecFlow.Tools.MsBuild.Generation

 

安裝完成後新增一個 Feature 檔

定義如下

Feature: SpecFlowFeature1
	In order to avoid silly mistakes
	As a math idiot
	I want to be told the sum of two numbers

@mytag
Scenario: Add two numbers
	Given I have entered 50 into the calculator
	And I have also entered 70 into the calculator
	When I press add
	Then the result should be 120 on the screen

 

測試綁定的步驟如下

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechTalk.SpecFlow;
 
namespace UnitTestProject1
{
    [Binding]
    public class SpecFlowFeature1Steps:Steps
    {
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int firstNumber)
        {
            this.ScenarioContext.Set(firstNumber, "firstNumber");
        }
        
        [Given(@"I have also entered (.*) into the calculator")]
        public void GivenIHaveAlsoEnteredIntoTheCalculator(int secondNumber)
        {
            this.ScenarioContext.Set(secondNumber, "secondNumber");
        }
        
        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            var firstNumber  = this.ScenarioContext.Get<int>("firstNumber");
            var secondNumber = this.ScenarioContext.Get<int>("secondNumber");
            var calculation  = new Calculation();
            var actual       = calculation.Add(firstNumber, secondNumber);
            this.ScenarioContext.Set(actual, "actual");
        }
        
        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int expected)
        {
            var actual = this.ScenarioContext.Get<int>("actual");
            Assert.AreEqual(expected, actual);
        }
    }
}

 

一切都跟以前一樣,沒想到一運行就得到這錯誤,翻遍官方文件,卻找不到相關的說明

Test Name: AddTwoNumbers
Test FullName: UnitTestProject1.UnitTestProject1.SpecFlowFeature1Feature.AddTwoNumbers
Test Source: C:\Users\yao\Desktop\WebApplication1\UnitTestProject1\SpecFlowFeature1.feature : line 7
Test Outcome: Failed
Test Duration: 0:00:00
Test Name: AddTwoNumbers
Test Outcome: Failed
Result StackTrace: 
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 UnitTestProject1.SpecFlowFeature1Feature.FeatureSetup(TestContext testContext)
Result Message: Class Initialization method UnitTestProject1.SpecFlowFeature1Feature.FeatureSetup threw exception. System.ArgumentNullException: System.ArgumentNullException: Value cannot be null. (Parameter 'key').
Result StandardOutput: 
-> Loading plugin C:\Users\yao\Desktop\WebApplication1\UnitTestProject1\bin\Debug\netcoreapp3.0\TechTalk.SpecFlow.MSTest.SpecFlowPlugin.dll
-> Using default config
-> error: Value cannot be null. (Parameter 'key')

 

Specflow 的版本是 3.0.25

最後找到這篇 

https://stackoverflow.com/questions/58736715/specflow-teardown-failed-for-test-fixture-system-argumentnullexception-val

把 Speclfow 3 升到預覽版 3.1.52 就可以了

綠燈

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo