Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpecFlow/BDD for Unit Tests?

Tags:

bdd

specflow

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.

like image 864
Nicko-Mctricko Avatar asked Nov 11 '10 09:11

Nicko-Mctricko


People also ask

Can we use BDD for unit testing?

BDD test frameworks are not meant for writing unit tests. Unit tests are meant to be low-level, program-y tests for individual functions and methods. Writing Gherkin for unit tests is doable, but it is overkill. It is much better to use established unit test frameworks like JUnit, NUnit, and pytest.

Can unit test be integrated with SpecFlow?

SpecFlow supports several unit test framework you can use to execute your tests. To use a specific unit test provider, you have to add it's dedicated NuGet package to your project. You can only have one of these packages added to your project at once.

Is SpecFlow a BDD tool?

What is SpecFlow? SpecFlow is a BDD framework for . NET which boosts your productivity by helping you with writing your feature files and automation code in your favorite IDE using C# and . NET methods.

What is BDD using SpecFlow?

SpecFlow is a testing framework that supports Behaviour Driven Development (BDD). It lets us define application behavior in plain meaningful English text using a simple grammar defined by a language called Gherkin.


Video Answer


2 Answers

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.

like image 183
Jason Evans Avatar answered Oct 09 '22 07:10

Jason Evans


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.

like image 29
Sam Holder Avatar answered Oct 09 '22 07:10

Sam Holder