Code Review – Benefits of Writing Tests During Review

programming practicesteamworktestingunit testingworkflows

A colleague of mine came up with an idea that I found interesting.

Wouldn't it be beneficial to write tests during code review, by the person doing the review assuming that we don't do TDD?

For this question assume that this is a purely academic project so there is no life at stake. Moreover the team is 4 people. Everyone knows the language and is familiar with all the tools/libraries/frameworks used and can write tests. So basically people who are not senior fullstack lead ninja engineers but decent coders.

Pros I found:

  1. Encourages deeper understanding of code during review to write meaningful tests.
  2. You could then add code review of those tests done by the author of the code being tested.

Cons I found:

  1. Feedback loop between code writing and testing grows.

EDIT: I know that it won't work well on "normal" web applications. What I had in mind was a corner case where you implement complex, scientific algorithms that require care to the details. Let's assume something like implementing my own graph library, NLP etc. I wonder if the code we are writing is isolated from databases and such but very tough to comprehend wouldn't the added level of control, the other person who needs to understand the source code and do meaningful test, make the whole process less prone to those less obvious bugs that don't crash the application but ultimately make your results rubbish?

Best Answer

Wouldn't it be beneficial to write tests during code review, by the person doing the review?

I have found that a good time to write tests is when you realize you need a test for a situation.

Task switching for computers is expensive - even more-so for humans.

At this point in time, you generally have a good understanding of the requirements and dependencies for the test. So leverage your team's immersion in the problem. If you need to refine your new test in the future, great, you already have the test framework/fixtures in place, and all you need to do is change the part that needs improvement.

If that happens during code review, why not go ahead and do that? I've done that before. I've found that it's better than not, especially if you can do it quickly, and even better if you wouldn't have done it otherwise.

Assuming that we don't do TDD?

Even if you do practice TDD, if you realize you need a test while doing code review, one that you don't have, why not write the test then and there?

Pros

  • You leverage your focus on the code under review.
  • Sometimes code review becomes hang-out and chat time when people aren't into it. Writing a test encourages everyone to more actively think about the code being reviewed.
  • More junior members of the team will have an opportunity to learn from the test-writing experience.
  • You may identify talent on your team that you didn't know you had.

Is it really a con that more tests may lead to more code? If the test was needed, and the code was needed for the test, and now you have it, then that's a good thing.

Caveats

Maybe some of the team needs to be focused on other things. If it causes a distraction from priorities, or your code review goes over schedule, then you need to limit or cut the actual writing of the test out. However, code review can certainly identify tests that need to be written, and perhaps they can at least be stubbed out for the writer to complete later.