Skip to content

Commit 69b0c19

Browse files
committed
Add a word of warning
1 parent dbcece9 commit 69b0c19

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

rules/0100-Writing-a-kata.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,65 @@ use certain pre-defined values. Those values should be preloaded too. For the
163163
format, either a fixed format string (`printf` style) or a full-fledged wonders
164164
helps you to get rid of a bunch of troubles, like additional whitespace,
165165
ambiguous syntax and so on.
166+
167+
168+
A word of warning
169+
-----------------
170+
171+
Before we dive into tests in the next section, here's a word of warning.
172+
The next section does not replace the documentation of your testing framework.
173+
It gives _general tips_. However, in order to write good tests, a deep
174+
understanding of your testing framework is mandatory. Otherwise, it will
175+
take much time to write those, especially if you don't know the programming
176+
language well enough yet.
177+
178+
Have a look at the following test:
179+
180+
```haskell
181+
main = do
182+
if solution 1 /= 1
183+
then exitFailure
184+
else
185+
if solution 2 /= 1
186+
then exitFailure
187+
else
188+
if solution 3 /= 2
189+
then exitFailure
190+
else
191+
if solution 4 /= 3
192+
then exitFailure
193+
else
194+
if solution 5 /= 3
195+
then exitFailure
196+
else return ()
197+
```
198+
This test checks whether `solution` returns the first five Fibonacci numbers.
199+
It's bad for several reasons:
200+
201+
- it doesn't return any helpful error message if the solution is wrong
202+
- it's not modular
203+
- it's not compatible to the CodeWars testing framework
204+
205+
Another variant, which uses `hspec` and random tests could be written as
206+
207+
```haskell
208+
main = do
209+
rs <- replicateM 100 (randomRIO (1,100))
210+
hspec $ do
211+
describe "fibonacci" $ do
212+
forM_ rs $ \x ->
213+
it ("works for " ++ show x) $
214+
fibonacci x `shouldBe` solution x
215+
```
216+
While better, it doesn't use Hspecs interoperability with QuickCheck:
217+
218+
```haskell
219+
main =
220+
hspec $ do
221+
describe "fibonacci" $ do
222+
prop "works for random numbers" $ \x ->
223+
fibonacci x `shouldBe` solution x
224+
```
225+
226+
So before you dive into the next section, make sure that you have at least
227+
a rough overview of the capabilities of your language's testing framework.

0 commit comments

Comments
 (0)