-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdd.qmd
176 lines (137 loc) · 3.43 KB
/
bdd.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# BDD {#sec-bdd}
```{r}
#| eval: true
#| echo: false
#| include: false
source("_common.R")
```
```{r}
#| label: git_contrib_box
#| echo: false
#| results: asis
#| eval: true
git_contrib_box()
```
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.[^tests-tools-snippets]
[^tests-tools-snippets]: Read more about creating code snippets on the [Posit website.](https://docs.posit.co/ide/user/ide/guide/productivity/snippets.html)
You can edit keyboard shortcuts by selecting **Tools** > **Edit Code Snippets...**
::: {layout="[49, -2, 49]"}
#### Snippets {.unnumbered}
``` sh
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 {.unnumbered}
```r
# 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](images/tests_spec_bdd_snippets.png){width='80%' fig-align='center'}
BDD uses a specific format for translating application behavior into into features. These 'user stories' are typically written in the [Gherkin language](https://cucumber.io/docs/gherkin/reference/) and include the following sections:
```{verbatim}
#| code-fold: false
#| eval: false
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.
```{r}
#| eval: false
#| code-fold: false
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 = {
})
})
```