Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BDD and microservices

Our solution relies on microservices. On the other hand, our CIO expects us to practice Behavior Driven Development on every new feature.

Is it possible to manage BDD in a microservices architecture ? Based on your experience, is it a good practice to adopt BDD against such an architecture, or do you think we should directly look at integration testing ?

[EDIT]

More precisely, in my opinion, BDD Tests are expected to verify the business logic, and only the business logic. In many frameworks, BDD Tests scenarios are created by the skateholders, with a DSL. BDD Tests tend to converge to exclusive "Infrastructure Ignorant" practices. On the other hand, Integration Tests are supposed to verify that the solution matches the target infrastructure (they are done by DevOps ?), and only the infrastructure. When business functions are "distributed" over microservices, you should mock almost everything (infra and business) in your BDD Tests environment (it should be the local environment) and mocking business weakens a lot your goals. Do you think these practices are compatible ?

like image 446
Rénald Avatar asked Jun 03 '15 13:06

Rénald


1 Answers

Why do you think that BDD and integration testing are different?

BDD just means driving your design through the desired behaviour, usually expressed through a set of acceptance tests.

These tests may be 'integration tests' which involve many [micro]services or they may be tests which specify the desired behaviour of a single service, or a single class in that service. Ideally there will be a mix of tests at all these levels. The important thing is that you specify the behaviour you want and use this to drive the development.

How your system is implemented is, to some degree, irrelevant as long as it exhibits the expected behaviour. For the high level tests that treat the system as a black box, this is true and the lower you go and the closer you you get to the actual code this becomes less true (as you are effectively testing the implementation at that point).

So I would focus on the behaviour expected from the new features and write the specifications for these acceptance tests first, then implement your services to fulfil the required behaviour adding lower level tests as needed in a pragmatic way, bearing in mind that the lower level the tests go the more likely they are to be fragile and to need to be changed as you change your implementation.

EDIT

Based on your question edit.

I don't agree that BDD tests should test only the business logic. In fact it is usual that BDD tests are more focussed on testing the system as a whole, with all the parts integrated together. Having said that BDD is just a style of testing by specifying the desired behaviour, and can be applied to any level of the application. You could test a single class by specifying the behaviour using Gherkin syntax, and we sometimes do this. we also specify the expected behaviour of the whole system using Gherkin and the expected behaviour of our services individually. These tests will naturally be a slightly different format depending on the level we are targeting.

For the system tests we might have specification like this:

Scenario: user can perform action A
   Given I am a user with access to some feature A
   And feature A is enabled for the user
   When I call perform action A with parameters 'Bob' and 'John'
   Then A 'BobJohn' is created
   And notifications are sent to the current user

for individual services we might have tests like

Scenario: create messages are handled correctly
   Given the service is set up
   When a message arrives to create a 'BobJohn'
   Then a new entry is added to the database with the key 'BobJohn'
   And an outgoing notification message for 'BobJohn' is created

For individual classes we might have tests like

Scenario: Notifier class should send notifications via all users preferred means
    Given the current user wants notification by Twitter
    And the current user who wants notification by email
    When I send the notification 'BobJohn' to the current user
    Then the twitter notifier should be invoked with 'BobJohn'
    And the email notifier should be invoked with 'BobJohn'

These are all BDD style tests but they test different aspects of the system.

like image 52
Sam Holder Avatar answered Oct 05 '22 21:10

Sam Holder