specFlow错误:ScenarioTearDown将对象引用设置为未设置为对象的实例

时间:2014-01-28 10:17:15

标签: specflow

我在specflow场景测试中遇到的问题如下。

1.它是Tear Down方法中的Throwing exception,因为Object引用未设置为对象的实例 2.尽管测试通过成功,但是导致测试失败。

帮助。

以下是feature.cs文件

// ------------------------------------------------------------------------------
//  <auto-generated>
//      This code was generated by SpecFlow (http://www.specflow.org/).
//      SpecFlow Version:1.9.2.1
//      SpecFlow Generator Version:1.9.0.0
//      Runtime Version:4.0.30319.18052
// 
//      Changes to this file may cause incorrect behavior and will be lost if
//      the code is regenerated.
//  </auto-generated>
// ------------------------------------------------------------------------------
#region Designer generated code
#pragma warning disable
namespace FunctionalTest.SpecFlowScenarios.APIKeyFixtures
{
    using TechTalk.SpecFlow;


    [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.9.2.1")]
    [System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute()]
    public partial class CreateAPIKeyFeature
    {

        private static TechTalk.SpecFlow.ITestRunner testRunner;

#line 1 "CreateAPIKey.feature"
#line hidden

        [Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute()]
        public static void FeatureSetup(Microsoft.VisualStudio.TestTools.UnitTesting.TestContext testContext)
        {
            testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
            TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "CreateAPIKey", "", ProgrammingLanguage.CSharp, ((string[])(null)));
            testRunner.OnFeatureStart(featureInfo);
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute()]
        public static void FeatureTearDown()
        {
            testRunner.OnFeatureEnd();
            testRunner = null;
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute()]
        public virtual void TestInitialize()
        {
            if (((TechTalk.SpecFlow.FeatureContext.Current != null) 
                        && (TechTalk.SpecFlow.FeatureContext.Current.FeatureInfo.Title != "CreateAPIKey")))
            {
                Tavisca.Vexiere.FunctionalTest.SpecFlowScenarios.APIKeyFixtures.CreateAPIKeyFeature.FeatureSetup(null);
            }
        }

 // this method is throwing the null ref exception
       [Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute()]
        public virtual void ScenarioTearDown()
        {
            testRunner.OnScenarioEnd();
        }

        public virtual void ScenarioSetup(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
        {
            testRunner.OnScenarioStart(scenarioInfo);
        }

        public virtual void ScenarioCleanup()
        {
            testRunner.CollectScenarioErrors();
        }

        [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
        [Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute("CreateAPIKey_Success")]
        [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("FeatureTitle", "CreateAPIKey")]
        [Microsoft.VisualStudio.TestTools.UnitTesting.TestCategoryAttribute("createAPIKey")]
        public virtual void CreateAPIKey_Success()
        {
            TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("CreateAPIKey_Success", new string[] {
                        "createAPIKey"});
#line 5
this.ScenarioSetup(scenarioInfo);
#line 7
testRunner.Given("that an Organization is already set up in Vexiere hierarchy.", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
#line 8
testRunner.When("I want to register new APIKey for above created organization.", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
#line 9
testRunner.Then("It should create new APIKey as per requested.", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
#line hidden
            this.ScenarioCleanup();
        }
    }
}
#pragma warning restore
#endregion

2 个答案:

答案 0 :(得分:0)

我发布这个因为我遇到了类似的问题(我不认为它是原始的海报问题),但它可能对其他人有用。< / p>

使用版本管理器在TFS构建服务器上运行我的specflow测试时,我的app.token.config文件与普通的app.config文件略有不同。 (发布管理器在构建/发布过程中复制app.token.config文件,在配置文件中更改服务器名称等内容)

在我的情况下,我最近在app.config中为specflow设置添加了语言设置。

  <specFlow>
    <unitTestProvider name="MsTest" />
    <stepAssemblies>
      <stepAssembly assembly="SpecResults" />
    </stepAssemblies>
    <language feature="en-GB" />
  </specFlow>

但是在app.token.config文件中(这是测试在运行时使用的),unitTestProvider元素丢失了(剪切了n个拼写错误),我们的一些测试使用了[AfterScenario]清理例程给出NullReferenceException。将unitTestProvider元素添加到app.token.config修复了我的问题。

请注意app.token.config是特定于发布经理2013的问题,但是可能值得检查运行测试的.config文件,以确保它具有unitTestProvider元素。

答案 1 :(得分:0)

我发现如果你使用NUnit unitTestProvider,而不是xUnit(我的情况)或MSTest(在OPs情况下);然后它会正常工作。

我猜这与不同测试框架的测试生命周期(并行运行?)的差异有关。