Making sure developers test their code

How can you make sure that all the developers in your team are testing their code? Coverage metrics are the only way I can objectively measure this. Is there another way?

(Of course, if you really follow TDD, then this should not be a problem. But let's assume that you have some developers who have not received โ€œTDDโ€ yet.)

+4
source share
11 answers

This is more a social problem than a technological one. First, you want unit tests to lead to 100% code coverage, or you can count on less and trust your developers to introduce unit tests where they really matter and where they make sense. You could probably get some kind of system that would do code coverage tests to ensure unit tests cover a certain percentage of code. But then there will still be ways to play the system. And this still will not lead to the creation of code that would be a mistake. Because of things like the stopping problem, it is impossible to ever span a path in code.

+12
source

Run sample coverage reports automatically during the build process. We do this with CruiseControl. Otherwise, you should check what is being tested in the test report.

+6
source

Code coverage tools are almost certainly superior to any special method you might come up with. That is why they exist.

Even developers who receive TDD are far from protected from coverage gaps. Often the code that fixes the error breaks the side test or creates a branch that the original developer did not expect, and the service developer did not understand that this is new.

+3
source

A good way to get written tests is to increase accountability. If a developer needs to explain to someone else why they did not write unit tests, they will most likely do it. Most of the companies I worked with demanded that any proposed fixes to the trunk be reviewed by another developer before the commit, and that the name of the reviewer be included in the commit comments. In this environment, you can tell your team that they should not allow the code to โ€œpassโ€ peer-to-peer code if unit tests are not performed.

You now have a chain of responsibility. If a developer commits a code without calling a reviewer, you can ask them who looked at the code (and since I learned the hard way, I should say โ€œnoโ€ to your boss when this question was asked, this is not fun!). If you become aware that the code is passed without unit tests, you can ask both the developer and the code reviewer why unit tests were not included. The ability to ask this question encourages code reviewers to insist on unit tests.

Another step you can take is to set commit commit in your version control system, which sends an email to the whole team when commit is committed, along with files and even the code that made the commit. This provides a high level of transparency and further encourages developers to "follow the rules." Of course, this only works if it is scaled to the amount your team commits per day.

This is more a psychological solution than a technical solution, but it worked well for me in managing software teams. It is also a bit softer than the rubber hose suggested in another answer. :-)

+2
source

Here we have a test folder with a package structure that reflects the actual code. To test the class, the policy states that it must have an accompanying testing class, with specific recommendations about which method should be tested. (Example: we do not need pure getters and setters for testing)

A quick look at the test folder shows when the class is absent, and an abusive person can be beaten with a rubber hose (or something, depending on your policy).

+1
source

Enter and change a random variable or skip zero somewhere, and you should expect to see red. = D

+1
source

One way to do this is to write a script to search for all the checks for the term "test" or "testfixture" (obviously, depending on your environment). If there is a commit log or an email sent to the manager in which the changes were made, then it would be trivial with your favorite word processing language to scan the code files for signs of unit tests (the Assert keyword is probably the best choice).

If there are no unit tests, then during the next code review, take an example of a recent registration and spend ten minutes talking about possible ways that it might โ€œgo wrongโ€, and how unit tests will be tested quickly found an error.

+1
source

Invite them to regularly submit a report or some screen shot of the results of their unit tests. They can either fake it (most likely, it will take more time than actually doing the tests), or actually doing them.

In the end, you will find out who does not run the tests, they will be those who have errors that could easily be caught using unit tests.

0
source

The problem is as social as technical. If you have developers who โ€œdon't quiteโ€ get โ€œTDD yet,โ€ helping them understand the benefits of TDD may be a better long-term solution than technical measures that โ€œforceโ€ them to write tests because it is necessary. Reluctant developers can easily write tests that meet the criteria for code coverage, and yet are not valuable tests.

0
source

One thing that should be mentioned here is that you need a system to run unit tests regularly. They should be part of your mandate or nightly build system. Just making sure that unit tests are written does not guarantee that you will get value from them.

0
source

Sit down with them and watch what they do. If they are not a unit test, gently remind them.

0
source

All Articles