I'm working on a project where I have a method declared as `public boolean checkVehicle(Vehicle vehicle)`. I call this method multiple times with different subclasses of `Vehicle`, like `checkVehicle(car)` for a `Car` object and `checkVehicle(truck)` for a `Truck` object. Since both `Car` and `Truck` extend from `Vehicle`, I'm wondering if I should just create one unit test using a `Vehicle` object, or do I need separate tests for `Car` and `Truck`? I'd appreciate any insights on the best practices for unit testing in this scenario.
3 Answers
If you’re using Java or C#, you can actually parameterize your tests. Check out how to implement 'parameterized tests' in those languages. This would allow you to test both `Car` and `Truck` without duplicating too much code. That said, if specific behaviours for each subclass aren’t needed, then testing just `Vehicle` might suffice.
Consider these questions: Do your tests cover all code paths? Have you tested for reasonable error paths and extremes? For instance, if you're passing strings, are you checking for empty or whitespace strings? If your method takes integers, are you testing beyond the allowed range? Think about these when deciding the number of tests; you might find that one thorough test is enough.
If one test covers all the behaviours you need, then you're good with just one. But if your method behaves differently based on whether the vehicle is a truck or a car, having separate tests is essential.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String