23

私は MSpec を使用して単体テストを作成してきましたが、BDD スタイルが本当に好きです。BDD スタイルの方がはるかに読みやすいと思います。現在、MSpec がサポートしていない Silverlight を使用しているため、MSTest を使用する必要がありますが、BDD スタイルを維持したいので、これを行う方法を模索しています。

私が達成しようとしていることを説明するために、MSpec テストを作成する方法を次に示します。

[Subject(typeof(Calculator))]    
public class when_I_add_two_numbers : with_calculator
{
  Establish context = () => this.Calculator = new Calculator();
  Because I_add_2_and_4 = () => this.Calculator.Add(2).Add(4);
  It should_display_6 = () => this.Calculator.Result.ShouldEqual(6);
}

public class with_calculator
{
  protected static Calculator;
}

したがって、MSTest を使用して、このようなテストを作成しようとします (ただし、TestInitialize 属性を 2 つ入れているため、機能しないことがわかりますが、私がやろうとしていることはわかります..)

[TestClass]
public class when_I_add_two_numbers : with_calculator
{
   [TestInitialize]
   public void GivenIHaveACalculator()
   {
      this.Calculator = new Calculator();
   }

   [TestInitialize]
   public void WhenIAdd2And4()
   {
      this.Calculator.Add(2).Add(4);
   }

   [TestMethod]
   public void ThenItShouldDisplay6()
   {
      this.Calculator.Result.ShouldEqual(6);
   }
}

public class with_calculator
{
  protected Calculator Calculator {get;set;}
}

MSTest を使用してこの方法でテストを作成するための、よりエレガントな提案を思いつく人はいますか?

4

5 に答える 5

35

これについてどう思うか:

[TestClass]
public class when_i_add_two_numbers : with_calculator
{
    public override void When()
    {
        this.calc.Add(2, 4);
    }

    [TestMethod]
    public void ThenItShouldDisplay6()
    {
        Assert.AreEqual(6, this.calc.Result);
    }

    [TestMethod]
    public void ThenTheCalculatorShouldNotBeNull()
    {
        Assert.IsNotNull(this.calc);
    }
}

public abstract class with_calculator : SpecificationContext
{
    protected Calculator calc;

    public override void Given()
    {
        this.calc = new Calculator();
    }
}

public abstract class SpecificationContext
{
    [TestInitialize]
    public void Init()
    {
        this.Given();
        this.When();
    }

    public virtual void Given(){}
    public virtual void When(){}
}

public class Calculator
{
    public int Result { get; private set; }
    public void Add(int p, int p_2)
    {
        this.Result = p + p_2;
    }
}
于 2011-01-13T10:41:30.790 に答える
2

Mark Nijhofは、彼のFohjin.DDD github リポジトリで、NUnit を使用して Give-When-Then スタイル テストを実行する例を公開しています。

上記の例からの抜粋を次に示します。

public class When_registering_an_domain_event : BaseTestFixture<PreProcessor>
{
    /* ... */

    protected override void When()
    {
        SubjectUnderTest.RegisterForPreProcessing<ClientMovedEvent>();
        SubjectUnderTest.Process();
    }

    [Then]
    public void Then_the_event_processors_for_client_moved_event_will_be_registered()
    {
        IEnumerable<EventProcessor> eventProcessors;
        EventProcessorCache.TryGetEventProcessorsFor(typeof(ClientMovedEvent), out eventProcessors);
        eventProcessors.Count().WillBe(1);
    }
}

そして、基本クラスの実装で Given を確認できます。

[Given]
public void Setup()
{
    CaughtException = new NoExceptionWasThrownException();
    Given();

    try
    {
        When();
    }
    catch (Exception exception)
    {
        CaughtException = exception;
    }
    finally
    {
        Finally();
    }
}
于 2012-08-02T20:32:43.857 に答える
0

MSTestEnhancerが役立つ場合があり、NuGet.orgからパッケージを入手できます。


サンプルコードは次のとおりです。

[TestClass]
public class TheTestedClassTest
{
    [ContractTestCase]
    public void TheTestedMethod()
    {
        "When Xxx happens, results in Yyy.".Test(() =>
        {
            // Write test case code here...
        });

        "When Zzz happens, results in Www.".Test(() =>
        {
            // Write test case code here...
        });
    }
}

テスト結果が表示されると、次のようになります。

試験結果

私はそれについてのより多くの情報を提示するために投稿を書きました. 詳細については、単体テストの結果を読みやすくするための MSTestEnhancer の紹介 - walterlvを参照してください。

于 2018-03-05T06:34:51.057 に答える