finepax007
finepax007

Reputation: 651

Is it good practice to fix the existing JUnits to make them pass after addition of new code?

Is it good practice to keep fixing the existing Junits to make them pass after writing a new code ?

Example.

  1. There are 100 JUnit tests which are green
  2. A new if else block is added to support some new functionality or a new helper class is introduced in the target class
  3. Because of this, around 10 existing Junits are failing.

Is it right to refactor existing Junits to make them pass for the added code or Refactor the code in such a way that existing JUnits should pass without any modification?

Upvotes: 3

Views: 85

Answers (2)

nbirla
nbirla

Reputation: 610

The sole purpose of unit tests is to check the functionality of a unit of code. When the code is changed,

  • you can add a new functionality altogether - in which case the existing test cases must not fail if they do, it is due to a bug which was introduced in the new set of code change
  • you can change the existing functionality - in which case the existing test cases might need some changes, for example, you might need to change the data you are passing as an input or Assert statement for changed output.

Upvotes: 1

Mureinik
Mureinik

Reputation: 312404

The fact that the tests now fail means you have changed the behavior of your code. Generally speaking, this can mean one of two things:

  1. If this change in behavior is intentional, the unit test is now wrong, and must be fixed.
  2. If this change was unintentional, it means you introduced a bug - i.e., the unit test is correct, and you must fix the code to make the test pass again.

Upvotes: 3

Related Questions