Appendix D — BDD

Published

2024-05-24

Please open an issue on GitHub

This appendix provides more information on behavior-driven development (BDD). If you decide to adopt the BDD functions in your test suite, I highly recommend creating code snippets to reduce the typing/copying and pasting.1

You can edit keyboard shortcuts by selecting Tools > Edit Code Snippets…

Snippets


snippet bddback
    describe(
    "Background: ${1:background}
        Given ...
        And ...", code = {
    })
    
snippet bddfeat
    describe(
     "Feature: ${1:feature}
        As a ...
        I want ...
        So that ...", code = {
        
    })
    
snippet bddscene
    it(
     "Scenario: ${1:scenario}
       Given ...
       When ...
       Then ...", code = {
        expect_equal(TRUE, TRUE)
    })
    
snippet bddtemp
    describe(
    "Feature: ${1:feature}
        As a ...
        I want ...
        So that ...", code = {
        it(
         "Scenario: ${2:scenario}
           Given ...
           When ...
           Then ...", code = {
            expect_equal(TRUE, TRUE)
        })
    })

 

Templates

# bddback ----
describe(
"Background: background
    Given ...
    And ...", code = {
})

# bddfeat ----
describe(
 "Feature: feature
    As a ...
    I want ...
    So that ...", code = {

})

# bddscene ----
it(
 "Scenario: scenario
   Given ...
   When ...
   Then ...", code = {
  expect_equal(TRUE, TRUE)
})

# bddtemp ----
describe(
"Feature: feature
    As a ...
    I want ...
    So that ...", code = {
  it(
   "Scenario: scenario
     Given ...
     When ...
     Then ...", code = {
    expect_equal(TRUE, TRUE)
  })
})

Snippets also work well with tab completion in the Posit Workbench IDE:

BDD Code Snippets

BDD Code Snippets

BDD uses a specific format for translating application behavior into into features. These ‘user stories’ are typically written in the Gherkin language and include the following sections:

Feature: title of feature
  
  As a ...
  I want ...
  So that ...
  
  Scenario 1:
    When ... 
    And ... 
    Then ...
    
  Scenario 2:
    When ... 
    And ... 
    Then ...

The testthat BDD functions can be adapted to use this format, because the description argument a text string and these functions can be nested.

testthat::describe(description = "
  Feature: Scatter Plot Data Visualization

  As a film analyst
  I want to visualize data on a scatter plot with various inputs
  So that I can analyze relationships between variables and groups in a customizable way.

  Background:
    Given the Shiny app is launched
    And I have a dataset available with continuous and categorical variables
  ", code = {
    

  testthat::describe(description = "
  Scenario: Select x and y continuous variables for plotting
    When I select the variable 'input$x' for the x-axis
    And I select the variable 'input$y' for the y-axis
    Then the scatter plot should show 'input$x' on the x-axis
    And 'input$y' on the y-axis
    ", code = {
      
    })
    
  })

  1. Read more about creating code snippets on the Posit website.↩︎