2012-11-01 10 views
5

私はNunitでSpecFlowを使用していますが、TestFixtureSetUpAttributeを使用して環境テストを設定しようとしていますが、呼び出されることはありません。NUnitでのSpecflowはTestFixtureSetUpAttributeを尊重しません

私は既にMSTestsとClassInitialize属性を使用しようとしましたが、同じことが起こります。この関数は呼び出されません。

どのようなアイデアですか?

[Binding] 
public class UsersCRUDSteps 
{ 
    [NUnit.Framework.TestFixtureSetUpAttribute()] 
    public virtual void TestInitialize() 
    { 
     // THIS FUNCTION IS NEVER CALLER 

     ObjectFactory.Initialize(x => 
     { 
      x.For<IDateTimeService>().Use<DateTimeService>(); 
     }); 

     throw new Exception("BBB"); 
    } 

    private string username, password; 

    [Given(@"I have entered username ""(.*)"" and password ""(.*)""")] 
    public void GivenIHaveEnteredUsernameAndPassword(string username, string password) 
    { 
     this.username = username; 
     this.password = password; 
    } 

    [When(@"I press register")] 
    public void WhenIPressRegister() 
    { 
    } 

    [Then(@"the result should be default account created")] 
    public void ThenTheResultShouldBeDefaultAccountCreated() 
    { 
    } 

ソリューション:

[Binding] 
public class UsersCRUDSteps 
{ 
    [BeforeFeature] 
    public static void TestInitialize() 
    { 
     // THIS FUNCTION IS NEVER CALLER 

     ObjectFactory.Initialize(x => 
     { 
      x.For<IDateTimeService>().Use<DateTimeService>(); 
     }); 

     throw new Exception("BBB"); 
    } 

    private string username, password; 

    [Given(@"I have entered username ""(.*)"" and password ""(.*)""")] 
    public void GivenIHaveEnteredUsernameAndPassword(string username, string password) 
    { 
     this.username = username; 
     this.password = password; 
    } 

    [When(@"I press register")] 
    public void WhenIPressRegister() 
    { 
    } 

    [Then(@"the result should be default account created")] 
    public void ThenTheResultShouldBeDefaultAccountCreated() 
    { 
    } 

答えて

6

実際のユニットテストは、あなたから生成され.cs内部にあるので、それは(ユニットテストであなたのステップクラス内ではなく内側にあるので、あなたのTestInitializeが呼び出されません.featureファイル)。 SpecFlowはそれがフックと呼ばれている独自のテスト生涯のイベントのしている

、これらはすべて事前に定義されたフックです:

  • [BeforeTestRun]/[AfterTestRun]
  • [BeforeFeature]/[AfterFeature]
  • [BeforeScenario]/[AfterScenario]
  • [BeforeScenarioBlock]/[AfterScenarioBlock]
  • [BeforeStep]/[AfterStep]

これにより、設定の柔軟性が向上することに注意してください。追加情報についてはsee the documentationをご覧ください。あなたはTestFixtureSetUpはあなたが書く必要があるので、あなたはおそらく、各機能の前に一度呼び出されますBeforeFeatureフックが必要になります属性を使用したいという事実に基づいて

[Binding] 
public class UsersCRUDSteps 
{ 
    [BeforeFeature] 
    public static void TestInitialize() 
    {    
     ObjectFactory.Initialize(x => 
     { 
      x.For<IDateTimeService>().Use<DateTimeService>(); 
     }); 

     throw new Exception("BBB"); 
    } 

    //... 
} 

[BeforeFeature]属性という方法はstaticが必要です。

VSインテグレーションを使用している場合は、SpecFlow Hooks (event bindings)という名前のプロジェクトアイテムタイプがあります。これは、開始に役立ついくつかの定義済みのフックでバインドクラスを作成します。

+1

ありがとうございます。私は静的に私の機能を変更する必要があります – muek

関連する問題