Amongst long list of benefits that automated testing gives our projects we have ‘documentation’ feature. I think it takes even one of the first places. Many times I was looking at unit tests trying to decipher what they did and why they were there. Ask yourself a question if your unit test project is a mixture of test methods or it shapes a specification that you can share with your client. Saying ‘specification’ I meant something like that:

nspec_sample

This is a test result generated by NSpec for a sample project. NSpec is a beautiful test framework that I’ve focused on for the last several months and probably will stay for a longer time.

If you really want to change your testing experience it’s more often about the approach not a framework. I hate when people see a goal in moving from NUnit to xUnit because it’s almost only changing attributes used for testing. NSpec also allows you to omit class/method attributes but the key concept is to create scenarios that are cool to maintain. For me the testing experience is close to Jasmine (JavaScript) but here we can use lambdas instead of long “function(){}” syntax. The image is worth a thousand words so:

nspec_1

It generates the following result:

nspec_2

Why better than: MSTest/NUnit/xUnit

These are just TDD frameworks with flat structure. No way to define scenarios. NSpec enables you to build real scenarios. It also simplifies the way we organise tests (far easier to add new test, find the proper place). Contexts decrease the size of Arrange/Given part.

Why better than MSpec:

Both are BDD frameworks, but NSpec has more user-friendly syntaxt. It’s possible to avoid hard string concatenation (like ShouldDoSomething, or should_do_something_else). Test result for complex scenarios looks better, you can have more levels. MSpec also takes advantage of lambdas. If you don’t like it, with NSpec you can write tests without them (using conventions).

Why better than SpecFlow:

I don’t believe that the client can develop SpecFlow*.feature files. Because of that I don’t like the workload involved with maintaining *.feature files and their mappings. I’m a developer and what I really want to do is to write code. Although what’s good in SpecFlow is creating specification-by-example.

NSpec disadvantages:

* debugging – see this official tutorial or install additional test adapter
* no resharper support – still in progress
* lot of lambdas (non-mandatory) – for some people it’s an advantage but maybe not for all

Regarding debugging with resharper I have my own solution.Check this out here

Keep in touch.

Advertisements

2 thoughts on “Improve your testing experience with NSpec, best BDD test framework

  1. I’ll have to disagree. Sorry.

    It’s not true that you can’t define scenarios in NUnit. You just change your context calls to nested classes. But both ways are ugly and unmaintainable.

    In your example it’s hard to see what you are testing. To much boilerplate code. And these are the simplest tests you will ever write. Also you have setup for contexts which makes it harder to read tests. You have to jump between tests and setup. Using setup is considered bad and that’s why it was removed from XUnit. I almost never use setup. Arrange/Act/Assert in test .

    I don’t understand “Why better than Selenium:” paragraph.
    Does Selenium support gherkin in any way? Isn’t it that you can use Selenium in tests specified in gherkin? When I use Selenium I write C# code.

    1. Hello Piotr,
      “Selenium” is a typo – should be SpecFlow. Fixed, thanks!
      According to NUnit – I had tried the approach with nested classes before I switched to NSpec. It was a pain. And there is no test runner that supports running such tests as scenarios.
      With NSpec you don’t need global SetUp for the context. It’s still you who places “Arrange = before” part anywhere you want. I also prepare to have it as close to the current test/context as possible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s