5

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()
    {
    }
4

1 に答える 1

6

Steps クラス内にあり、単体テスト内にないため、呼び出されませTestInitializeん (実際の単体テストは、ファイル.csから生成された 内にあるため)。.feature

SpecFlow には、フックと呼ばれる独自のテスト ライフタイム イベントがあります。これらはすべて定義済みのフックです。

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

これにより、セットアップの柔軟性が高まることに注意してください。詳細については、ドキュメントを参照してください

属性を使用したいという事実に基づいて、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

SpecFlow Hooks (event bindings)また、VS 統合を使用している場合は、開始するのに役立つ定義済みのフックを持つバインディング クラスを作成するというプロジェクト アイテム タイプがあることにも注意してください。

于 2012-11-02T09:51:35.717 に答える