Should code review be performed before or after unit tests

code-reviewslanguage-agnostic

I am debating with my colleague on when to perform code review – before or after unit tests. What is the best practice?

Some factors we might need to take into account (there may be more):

  • Size of code change – a big change means more changes will result from the code review. If these changes are big than, if UT was before code review, you'll need to repeat most of your UTs again.
  • Time required to perform unit test
  • Is it new functionality or a bug fix

Best Answer

You should always unit test before doing the code review and here's why

  1. If your code is broken in a way that would be caught by unit tests you will be wasting the other developer's time by getting them involved in the red/green/refactor cycle.
  2. Tests show other developers the intended use of the code which makes it easier to review.
  3. Tests should be reviewed along with the code that is tested in case you are missing test cases or your tests don't work properly.
  4. Tests and code review tend to catch different issues with only a little overlap in issues found. Unit tests don't get annoyed at having to retest code when the reviewer finds issues, developers do get annoyed and probably won't do as well the second time through.

There are probably other reasons but those are the ones I've personally seen and experienced having implemented code review practices within 3 different teams/companies.

Edit Of course the above is for times when code review is a step in your software development process (whether waterfall or agile). If you're working on a particularly large or difficult section of code feel free to get another pair of eyes on it at any point.

Related Topic