Bennett Garner
1 min readApr 21, 2020

--

Unit testing is interesting because you often need to explain why the expected value is what it is.

Sometimes you need comments, but often you can also do it in code.

Consider:

total_objs = Object.objects.filter(**test_criteria).count()
self.assertEqual(total_objs, 100) # 45 obj_a + 45 obj_b + 10 obj_c

Versus a well documented (in code) test:

total_objs = Object.objects.filter(**test_criteria).count()
expected_value = len(obj_a) + len(obj_b) + len(obj_c)
self.assertEqual(total_objs, expected_value)

It may take a couple extra lines, but being explicit about where values come from is always worth it.

--

--

Bennett Garner
Bennett Garner

Written by Bennett Garner

DeveloperPurpose.com — Build a coding career with meaning and purpose 💻 Top writer in technology ✍️

No responses yet