Testing is one of the most crucial ways to prevent bugs from entering production. It can be used to ensure that new code changes do not affect previously defined behaviors. They also help ensure new features will work as expected. For organizations that begin to find stability in their codebase, testing can provide some needed structure to projects.
However, it can be difficult to start a culture around testing. Developers that are used to shipping quickly can see testing as an unnecessary task. This is often due to the fact that adding tests can be extremely time-consuming. Also, when bugs are still found in production, it can devalue the use of testing.
“When a measure becomes a target, it ceases to be a good measure.” Goodhart’s Law
Worse yet, aiming for a code coverage value without considering the state of the team can cause headaches for developers. Instead of writing high-quality tests, they may find ways to cover the code without actually testing anything.
A culture of testing can help to make writing tests a natural part of the development process. Encourage the team to view writing tests as a valuable contribution to the project, rather than a burden. But how can you jumpstart a culture of testing on your team? Here are a few strategies that you can consider:
- Make it easy
- Provide mentoring and guidance
- Set a coverage goal
- Create a testing leaderboard
Make it Easy
It may come as no surprise that making testing easy will help to encourage testing. But how do we actually make it simple?
One of the most challenging parts of testing is having the proper infrastructure. When developers need to spend many cycles figuring out how to get their tests to run, it wastes valuable time they could be spending on coding. Investing in the proper testing infrastructure ahead of time can cut down on headaches later.
When building out testing infrastructure, make sure that data can easily be mocked. Create factories for models that will be often used in tests (e.g. a user
object). If there are third-party APIs used, make sure that examples of how to mock them exist somewhere in the test files. If testing is used to remedy bugs found in production, make it easy to store the inputs to functions so they can be used to create a test.
By putting in the legwork at the start, it will help your team to spend less time later writing tests and more time focusing on building your project.
Provide Mentoring and Guidance
It’s important to note that not every developer has the same background, some have been burnt from not testing, while others have never written a test previously. Sometimes, developers may be hesitant to write tests because they are not sure how to get started or what is expected. Knowing that some members of your team may be inexperienced in writing tests will help drive better remediation steps.
Providing access to classes and workshops is an often overlooked tool. But investing in your developers as a proactive step can help the team grow more quickly. Good classes can also be exciting to developers, encouraging them to take what they’ve learned and apply it.
Furthermore, your senior developers should be champions of the engineering culture. On any team, they are likely to do the most code reviews. Some of the best learnings for less experienced developers happen during the review process. Encourage your seniors to view reviewing tests as important as the code itself. They should be more able to identify edge cases that are left untested.
Set a Coverage Goal
Setting a code coverage goal and making it visible to the team can help build a testing culture. Although we previously mentioned that setting a code coverage goal blindly is a poor idea, there is a correct way to do it.
By setting a specific and measurable code coverage goal, you can give the team a clear target to aim for. Displaying the current code coverage in a prominent location, such as on a dashboard or in a daily stand-up, can help to keep the goal top of mind for the team.
At Codecov, we’ve done the legwork of finding the best path to setting a proper coverage goal. At first, we recommend setting some boundaries around coverage to get your team used to seeing coverage values. As they get used to the idea of writing tests, then and only then is it a good idea to begin looking for a specific code coverage goal.
Create a Testing Leaderboard
Writing tests isn’t glamorous. It’s often overlooked and unrecognized. By offering recognition for achieving high code coverage, you can help to motivate developers to write tests for their code. We’ve outlined a way to find your testing champions using Codecov.
By having a public testing leaderboard, your developers that write tests can be recognized. If need be, you can reward the top leaders with some benefits as a token of appreciation.
Adding your first test
The most difficult part of this process is starting to get tests in the codebase. This makes getting the team on board with starting a testing culture the easiest way to begin. Without the buy-in from your developers, any change is likely to fail. But with their blessing, you’ll be able to instill the culture not only with the current team but with any new developers that may join.
After getting everyone on the same page, you can adopt any of the above strategies to help create a culture of testing and incentivize developers to increase code coverage. This can ultimately lead to a more reliable and high-quality codebase.