Summary
The article discusses table-driven testing, a more idiomatic way of writing tests for functions in Go, Python, and PHP.
Abstract
The article titled "Patterns: table-driven testing" showcases a better way to structure tests for improved readability and easier extension. The author uses the example of a function that anonymizes emails in a given text and compares the classical way of testing it with the table-driven way. The classical way involves having a test function for each case, which can lead to duplicated test code and make it harder to read. The table-driven way, on the other hand, involves defining a table with input data and expected outcomes for each case, and then executing the function being tested and asserting its outcome. The author provides examples of how to implement table-driven testing in Go, Python, and PHP.
Opinions
- The author believes that table-driven testing is a more idiomatic way of writing tests for functions in Go, Python, and PHP.
- The author thinks that the classical way of testing can lead to duplicated test code and make it harder to read.
- The author suggests that one test function should be used per function being tested.
- The author recommends using pytest's parametrize feature for Python testing.
- The author advises using data providers for PHP testing.
- The author emphasizes that tests should be easy to read and reason about, not only by the author but also by the people who will review or refactor them in the future.
- The author encourages readers to provide feedback or ask questions about the article on Twitter.