28
votes

Seems like the internet doesn't have a definitive answer, or set of principles to help me answer the question. So I turn to the great folk on SO to help me find answers or guiding thoughts :)

SpecFlow is very useful for BDD in .NET. But when we talk about BDD are we just talking integration/acceptance tests, or are we also talking unit tests - a total replacement for TDD?

I've only used it on small projects, but I find that even for my unit tests, SpecFlow improves code documentation and thinking in terms of language. Converseley, I can't see the full code for a test in one place - as the steps are fragmented.

Now to you..........

EDIT: I forgot to mention that I see RSpec in the RoR community which uses BDD-style syntax for unit testing.

6
When counting the number of each type of test, you should think of a triangle. The base is unit, the middle is integration, and the top is UI testing. Int & UI tests are a yes but the sheer number of unit tests you are hopefully writing SHOULD make SpecFlow impractical for unit tests.Brantley Blanchard

6 Answers

32
votes

I've recently started to use SpecFlow for my BDD testing, but also, I still use unit and integration tests.

Basically, I split the tests into seperate projects:

  • Specs
  • Integration
  • Unit

My unit tests are for testing a single method and do not perform any database calls, or external references whatsoever. I use integration tests for single method calls (maybe sometime two) which do interact with an external resources, such as a database, or web service, etc.

I use BDD to describe tests which mimick the business/domain requirements of the project. For example, I would have specs for the invoice generation feature of a project; or for working with a shopping basket. These tests follow the

As a user, I want, In order to

type of semantics.

My advise is to split your tests based on your needs. Avoid trying to perform unit testing using SpecFlow.

5
votes

We have started using Specflow even for our unit tests.

The main reason (and benefit) for this is that we find that it forces you to write the tests from a behavior point of view, which in turn forces you to write in a more implementation agnostic way and this ultimately results in tests which are less brittle and more refactoring friendly.

Sure this can also be done with standard unit testing frameworks, but you aren't guided that way as easily as we have found we are using specflow and the gherkin syntax.

There is some overhead setting things up for specflow, but we find this is quickly repaid when you have quite a few tests (due to the significant step reusability that you can get with specflow) or you need refactor your implementation.

Plus you get nice readable specs that are easy for newcomers to the team to understand.

3
votes

Given:

  • Unit tests are test of (small) "units of code"
  • The customer of most “units of codes” are other programmers.
  • Part of the reason for having a unit test is to provide an example of how to call the code.

Therefore:

  • Clearly unit tests should normally be written in the programming language that the users of the “unit of code” will be calling it with.

However:

  • Sometimes data tables are needed to setup the conditions a unit test runs in.
  • Most unit test frameworks are not good at using tables of data.

Therefore:

  • Specflow may be the best option for some unit test, but should not be your default choose.
1
votes

I see it as an integration testing which mean it doesn't replace your unit test cases written as part of your TDD process. Someone will have different opinion about this. IMHO unit test case only test the methods/functions and all the dependencies should be mocked and injected. When in it comes to integration testing, you will be injecting real dependencies instead of mocked one. You could do the same integration testing with any of the unit testing frameworks, but the BDD provides you cleaner way of explaining the integration test use case in a Domain Specific Language which is a plain English(or any localized language).

Ta,
Rajeesh

1
votes

I used specflow for BDD testing on two different good sized applications. Once we worked through the kinks of the sentence naming conventions, it worked out pretty good. BA's and QA's, and even interns could write BDD tests for the application.

However, I ALSO used it for unit tests. Heresy! I can hear some of you scream. However, there were VERY good reasons for it. The system was responsible for making many calculations or determinations based off a lot of different data. With lots of unit tests that require all this data to be input for test purposes, it makes it a LOT easier to manage that data used for the unit tests via the table format provided by specflow. Effectively mocking the data repository in table format, allowing the different components to be vigorously tested.

I don't know if I would do it in every case, but in the ones I used it for, it made laying out the volumes of data necessary for for performing the unit tests so much easier and clearer.

0
votes

In the end we are trying to deliver to the customer exactly what the customer wants and as such I really don't see the need to write unit tests in addition to SpecFlow. After all, it exercises the same code base. I am fairly new to BDD/ATDD/TDD but other than being "complete" and strictly adhering to TDD I'm finding it unnecessary to write more unit tests.

Now I suppose if the team was dispersed and the developer was not able to run the entire application then separate unit tests would be necessary but where the developer(s) has access to the entire code base and is able to run the application, then why bother write more tests.