behaviour driven development with specflow

20
BDD with SpecFlow Pascal Laurin February 2015 @plaurin78 [email protected] www.pascallaurin.com Microsoft C# MVP MSDEVMTL user group organizer Developer & Architect at GSoft

Upload: pascal-laurin

Post on 16-Jul-2015

218 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Behaviour Driven Development with SpecFlow

BDD with SpecFlowPascal Laurin

February 2015

@[email protected]

www.pascallaurin.com

Microsoft C# MVPMSDEVMTL user group organizerDeveloper & Architect at GSoft

Page 2: Behaviour Driven Development with SpecFlow

TDD with B instead of a T!

Somewhat inspired by TDD but using behaviours instead

of tests

What level of behaviours?

Systems level

Applications level

Components level

Classes level

| 2

What is BDD?

Page 3: Behaviour Driven Development with SpecFlow

Unit Tests

Focus on classes

Validate the implementation

Integrated Tests / Functional Tests / Acceptance Tests

Focus on components

Validate behaviours

Systems Tests

Focus on the complete system

Validate the whole system environment including external

dependencies

| 3

Differences between types of automated tests

Page 4: Behaviour Driven Development with SpecFlow

By changing the level of abstraction and the way we

communicate

Using the language of the business domain

Using sentences to express the expected behaviours

By focusing on what the system under test should do

Requirements, use cases, specifications and behaviours are all

useful for BDD

| 4

How to validate behaviours instead of tests?

Page 5: Behaviour Driven Development with SpecFlow

Not just for developers

Analysts, QA, business people must also included in this process

Specifications as tests, documentation and collaboration

tool

By automating the specifications like unit tests

By processing specifications as the documentation of the system

By working together on the specifications before writing code

What would a process like that looks like?

| 5

Going further

Page 6: Behaviour Driven Development with SpecFlow

| 6

[1] Specification by Example

Page 7: Behaviour Driven Development with SpecFlow

1. Deriving scope from goals

2. Specifying collaboratively

3. Illustrating using examples

4. Refining the specification

5. Automating validation without changing specifications

6. Validating frequently

7. Evolving a documentation system

| 7

Specification by Example

Page 8: Behaviour Driven Development with SpecFlow

An example of a good business goal

Increase repeat sales to existing customers by 50% over the next

12 months.

| 8

Business goals

Page 9: Behaviour Driven Development with SpecFlow

User stories for a basic loyalty system

In order to be able to do direct marketing of products to existing

customers, as a marketing manager I want customers to register

personal details by joining a VIP program.

In order to entice existing customers to register for the VIP

program, as a marketing manager I want the system to offer

free delivery on certain items to VIP customers.

In order to save money, as an existing customer I want to receive

information on available special offers.

| 9

Scope

Page 10: Behaviour Driven Development with SpecFlow

Free delivery

VIP customer with five books in the cart gets free delivery.

VIP customer with four books in the cart doesn’t get free delivery.

Regular customer with five books in the cart doesn’t get free

delivery.

VIP customer with five washing machines in the cart doesn’t get

free delivery.

VIP customer with five books and a washing machine in the cart

doesn’t get free delivery.

| 10

Key examples

Page 11: Behaviour Driven Development with SpecFlow

Free delivery

Free delivery is offered to VIP customers once they purchase a

certain number of books. Free delivery is not offered to regular

customers or VIP customers buying anything other than books.

Given that the minimum number of books to get free delivery is

five, then we expect the following:

| 11

Specification with examples

Page 12: Behaviour Driven Development with SpecFlow

Customer type Cart contents Delivery

VIP 5 books Free

VIP 4 books Standard

Regular 10 books Standard

VIP 5 washing machines Standard

VIP 5 books, 1 washing machine Standard

| 12

Specification with examples

Page 13: Behaviour Driven Development with SpecFlow

1. Deriving scope from goals

2. Specifying collaboratively

3. Illustrating using examples

4. Refining the specification

5. Automating validation without changing

specifications

6. Validating frequently

7. Evolving a documentation system

| 13

Specification by Example

Page 14: Behaviour Driven Development with SpecFlow

14

SpecFlow

Visual Studio extension, runs like unit tests using any test runner

Write specifications in English with syntax coloring and auto-

completion using the Gherkin[1] language

[1] https://github.com/cucumber/cucumber/wiki/Gherkin

Page 15: Behaviour Driven Development with SpecFlow

15

SpecFlow

Integrated debugging support

Matching specification lines with step definitions using a RegEx

syntax

Use ScenarioContext.Current and generic Get/Set methods to

store and retrieve test related data between step definitions

Page 16: Behaviour Driven Development with SpecFlow

16

SpecFlow

Provides code snippet to implement missing step definitions either by

pressing F12 on an unmatched line or by runner the tests

Page 17: Behaviour Driven Development with SpecFlow

1. Deriving scope from goals

2. Specifying collaboratively

3. Illustrating using examples

4. Refining the specification

5. Automating validation without changing specifications

6. Validating frequently

7. Evolving a documentation system

| 17

Specification by Example

Page 18: Behaviour Driven Development with SpecFlow

| 18

Specification by Example book

Page 19: Behaviour Driven Development with SpecFlow

BDD and TDD

Unit tests, integrated tests and manual tests are all important!

Improving communication

Use business domain language, higher level of abstraction,

avoiding technical vocabulary and implementation details

Requires investment

Good software design practices will help

You won’t necessarily hit the same challenges than with TDD

The real value is in collaboration and automation!

| 19

Conclusion

Page 20: Behaviour Driven Development with SpecFlow

References

[1] Specification by Example

• http://specificationbyexample.com/

SpecFlow

• http://specflow.org/home.aspx

SlideShare of the presentation

• http://www.slideshare.net/PascalLaurin

BitBucket for the code

• https://bitbucket.org/pascallaurin/bdd-talk

@plaurin78

[email protected]

www.pascallaurin.com| 20

Questions?