1
- Tested testing theories
2
- ------------------------
1
+ # Tested testing theories
3
2
4
3
Tests are the bread and butter of Codewars. Without tests, you couldn't
5
4
check whether the user has solved your kata, so they're very important. Yet
@@ -13,7 +12,7 @@ cannot change the tests in approved katas with more than 500 solutions.
13
12
[ #123 ] : https://github.com/Codewars/codewars.com/issues/123
14
13
15
14
16
- ### Make your test descriptive
15
+ ## Make your test descriptive
17
16
18
17
Let's have a look at the following test:
19
18
@@ -45,7 +44,7 @@ Test.assertEquals(fizzBuzz(15), 'FizzBuzz', "testing on 15")
45
44
This tells the user immediately that their function failed on ` 15 ` .
46
45
47
46
48
- #### Group your tests
47
+ ### Group your tests
49
48
50
49
Furthermore, you should group your tests with ` describe ` and ` it ` . After all,
51
50
a tests _ describes_ the behaviour of something, for example the behaviour
@@ -90,21 +89,47 @@ Test.describe('foo', function(){
90
89
```
91
90
92
91
93
- #### Make errors obvious
92
+ ### Make errors obvious
94
93
95
94
Especially if you use random tests (see below), you want to make sure that the
96
95
user knows _ why_ the test failed. You could print the arguments. You could
97
96
show a hint. Either way, a user shouldn't be left alone in face of an error.
98
97
99
- ### Always test all corner cases of your input domain
98
+
99
+ ### Think of the HTML
100
+
101
+ Keep in mind that every output will be interpreted as HTML. If you want to
102
+ use ` < ` , ` > ` or ` & ` in your error messages or description, make sure to
103
+ escape it---unless you actually want to use HTML tags:
104
+
105
+ Character | [ HTML entity] | Mnemonic
106
+ ----------|---------------|-------------------------------------------
107
+ ` < ` | ` < ` | ** l** esser ** t** han
108
+ ` > ` | ` > ` | ** g** reater ** t** han
109
+ ` & ` | ` & ` | ** amp** ersand
110
+
111
+ All other characters are usually safe to use in UTF8 encoded documents.
112
+
113
+ [ HTML entity ] : http://dev.w3.org/html5/html-author/charref
114
+
115
+
116
+ ## Always test all corner cases of your input domain
100
117
101
118
If you ask the user to create a function that should work for
102
119
` 1 <= N <= 1,000,000 ` , make sure that their function works for * both* 1 and
103
120
1,000,000. If you specified that "negative input should return a monkey",
104
121
make sure that you actually test negative input.
105
122
106
123
107
- ### Use random tests
124
+ ## Use the tools of your testing framework
125
+
126
+ If possible, try to use multiple kinds of assertions, e.g. ` assertEquals `
127
+ and ` assertNotEquals ` , or ` shouldSatisfy ` and ` shouldNotSatisfy ` . That
128
+ way, a cheating user will have some more work to circumvent equality or
129
+ predicate based tests.
130
+
131
+
132
+ ## Use random tests
108
133
109
134
Whenever when you state a kata you also have to write a solution. This is
110
135
great, as you can use exactly that function to check the users code
@@ -116,7 +141,7 @@ sufficient random input is most often harder than creating the kata itself.
116
141
But it is worth every honour.
117
142
118
143
119
- ### Use __ more__ random tests
144
+ ## Use __ more__ random tests
120
145
121
146
While it's nice to have a random test which validates that the user returns
122
147
the same as your hidden solution, there's no guarantee that your solution
@@ -152,7 +177,7 @@ Test.it('returns something valid', function(){
152
177
```
153
178
154
179
155
- ### Hide your solution
180
+ ## Hide your solution
156
181
157
182
If you use random tests, make sure to hide your solution. In Java or C#, this
158
183
includes making your function ` private ` . Haskell doesn't allow mutual imports,
@@ -185,14 +210,14 @@ There are more creative ways to hide/store the function, but that's one way
185
210
at least. Note that all dynamic languages on Codewars (Ruby, Python, CS, JS)
186
211
support this kind of local scopes.
187
212
188
- ### Always have some example tests
213
+ ## Always have some example tests
189
214
190
215
Unless you're creating a puzzle where the user has to find * the answer* ,
191
216
present some example tests. Those tests should also contain the corner
192
217
cases and some easy random tests, e.g. check that the user actually returns
193
218
a number for arbitrary negative numbers.
194
219
195
- ### Handle floating point values the right way
220
+ ## Handle floating point values the right way
196
221
197
222
Whenever the user returns a floating point number, make sure that you take
198
223
floating point behaviour into account. The user might not add the numbers
@@ -210,7 +235,7 @@ want to have something "almost exact" and `1e-7` if I want something
210
235
For more information about floating-point arithmetic, read
211
236
[ What Every Computer Scientist Should Know About Floating-Point Arithmetic] ( https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html ) .
212
237
213
- #### An example of floating point dangers
238
+ ### An example of floating point dangers
214
239
215
240
``` python
216
241
def add_three (a ,b ,c ):
@@ -223,7 +248,7 @@ print(add_three_reference(1e-12,1e-12,1) == add_three(1e-12,1e-12,1))
223
248
# False
224
249
```
225
250
226
- #### Relative error testing
251
+ ### Relative error testing
227
252
228
253
The following example shows one way to check floating point values in a more
229
254
sane way:
@@ -250,14 +275,14 @@ def assertFuzzyEquals(actual, expected, msg=""):
250
275
Most language sections contain their equivalent and use ` expect ` or a similar
251
276
function of their test framework.
252
277
253
- #### Consider integral tests
278
+ ### Consider integral tests
254
279
255
280
If your number is guaranteed to be an integral number, use ` int ` , ` long `
256
281
or your language's equivalent instead of floating point numbers. However, keep
257
282
in mind that all values, either input, output, or intermediate should fit in
258
283
this type, otherwise you might encounter overflow or underflow issues.
259
284
260
- ### Fix broken tests early and fix them right
285
+ ## Fix broken tests early and fix them right
261
286
262
287
While [ #163 ] ( https://github.com/Codewars/codewars.com/issues/163 ) will give you
263
288
the tools to fix a kata even late, you should take every reported test issue by
@@ -277,17 +302,17 @@ might get rejected.
277
302
278
303
So what can you do?
279
304
280
- #### Change description to address the bug
305
+ ### Change description to address the bug
281
306
You should definitely tell your users if your kata is broken. Especially if your
282
307
tests have a defect in only one particular language. It's not the best you can
283
308
do, but as long as #163 is still there, it might be the only thing you can do.
284
309
285
- #### Reject old solutions, they didn't follow the description
310
+ ### Reject old solutions, they didn't follow the description
286
311
This is fine as long as there haven't been many solutions. If your kata has been
287
312
solved by hundreds, it might seem a little bit unfair, but correct tests are
288
313
a little bit more important.
289
314
290
- #### Experimental solutions
315
+ ### Experimental solutions
291
316
There are also other alternatives:
292
317
293
318
- check the time: if the kata solution is submitted after a certain point in
0 commit comments