🔧 Code Smell 275 - Missing Test Wrong Path
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
Check the happy path to be happy
TL;DR: Ensure you fail the test when no exception is thrown in invalid conditions.
Problems
- Silent faulty tests
- Missing failure condition
- Poor error validation
- Unclear test outcome
- Skipped test logic
Solutions
- Add failure assertion
- Explicit exception validation
- Test invalid actions
- Catch specific errors
- Check all your test paths
- Use mutation testing
Context
When writing tests you expect certain cases to throw exceptions.
If you forget to add a fail condition when no exception is thrown, your test might pass silently.
If breaking some contract doesn't raise an exception, the test will still pass without you noticing the issue.
Always include a failure condition to ensure the test fails when the expected exception isn't thrown.
Sample Code
Wrong
// Test: firing at an already hit position should not be allowed
const game = new Battleship();
game.fireAt("A3");
// First hit
try {
game.fireAt("A3");
// Firing at the same spot
} catch (e) {
console.assert(e.message === 'Position already hit.',
'The error message should indicate the position is already hit.');
}
Right
// Test: firing at an already hit position should not be allowed
const game = new Battleship();
game.fireAt("A3");
// First hit
try {
game.fireAt("A3");
// Firing at the same spot
// THIS LINE IS IMPORTANT
console.assert(false,
'An exception should have been thrown' .
' for firing at the same position.');
// THIS LINE IS IMPORTANT
} catch (e) {
console.assert(e.message === 'Position already hit.',
'The error message should indicate the position is already hit.');
}
Detection
[X] Semi-Automatic
You can detect this smell by looking for try-catch blocks without a failure condition after an action that should throw an exception.
Test cases expecting exceptions should always include assert(false) right after the invalid action.
Tags
- Testing
Level
[X] Intermediate
AI Generation
AI generators can create this smell because they often focus on handling exceptions but might miss adding failure conditions when the exception doesn't occur.
This leads to the silent passing of faulty tests.
AI Detection
AI can detect this smell if you instruct it to check for missing failure assertions after expected exceptions.
It can automatically add the missing condition to ensure tests fail properly when no exception is thrown.
Try Them!
Remember: AI Assistants make lots of mistakes
Without Proper Instructions | With Specific Instructions |
---|---|
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
Gemini | Gemini |
Conclusion
You must include a fail condition when testing invalid actions like firing at the same position in Battleship.
This ensures the test fails if no exception is thrown, preventing silent errors. Always validate the error message and ensure your tests catch valid and invalid behaviors.
Relations
Code Smell 73 - Exceptions for Expected Cases
Maxi Contieri ・ May 31 '21
Code Smell 132 - Exception Try Too Broad
Maxi Contieri ・ May 18 '22
Disclaimer
Code Smells are my opinion.
Credits
Time invested in writing tests and refactoring delivers impressive returns in delivery speed, and Continuous Integration is a core part of making that work in a team setting
Martin Fowler
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
🔧 Code Smell 280 - Spaghetti Code
📈 24.72 Punkte
🔧 Programmierung
🔧 Code Smell 258 - Secrets in Code
📈 24.72 Punkte
🔧 Programmierung
🔧 Code Smell 232 - Reusable Code
📈 24.72 Punkte
🔧 Programmierung
🔧 Code Smell 230 - Schrödinger Code
📈 24.72 Punkte
🔧 Programmierung
🔧 Code Smell 205 - Code in Destructors
📈 24.72 Punkte
🔧 Programmierung
🔧 Code Smell 184 - Exception Arrow Code
📈 24.72 Punkte
🔧 Programmierung
🔧 Code Smell 203 - Irrelevant Test Information
📈 24.63 Punkte
🔧 Programmierung
🔧 Code Smell 255 - Parallel Hierarchies
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 271 - The Hollywood Principle
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 229 - Red Tape
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 254 - Mystery Guest
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 270 - Boolean APIs
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 209 - Side Effects
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 252 - NullCustomer
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 269 - Low-Level Addition
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 200 - Poltergeist
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 251 - Collections Empty
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 268 - Ternary Metaprogramming
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 197 - Gratuitous Context
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 250 - Premature Memoization
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 267 - Objects Aliasing
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 195 - Yoda Conditions
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 249 - Constants as Numbers
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 266 - Collection Aliasing
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 189 - Not Sanitized Input
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 248 - Unreliable Copy
📈 21.46 Punkte
🔧 Programmierung
🔧 Code Smell 265 - Linguistic Confusion
📈 21.46 Punkte
🔧 Programmierung
🐧 Code Smell
📈 21.46 Punkte
🐧 Server