Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you change code that has a unit test against it, which do you change first?

I have a project that I am building that I have to change a method to behave slightly different. I have several unit tests already built against that method, but I will need to add more to cover the new behavior I am going to add to it. Is it good form to change/add those tests first before making the code change, or do you change your code, fix the broken tests and add the new tests to cover the new behavior?

like image 514
Jason Avatar asked Oct 14 '09 21:10

Jason


People also ask

Should you write unit tests before code?

It often makes sense to write the test first and then write as much code as needed to allow the test to pass. Doing this moves towards a practice known as Test-Driven Development (TDD). Bluefruit uses a lot of TDD because it helps us to build the right product without waste and redundancies.

What are the three steps in a unit test?

The idea is to develop a unit test by following these 3 simple steps: Arrange – setup the testing objects and prepare the prerequisites for your test. Act – perform the actual work of the test. Assert – verify the result.

Should unit tests change?

Every time you need to. Tests should be changed if they become inaccurate, or don't follow specs anymore. Tests should be removed if they have become irrelevant.


2 Answers

If you're to follow TDD practices, you should update your tests first. You'll have broken test cases that should hopefully get fixed when you fix your code.

like image 78
Ates Goral Avatar answered Sep 28 '22 02:09

Ates Goral


It is better to update the tests first and let them fail and then go back and update the code until the test passes. A.K.A Test driven development or Test first development.

like image 30
leonm Avatar answered Sep 28 '22 02:09

leonm