You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 51b5681
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: source/exercises/trigrams/trigrams.rst
+56-1Lines changed: 56 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
Trigrams -- Simple Text Manipulation
5
5
====================================
6
6
7
-
.. rubric:: Kata Fourteen: Tom Swift Under Milk Wood
7
+
.. rubric:: Kata Fourteen: Tom Swift Under the Milk Wood
8
8
9
9
Adapted from Dave Thomas's work:
10
10
@@ -140,6 +140,54 @@ This assignment has two parts: the key one is the trigrams exercise itself, but
140
140
141
141
I suggest you write the trigrams part first; it's more interesting :-)
142
142
143
+
Test Driven Development
144
+
-----------------------
145
+
146
+
You've recently learned about unit testing and Test Driven Development (TDD). That's put that to work on this exercise. Remember that the key to TDD is that you first decide on what you need a piece of your code to do, then you write a test to check that it does that, and only then do you write the actual code itself.
147
+
148
+
Because you're new to this, we're going to give you some tests to get you started. You'll find them in: :download:`test_trigrams.py <./test_trigrams.py>`, which should be with this assignment.
149
+
150
+
Running the Tests
151
+
.................
152
+
153
+
To run the tests, use the ``pytest`` test runner: set your working directory the dir with the test file, and run the ``pytest`` command:
154
+
155
+
.. code-block:: bash
156
+
157
+
$ pytest
158
+
======================= test session starts =======================
159
+
platform darwin -- Python 3.8.2, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
ImportError while importing test module '/Users/chris.barker/Personal/UWPCE/Python210CourseMaterials/source/exercises/trigrams/test_trigrams.py'.
166
+
Hint: make sure your test modules/packages have valid Python names.
167
+
Traceback:
168
+
test_trigrams.py:17: in<module>
169
+
import trigrams
170
+
E ModuleNotFoundError: No module named 'trigrams'
171
+
===================== short test summary info =====================
172
+
ERROR test_trigrams.py
173
+
!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!
174
+
======================== 1 error in 0.13s =========================
175
+
176
+
You should have gotten something like that error: it is indicating that the "trigrams" module does not exist -- which makes sense, as you haven't written in yet. So the first step is the create your code file: name it ``trigrams.py`` and put it in the same directory as the ``test_trigrams.py`` file. It can be empty for now. Now try running pytest again, and it should get farther: you'll have a lot of test failures, but the test should actually run. You should get something like:
177
+
178
+
.. code-block:: bash
179
+
180
+
...
181
+
> tris = trigrams.build_trigram(IWISH)
182
+
E AttributeError: module 'trigrams' has no attribute 'build_trigram'
183
+
184
+
test_trigrams.py:29: AttributeError
185
+
====================== short test summary info =======================
========================= 1 failed in 0.15s ==========================
188
+
189
+
You get an Attribute error, as you haven' defined anything in your ``trigrams.py`` file. But now it's time to actually work on the code!
190
+
143
191
trigrams
144
192
--------
145
193
@@ -164,6 +212,13 @@ This produces an (ordered) list of words::
164
212
165
213
Now you've got some words to play with. Once you think you've got it working, try a bit longer piece of text. But this will do for now, and it's small and simple enough that you can immediately see if your code is working.
166
214
215
+
You will find that example in the test file, so we can write tests agains it:
Copy file name to clipboardExpand all lines: source/modules/IteratorsAndGenerators.rst
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -208,7 +208,7 @@ the list -- not so good if memory is tight. Note that they are shallow copies, s
208
208
209
209
Nevertheless, we can do better:
210
210
211
-
The ``itertools`` module has a ``islice()`` (iterable slice) function.
211
+
The ``itertools`` module has an ``islice()`` (iterable slice) function.
212
212
It returns an iterator over a slice of a sequence -- so no more copies:
213
213
214
214
.. code-block:: ipython
@@ -248,9 +248,11 @@ Usually returns the iterator object itself.
248
248
Returns the next item from the container. If there are no further items,
249
249
raises the ``StopIteration`` exception.
250
250
251
+
An *iterable*, on the other hand, must have a ``__iter__`` method that returns an initialized iterator (which may or may not be itself). It doesnot have a ``__next__`` method. So you oculd say that the difference between and iterator and a iterable, is htat iterables do not have ``__next__`` methods: you cannont call ``next()`` on them.
Copy file name to clipboardExpand all lines: source/modules/TestDrivenDevelopment.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Test Driven Development
10
10
"Testing" is any strategy for making sure your code behaves as expected. "Unit Testing" is a particular strategy, that:
11
11
12
12
* is easy to run in an automated fashion.
13
-
* utilized isolated tests for each individual
13
+
* utilized isolated tests for each individual function.
14
14
15
15
"Test Driven Development" (TDD) is a development strategy that integrates the development of unit tests with the code itself. In particular, you write the tests *before* you write the code, which seems pretty backward, but it has some real strengths.
0 commit comments