2

TDD & BDD? Which, Why and How?

Can anyone give a good explanation to justify "Which", "Why" and "How" on both?

Thanks in advance.

4

1 回答 1

1

TDD is used more for unit testing e.g. testing a method on a class. BDD is used for testing the behaviour of a system e.g. Creating a user, or Sending out new product emails.

So for TDD you might see something like.

public void Test()
{
    // Arrange.
    var sut = new ClassToTest();

    // Act.
    int result = sut.SoSomething();

    // Assert.
    Assert.Equal(result, 23);
}

With BDD (depending on the tools you're using) you tend to see something like this:

Feature: Add a user
    As a system admin
    In order to give a user access to the site
    I want to create a user account

Scenario: Creating a basic user
    Given I have the user's name
    When I create a new user account
    Then that user can log onto the site

As you can, BDD is testing the behaviour of a system rather then single unit. Here is a very good intro to BDD by Dan North - http://dannorth.net/introducing-bdd/

I would recommend using TDD when you are building your classes/code and want to testing little bits of it at a time. Use BDD when you want to test more then one of those classes in a test i.e. integration test.

EDIT:

With the how side of things, for BDD I would recommend using SpecFlow. This is a popular BDD tool which adds a lot of functionality to Visual Studio for creating feature files (The Feature: stuff I mentioned above) and running and debugging the tests.

Under the hood SpecFlow can use NUnit or MSTest to generate the tests. Other BDD tools include:

and many others I've forgotten about right now :) I would suggest you try them out and see which one you prefer.

For TDD you have many options including:

  • NUNit
  • xUnit
  • MSTest

A lot of the above tools can installed via NuGet in Visual Studio, which is handy.

于 2012-08-28T08:32:14.190 回答