1

When using BDD for higher level of tests, then this language in tests - given,when,then seems easy to understand.I am using C#.So,what I do is name the class as "whenthishappens",setup is the "given",and then I have tests.But,how to use this style of BDD when writing tests for a class method.Or,should I have just tests named "shouldDoXXX".?

1

4 Answers 4

2

I normally name my tests shouldDoXXXX where the test name describes what it should do for all similar contexts. So I might say shouldAddUpTwoNumbersCorrectly. This is a bit different to the way a lot of BDDers do it - the Ruby crowd particularly seem to like shouldAddTwoPlusTwoToMakeFour, so repeating the particular example they use. Whichever works for you!

Inside the test, I often write comments as Given / When / Then:

public void ShouldAddUpTwoNumbersCorrectly() 
{
    // Given two numbers
    // When I give them to the summer
    // Then the result should be the sum of the two numbers
}

Then I fill in the code between the comments. If the test is very simple I may skip the comments.

I don't bother with English-readable frameworks for a class test, because the audience is technical and capable of reading code. The BDD frameworks which do Given / When / Then were created largely to help with conversations with non-technical stakeholders, and to encourage developers to use their language. I don't find them useful at a class level. YMMV.

2

Here is a blog I wrote about BDD style tests for MSTest based code:

http://mrclyfar.blogspot.com/2010/02/amazing-mapping-demo-at-ted-2010.html. I used ideas from this blog post.

Personally I use StoryQ for my tests. StoryQ is a testing framework similar to MSpec and SpecFlow, in that you can write your tests out in a more descriptive manner. In the beginning, I did try using BDD naming for MSTest style code, but I found it to be less flexible then I wanted. I then experimented with MSpec, which is awesome.

In the end I chose StoryQ since it had that little bit more verbosity about it which I like. Makes it easier for me to write descriptive tests without having to use context inheritance.

5
  • So,when you are writing unit test for a class,you use given\when\then format? So,you will have multiple classes for testing a class,since when represents a scenario
    – jess
    Jul 5, 2010 at 11:14
  • Yeah, you would have multiple classes for the when scenarios. That is the suggested practise described in the Eric Lee blog post I gave. Jul 5, 2010 at 11:20
  • I just dived into StoryQ.And,was able to write test.I am using MBUnit Gallio to run tests.But,I don't see the descriptive output anywhere in the story format
    – jess
    Jul 5, 2010 at 13:45
  • I don't think the output for StoryQ will work with Gallio. I can only see the descriptive text in my MSTest output window, or Resharper test runner. I don't Gallio supports StoryQ. Jul 5, 2010 at 14:18
  • Just use "ExecuteWithReport", and have a look at the generated report file :) Jul 29, 2010 at 12:37
1

This C# framework does exactly what you require, and allows you to start your test method names with IWantTo

public void IWantTo...()

It is open source and found at

Converts this ...

[Test]
public void IWantToRegisterANewUser()
{
  ICustomer customer = new Customer();

  SoThat(MyBusinessValue.IncreaseCustomerBase)
    .As(new User())
    .Given(customer.Register)
    .When(customer.Confirm_Registration)
    .Then(customer.Login);
}

to this ...

I want to register a new user
  So that Increase customer base
       As user
    Given Register customer
     When Confirm customer registration
     Then Login customer
0

You could also have a look at the BDD for C# NUnit. An alternative way might be to use some C# libraries, which help writing BDD-like unit tests with readable descriptions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.