In her talk at Helvetic Ruby 2024, Sarah Lima, a software developer at Toot and maintainer of Factory Bot, discusses common testing antipatterns in software development. She emphasizes that antipatterns may initially seem effective but often lead to regret due to a lack of consideration for the broader context of code behavior. The talk condenses substantial information into key points to help developers enhance their testing practices.
Key Points Discussed:
- Defining Antipatterns: Antipatterns stand in opposition to best practices and can seem beneficial initially, but often lead to undesirable outcomes later.
Positive Expectation in Tests: When writing tests, instead of expressing what should not happen (e.g., expect user.save.not_to be_false
), developers should clarify what they expect to happen positively (e.g., expect user.save.to be true
). It’s acceptable to quickly write tests to fix bugs, but a follow-up refactor for clarity is advisable.
Avoiding False Positives: It’s critical that tests fail for the right reasons. Developers should write tests that fail when an error occurs. Even if not employing Test-Driven Development (TDD), they should create the test to confirm failure before implementing fixes.
Use of let
: While let
could simplify setup, its use may obscure relationships between the test setup and the verification logic, especially as codebases grow. Sarah recommends writing test setups inline for clarity, following the Arrange-Act-Assert pattern.
Test Hooks Concerns: Using test hooks in production code can lead to discrepancies between testing and production behaviors. Instead, developers should aim for structured solutions, such as utilizing specific gems for managing dependencies.
Final Thoughts: The talk emphasizes that a good test suite should be expressive, maintainable, isolated, and reliable. Avoiding testing antipatterns is crucial for better testing outcomes. Sarah concludes by recommending further resources, including the book "Testing Rails" and the Thoughtbot blog, which focuses on testing practices.
This talk serves as a valuable guide for software developers who want to refine their testing strategies and avoid common pitfalls encountered in software testing.