Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do when a new feature causes existing unit tests to become invalid?

I'm building a new application and trying to adhere to "test-first" development as faithfully as I can. I'm finding myself in situations where I need to implement/change a feature that has an effect of invalidating a number of existing unit tests. How should I be dealing with this? As I see it, there are 3 options:

  • Update or remove all existing tests to meet the new feature requirements (adding any more as necessary), then implement the feature

  • Implement the feature first, run tests to see failures, and update or remove any failed tests (adding any more as necessary)

  • Add new tests for the new feature, implement the feature, run all tests to see the old ones fail, remove or update the old tests as necessary

The first option adheres to TDD, but can be excruciatingly counter-productive. The second option is the easiest, but you wouldn't be faithfully testing first and may not be properly "covered." The third option is a compromise of both and attractive to a degree, but you run the risk of re-writing a test when you could have just updated an old one.

I don't feel like I have any clear strategy here. What do you do in these situations?

like image 508
Kurt Schindler Avatar asked Aug 18 '09 18:08

Kurt Schindler


People also ask

What are the errors commonly found during unit testing?

Unit testing considerations What errors are commonly found during Unit Testing? (1) Misunderstood or incorrect arithmetic precedence, (2) Mixed mode operations, (3) Incorrect initialization, (4) Precision inaccuracy, (5) Incorrect symbolic representation of an expression.

What makes a unit test self validating?

Tests must be self-validating. This means each test must be able to determine if the actual output is according to the expected output. This determines if the test is passed or failed. There must be no manual interpretation of results.

What should you not do in unit testing?

Never Follow Standard Naming Conventions Unit test code is different from algorithm implementation or business logic, and for this reason, standard naming conventions are not applicable to test classes, methods, and variables.


1 Answers

I would choose one test and change it to require the new feature. If there aren't any obvious candidates, i.e., it is truly new, I would create one. I would then write the code to pass that test. At that point I would run my other tests and notice that some of them fail. At that point, I would revisit each test in turn either correcting the test to reflect the new feature (so it would pass with no other code changes) or update the test with regard to the new feature (which may require some further changes to the code under test).

like image 71
tvanfosson Avatar answered Sep 29 '22 16:09

tvanfosson