Specflow

From Logic Wiki
Jump to: navigation, search


What Is An Acceptance Test?

  • Validates that the right system is being built
  • Business/user/customer point of view
  • Written in non-technical format (uses Gherkin)
  • it based on Pass / Fall
  • Helps document what the system should do
  • When Automated, become "living documentation"
  • Usually execute a vertical slice through the system
  • Helps define what "done" means

Business Facing Tests

A test that's intended to be used as an aid to communicating with non-programming members of a development team such as customers, users, business analysts and the like

  • Product owners
  • Users
  • BAs
  • Testers
  • Developers

Technology facing Tests

Unit tests

  • Developers

Use in Test First Approaches

  1. Collaboratively define what the system should do next
  2. Write the tests in SpecFlow
  3. Write Code
  4. All Test pass ?
    1. YES -> Go to 1
    2. NO -> Go to 3

Specflow Structure

  • On the Visual Studio we prepare a Feature File
  • it containing Scenarios
  • Scenario contains Steps
  • Steps contains Test Codes
  • and it's on Testing Framework
  • use # to write comments

Feature file and scenarios are written in Gherkin language

Starting SpecFlow

  1. Create a solution
  2. Install NuGet Specflow
  3. Restart VS
  4. Add a Class Library Project named Something.specs
  5. Add New Item
  6. Select Specflow feature file
  7. name this file
  8. Write feature and scenarios
  9. Install NuGet of Specflow NUnit

Features

  • Contain one or more scenarios
  • Group/contain logically related test scenarios
  • Represents small, discrete features of the system
  • Start with "Feature" keyword
  • Followed by feature name/terse description
  • Optional free text description
Feature:Brush Teeth
  Brushing teeth is good
  because it helps keep 
  them clean
  • following with the keywords below (human centric)
    • As a ...
    • I want ...
    • So that ...
Feature:Brush Teeth
  As a human
  I want to brush my teeth
  So that they stay pain free
  • or alternatively with the keywords below (value centric)
    • In order to
    • As a
    • I want
Feature:Brush Teeth
  In order to stay pain free
  As a human
  I want to brush my teeth
  • or any text you think it's useful

Scenarios

  • Concrete examples of expected system behaviour
  • Each scenario describes a particular situation
  • Each scenario should be independent and isolated
  • Can represent:
    • "Happy paths"
    • Error paths
    • Edge cases
  • Start with "Scenario" keyword
  • followed by title
 Scenario:Succesfull brushing

Scenario Outlines

Not to write multiple scenarios just for minor changes we use this

We use placeholders instead of values

ScenarioOutline:Teeth whiteness 
  Given I'm using "<brand>" brand toothpaste
  When I brush for "<mins>" minutes
  Then the teeth look "<percent>" white
  Examples:
     | brand | mins | percent |
     | Brand X | 1  | 80  |
     | Brand Y | 3  | 100 |
     | Brand Z | 10 | 50  |

Scenario Steps

  1. st Phase Set up initial state Given
  2. nd Phase Perform actions When
  3. rd Check end state Then
Scenario:Succesfull brushing
  Given there is toothpaste on the brush
  Given the mouth is open 
  When the back teeth are brushed
  When the front teeth are brushed
  Then the teeth look clean 
  Then the mouth feels fresh
  Then the braces aren't damaged

Or alternativeley to improve readibility

Scenario:Succesfull brushing
  Given there is toothpaste on the brush
  And the mouth is open 
  When the back teeth are brushed
  And the front teeth are brushed
  Then the teeth look clean 
  And the mouth feels fresh
  But the braces aren't damaged

Using Data Tables in Steps

  • Allows tabular data to be passed to an automation step
  • Useful for specifying larger data rather than multiple steps
Scenario:Successful brushing
  Given I have paste
    And brush
    And water
  When the back teeth are brushed
  Then the teeth look clean

we can rewrite this with using tables

Scenario:Successful brushing
  Given I have the following tools
    | toolname |
    | brush    |
    | paste    |
    | water    |
  When the back teeth are brushed
  Then the teeth look clean

Tags

To categorize scenarios and features in test explorer in VS we use tags

  • Mark features and scenarios with arbitrary tags
  • Map to unit test framework "categories"
  • Scenarios "inherit" feature tags
  • Scenarios and features can have multple tags
  • Tags specified using @
  • @ignore is a special case


@smoketest
Scenario: Brush ..... 

Possible uses

  • Group features into feature supersets
  • Mark certain test as @important
  • Differentiate between @slow and @fast executing tests
  • Mark tests as @wip (Work In Progress)
  • Mark test to be executed @weekly @daily @hourly
  • Mark test as @humanexecuted

Background

  • Provides context (state setup) to the scenarios in a feature
  • Executed before every scenario
  • We can reduce replications
  • Better not too long
 Background:
   Given I have teeth
   And I have some toothpaste

 Scenario:Successfull brushing
 ...
 Scenario:Toothpaste runs out 
 ... 

Using Specflow

After writing Features ans scenarios, Write click and select Generate Step Definitions and click Generate. Give a file name and steps will be generated.

Style gives you options in generate options

Please check Specflow Advanced page