Debian Codegraph @

docs/philosophy.rst

raw
1 ===========================
2 Behavior Driven Development
3 ===========================
4
5 Behavior-driven development (or BDD) is an agile software development
6 technique that encourages collaboration between developers, QA and
7 non-technical or business participants in a software project. It was
8 originally named in 2003 by `Dan North`__ as a response to test-driven
9 development (TDD), including acceptance test or customer test driven
10 development practices as found in extreme programming. It has `evolved over
11 the last few years`__.
12
13 __ https://dannorth.net/introducing-bdd
14 __ https://forums.pragprog.com/forums/95/topics/3035
15
16 On the "Agile specifications, BDD and Testing eXchange" in November 2009 in
17 London, Dan North `gave the following definition of BDD`__:
18
19 BDD is a second-generation, outside–in, pull-based, multiple-stakeholder,
20 multiple-scale, high-automation, agile methodology. It describes a cycle
21 of interactions with well-defined outputs, resulting in the delivery of
22 working, tested software that matters.
23
24 __ https://skillsmatter.com/skillscasts/923-how-to-sell-bdd-to-the-business
25
26 BDD focuses on obtaining a clear understanding of desired software behavior
27 through discussion with stakeholders. It extends TDD by writing test cases
28 in a natural language that non-programmers can read. Behavior-driven
29 developers use their native language in combination with the ubiquitous
30 language of domain-driven design to describe the purpose and benefit of
31 their code. This allows the developers to focus on why the code should be
32 created, rather than the technical details, and minimizes translation
33 between the technical language in which the code is written and the domain
34 language spoken by the business, users, stakeholders, project management,
35 etc.
36
37
38 BDD practices
39 -------------
40
41 The practices of BDD include:
42
43 - Establishing the goals of different stakeholders required for a vision to
44 be implemented
45 - Drawing out features which will achieve those goals using feature
46 injection
47 - Involving stakeholders in the implementation process through outside–in
48 software development
49 - Using examples to describe the behavior of the application, or of units
50 of code
51 - Automating those examples to provide quick feedback and regression
52 testing
53 - Using 'should' when describing the behavior of software to help clarify
54 responsibility and allow the software's functionality to be questioned
55 - Using 'ensure' when describing responsibilities of software to
56 differentiate outcomes in the scope of the code in question from
57 side-effects of other elements of code.
58 - Using mocks to stand-in for collaborating modules of code which have not
59 yet been written
60
61
62 Outside–in
63 ----------
64
65 BDD is driven by `business value`__; that is, the benefit to the business
66 which accrues once the application is in production. The only way in which
67 this benefit can be realized is through the user interface(s) to the
68 application, usually (but not always) a GUI.
69
70 __ https://lizkeogh.com/2007/06/13/bdd-tdd-done-well/
71
72 In the same way, each piece of code, starting with the UI, can be
73 considered a stakeholder of the other modules of code which it uses. Each
74 element of code provides some aspect of behavior which, in collaboration
75 with the other elements, provides the application behavior.
76
77 The first piece of production code that BDD developers implement is the UI.
78 Developers can then benefit from quick feedback as to whether the UI looks
79 and behaves appropriately. Through code, and using principles of good
80 design and refactoring, developers discover collaborators of the UI, and of
81 every unit of code thereafter. This helps them adhere to the principle of
82 YAGNI, since each piece of production code is required either by the
83 business, or by another piece of code already written.
84
85
86 The Gherkin language
87 --------------------
88
89 The requirements of a retail application might be, "Refunded or exchanged
90 items should be returned to stock." In BDD, a developer or QA engineer
91 might clarify the requirements by breaking this down into specific
92 examples. The language of the examples below is called Gherkin and is used
93 by *behave* as well as many other tools.
94
95 .. code-block:: gherkin
96
97 Scenario: Refunded items should be returned to stock
98 Given a customer previously bought a black sweater from me
99 and I currently have three black sweaters left in stock.
100 When he returns the sweater for a refund
101 then I should have four black sweaters in stock.,
102
103 Scenario: Replaced items should be returned to stock
104 Given that a customer buys a blue garment
105 and I have two blue garments in stock
106 and three black garments in stock.
107 When he returns the garment for a replacement in black,
108 then I should have three blue garments in stock
109 and two black garments in stock.
110
111 Each scenario is an exemplar, designed to illustrate a specific aspect of behavior of the application.
112
113 When discussing the scenarios, participants question whether the outcomes
114 described always result from those events occurring in the given context.
115 This can `help to uncover further scenarios which clarify the
116 requirements`__. For instance, a domain expert noticing that refunded items
117 are not always returned to stock might reword the requirements as "Refunded
118 or replaced items should be returned to stock, unless faulty.".
119
120 __ https://dannorth.net/whats-in-a-story
121
122 This in turn helps participants to pin down the scope of requirements,
123 which leads to better estimates of how long those requirements will take to
124 implement.
125
126 The words Given, When and Then are often used to help drive out the
127 scenarios, but are not mandated.
128
129 These scenarios can also be automated, if an appropriate tool exists to
130 allow automation at the UI level. If no such tool exists then it may be
131 possible to automate at the next level in, i.e.: if an MVC design pattern
132 has been used, the level of the Controller.
133
134
135 Programmer-domain examples and behavior
136 ---------------------------------------
137
138 The same principles of examples, using contexts, events and outcomes are
139 used to drive development at the level of abstraction of the programmer, as
140 opposed to the business level. For instance, the following examples
141 describe an aspect of behavior of a list:
142
143 .. code-block:: gherkin
144
145 Scenario: New lists are empty
146 Given a new list
147 then the list should be empty.
148
149 Scenario: Lists with things in them are not empty.
150 Given a new list
151 when we add an object
152 then the list should not be empty.
153
154 Both these examples are required to describe the boolean nature of a list
155 in Python and to derive the benefit of the nature. These examples are
156 usually automated using TDD frameworks. In BDD these examples are often
157 encapsulated in a single method, with the name of the method being a
158 complete description of the behavior. Both examples are required for the
159 code to be valuable, and encapsulating them in this way makes it easy to
160 question, remove or change the behavior.
161
162 For instance as unit tests, the above examples might become:
163
164 .. code-block:: python
165
166 class TestList(object):
167 def test_empty_list_is_false(self):
168 list = []
169 assertEqual(bool(list), False)
170
171 def test_populated_list_is_true(self):
172 list = []
173 list.append('item')
174 assertEqual(bool(list), True)
175
176 .. Other practitioners[who?], particularly in the Ruby community, prefer to split these into two separate examples, based on separate contexts for when the list is empty or has items in. This technique is based on Dave Astels' practice, "One assertion per test"[12].
177 .. 12. http://techblog.daveastels.com/tag/bdd/
178
179 Sometimes the difference between the context, events and outcomes is made more explicit. For instance:
180
181 .. code-block:: python
182
183 class TestWindow(object):
184 def test_window_close(self):
185 # Given
186 window = gui.Window("My Window")
187 frame = gui.Frame(window)
188
189 # When
190 window.close()
191
192 # Then
193 assert_(not frame.isVisible())
194
195 However the example is phrased, the effect describes the behavior of the
196 code in question. For instance, from the examples above one can derive:
197
198 - lists should know when they are empty
199 - window.close() should cause contents to stop being visible
200
201 The description is intended to be useful if the test fails, and to provide
202 documentation of the code's behavior. Once the examples have been written
203 they are then run and the code implemented to make them work in the same
204 way as TDD. The examples then become part of the suite of regression tests.
205
206
207 Using mocks
208 -----------
209
210 BDD proponents claim that the use of "should" and "ensureThat" in BDD
211 examples encourages developers to question whether the responsibilities
212 they're assigning to their classes are appropriate, or whether they can be
213 delegated or moved to another class entirely. Practitioners use an object
214 which is simpler than the collaborating code, and provides the same
215 interface but more predictable behavior. This is injected into the code
216 which needs it, and examples of that code's behavior are written using this
217 object instead of the production version.
218
219 These objects can either be created by hand, or created using a
220 mocking framework such as :pypi:`mock`.
221
222 Questioning responsibilities in this way, and using mocks to fulfill the
223 required roles of collaborating classes, encourages the use of Role-based
224 Interfaces. It also helps to keep the classes small and loosely coupled.
225
226
227 Acknowledgement
228 ---------------
229
230 This text is partially taken from the wikipedia text on `Behavior Driven
231 Development`_ with modifications where appropriate to be more specific to
232 *behave* and Python.
233
234 .. _`Behavior Driven Development`: https://en.wikipedia.org/wiki/Behavior_Driven_Development
235