Tests should contain as little logic as possible. This helps eliminate the test itself as the culprit when a test fails. As a rule of thumb, avoid control flow or conditional logic inside tests.
As a motivating example, consider the following, buggy Fizzbuzz implementation:
Let’s say we want to test the fizzbuz function with a number of inputs, but we think writing out each assertion by hand is too tedious; instead we write a test like this:
This test repeats the logic error from the implementation, so although the implementation is buggy the test will succeed.
Had we written the “obvious” tests, the test for fizzbuzz(15) would have failed and exposed the logic bug:
Parametrized tests
A better way to reduce boilerplate when writing tests is to use parametrized tests (also known as table-driven tests). The idea is to parametrize tests over a series of inputs and expected outputs. Here’s an example of how to do it with the pytest framework:
.png)
 4 months ago
                                12
                        4 months ago
                                12
                     
  


