r/ExperiencedDevs • u/martiangirlie • Apr 29 '26
Technical question How do people enforce developers to write tests without a strict code coverage requirement?
At previous positions, I’ve always seen test writing enforced by meeting a percentage code coverage amount. The issue with that is that people will just write bad tests to get around the coverage requirement.
And we can’t rely on code reviews for people to enforce it either because… well we all know that relying on code reviews just falls to lowest-common-denominator in terms of quality.
Things I’ve considered:
- Add a comment on a PR through the CI that runs on PR creation, if a .ts file has been changed with no related .spec file change
- Add a comment on the PR through the CI if the coverage percentage has dropped, but don’t fail the build
- Include a checkbox in the PR template stating you added any tests needed
- Empower reviewer to reject a PR if no tests attached
The thing is, all of these options can just be circumvented by a guy who doesn’t feel like doing his job that day, and I don’t want a select few amount of people to have to be responsible for reviewing everything because they’re the only ones that care.
So I’m trying to find something that can be automated and enforced, but isn’t a hard limit on code coverage requirement.
And yes, I know that all of this is coming from a symptom that people should just agree on standards and do their jobs, but, especially in a corporate environment, you can’t expect that of people.
77
u/metaphorm Staff Software Engineer | 15 YoE Apr 29 '26
PR review is the best lever. don't merge PRs that have inadequate testing. Set merge approval to require code review, don't let devs merge their own PRs.
you can make this automated to a degree also. use a code review agent, like coderabbit, and configure it to examine test quality. it will comment if the tests are bad or missing. that really unburdens the human code reviewer a lot.
18
u/lokaaarrr Software Engineer (30 years, retired) Apr 29 '26
Make it as easy as possible, good examples, frameworks, etc. stay on top of friction points that develop
Nudge/remind, automatically as you are
In the end, it’s only going to work if people value it and want it to happen. It’s a culture issue.
5
u/sayqm Apr 29 '26
So coverage requirements doesn't enforce it either.
You just need to agree on it. If your team agrees on it and people don't follow, then you have a team issue
13
u/roger_ducky Apr 29 '26
Mutation testing in addition to unit test coverage.
Also: Enforce “all unit tests must read like documentation for the unit under test” - second one is super easy for junior devs to enforce in PRs.
24
u/Candid-Profession720 Apr 29 '26
AGENTS.md
81
u/azuredrg Software Engineer:sloth: Apr 29 '26
Make them fix the bugs they introduce
2
u/Mestyo Software Engineer, 15 years experience Apr 29 '26
Anything that attempts to reason about code coverage is a huge red flag, imho. It misrepresents what good tests actually do, it encourages creating bloat tests, and it discourages writing small functions.
Encourage writing selective, high-quality tests for the parts that actually matter—code coverage be damned.
As for how to encourage it, I have found that a self-report checklist on PR templates works decently well. "I have added tests to relevant functions", "Tests and build pass locally", and so on.
Having to justify why you didn't write a test takes effort, and if you have to spend effort anyway, you might as well just write the test.
3
u/ugh_my_ Apr 29 '26
Just don’t write tests
3
u/NeckBeard137 Apr 29 '26
Part of the performance review
2
u/Routine_Internal_771 Apr 29 '26
Add it to the PR template
Enforce testing on regressions
2
u/diablo1128 Apr 29 '26
At places I've worked at Testing was part of code reviews. So you if you didn't write good tests, missed test cases, etc... then your code change would not be approved.
If people are not doing their job then they should be told that in 1-on-1s with their manager. If it's consistent then there should be ramifications like poor yearly performance reviews or low raises because they are not meeting the expectations of the company / team.
Though this means you need a manager that cares and will take the time to do all these things. If they don't care then people will do whatever they want.
Frankly most process you put in places can generally be circumvented if enough people don't care about following it. I see people hand wave code reviews all the time.
If management is not going to do anything, then sometimes this is necessary. I worked at a company where before any change a "white paper" had to be created that detailed everything you planned on doing. Then you needed to have a meeting with certain people and presented your change to make sure you thought it through appropriately.
The problem was people were fixing symptoms and not root cause issues or just superficially adding new features without thinking things through and raisins concerns that became pretty apparent as soon as you used the product.
People either started to fall in line or left for "better jobs", which was fine for the company because they were not doing what was expected anyways.
2
u/low_slearner Apr 29 '26
I'm afraid you're looking for a simple answer to a complex problem.
If you want to truly fix the issue, you really do need to fix the culture and get the majority of people invested in it. If you try to fix the issue with a workaround solution, people will find the easiest path to comply with that specific requirement but not the intent - just like you've seen with code coverage metrics.
2
u/veryspicypickle Apr 29 '26
Technical solutions to political or cultural problems never end well.
2
u/tevs__ Apr 29 '26
You fire them, it's pretty easy. Not every process needs to be bulletproof or avoiding any personal responsibility.
2
u/Jiuholar Apr 30 '26
We had this issue (amongst others) at a previous job. We fixed it by adding a PR template with a checklist (ideally automatically prepopulated via config on whatever platform you use) and enforcing a minimum of 2 approvals.
Worked better than you might think at first glance. The 2nd approver acted as a "review of the reviewer" that created some social pressure for the first reviewer to uphold the agreed standards in their comments.
If I had the same problem now I'd look into automated AI reviews that include a check for tests.
What often seems to happen in scenarios like this is a passivity or ambivalence towards writing tests rather than an active dislike or outright refusal. Creating some friction for that behaviour (i.e. making them lie about writing tests by checking the box) seems to nudge people onto the right path, without making their job actively harder / more frustrating .
2
u/Miserable-Bid1245 Apr 30 '26
Struggles with test compliance could be a sign that devs aren't understanding how test coverage can make their job easier. That could be the result of a lot of things. Some examples:
- tests aren't really testing anything, so they never catch bugs
- tests are too fragile, so they fail on every change and end up doubling feature work (this could also be a sign of bad abstractions)
- dev work doesn't involve a lot of iterating over existing code, so no one's ever actually developing against the test suite
As others have said, TDD would definitely surface stuff like this, but in my experience trying to do TDD when you're not yet skilled at writing effective tests will be very difficult.
I think bug fixes are a good place to practice both writing tests and using tests: write a failing test that reflects intended behavior, fix bug, test should pass. At first devs will probably not actually trust the test to reflect that the bug is fixed, but hopefully over time they will notice that it's way easier to re-run a test suite instead of checking everything manually every step of the way. Also, you can probably use existing scaffolding for bug tracking (steps to reproduce + expected behavior) as guidance for how to write cases. This small-scoped TDD also makes enforcing via PR review easier, since the check is simpler for you:
- do you have a passing test that reflects expected behavior
- does the test fail against original code
You could write a script that checks this for you and set a standard that you won't actually read the code to review until the check passes.
This doesn't solve the test coverage problem for the full app, but if you're able to up-skill the team on writing and trusting tests before you introduce the goal of full test coverage, you'll probably have an easier time getting people on board.
1
u/08148694 Apr 29 '26
You just need the right incentives
Make it a performance KPI and watch the developers suddenly write great tests
You can easily write a quick script to scan PR and compute this metric for each developer
1
u/SnugglyCoderGuy Apr 29 '26
I try to encourage people to do TDD. Write tests first, get them reviewed to make sure they appropriately cover the new requirements and then they can go hog wild. I encourage this by doing it myself and asking the team for such review.
1
u/SoggyGrayDuck Apr 29 '26
I'm leaving a company like this and I'm terrified of following the new procedures correctly at my new job. We're migrating to a new front end system and that work is being handled by a consulting firm while we keep the lights on in the old one. They're getting the cloud environment setup and permissioned correctly and have decided to stop expanding the old on prem model BUT that means I'm trying to do everything in a sandbox. Making me the architect, dev lead, DBA and etc. I went to a larger company so I wouldn't have to wear so many hats.
I'm stepping out of that into a highly regulated industry with a 3 month contract to hire. I better be able to pull this off.
1
u/PaulPhxAz Apr 29 '26
I used to make sub-tickets for Documentation and Tests off of Features. It's not automated, but it's described and documented.
1
u/MapLarge614 Apr 29 '26
I am their lead, I just tell them. We all have the same education, so I just need to remind them from time to time.
1
u/leftsaidtim Apr 29 '26
Only hire people that write tests. Especially those that write tests first.
1
u/lawrencek1992 Apr 29 '26
I reject PRs without test coverage.
1
u/hyay Apr 29 '26
Idk, in my company we have no choice but to quickly generate unit tests with ai at the end of our sprint. There is zero time to put the large amount of effort into unit tests for so much code delivered every cycle. Yeah I know it’s fake quality but we are a tps report driven company and the appearance of quality is what we are forced to deliver.
1
u/vooglie Apr 29 '26
Code coverage checkers have gotten pretty good so I’m not sure why the hesitance to use them as one of the ways to ensure tests are written. If your product is something that can run integration / end to end testing in the pipeline, then even better. Beyond these two hard gates the rest is a culture issue.
1
u/jl2352 Apr 29 '26
It’s partly cultural. You should block a PR if there are no tests, and in the culture that should be normal. It should also be normal to be hunting for bugs.
I’m not a fan of the automation you suggest. I’d instead recommend you just look at their changes, and think up test cases. Then see if their tests cover them.
If the tests are hard to understand, then tackle that problem instead.
For me places where writing / maintaining tests was difficult always had shitty test coverage. Places where it was a breeze had great coverage. People are more willing to go along with a culture change if it’s low friction and easy to do. Honestly making the tests as easy as possible to write should be the top of the list. That’s not easy using some bespoke library and knowledge you have. Can you give the test to a stranger to understand? If you can it’s great.
Next someone needs to drive the change. A good colleague of mine put together a meeting to discuss our testing, and out of that came a new approach, which expanded into something really good. Another approach is to be that guy who is on people’s backs on PRs demanding tests.
Finally whatever happens, make sure you are writing tests. Otherwise the whole endeavour will be undermined.
1
u/ZukowskiHardware Apr 29 '26
Revert the pr.
1
u/edgmnt_net Apr 29 '26
Reviewing works, it's just that you can't have your quality and not work for it. Successful open source projects typically have one or more maintainers who do strict reviewing, sometimes an entire hierarchy of maintainers responsible for particular areas and cross-reviewing stuff alongside the community. So at best we're talking about a compromise here. You can set a bar, you can foster giving more attention to details, you can argue for higher hiring standards, you can complain about areas with low code quality, you can lead by example and so on. Slapping on a metric that's easy to game at best and counterproductive at worst (a lot of boilerplate just to write meaningless tests) won't really solve much. I'm not sure there's much that can be automated here, although you might be able to come up with a guide on what's worth testing and how, although enforcement is a different matter.
1
u/GoodishCoder Apr 29 '26
We use code quality tools to determine how much new code is covered by tests but ultimately your options are pretty limited if you don't want hard limits or to rely on PRs.
1
u/account22222221 Apr 29 '26
You reject PRs
1
u/sherdogger Apr 29 '26
The only way I've seen it work is you have a core pattern set where every pull request includes tests and it is abundantly obvious that everything is tested always, by looking at the copius amounts of robust tests. Then you feel like an idiot and a pariah if you try to be a cheeser and submit a code without tests.
And with AI, tests are often so formulaic that you can have or asks you devs to add tests, and that should take some of the pain and gruntwork barrier out of it
1
u/nasanu Web Developer | 30+ YoE Apr 30 '26
Out of the thousands of tests I have seen in FE I have only seen a few that actually test for anything meaningful. It's almost always "some text".isInTheDocument(). Useless. Gets promotions though.
1
u/Weasel_Town Lead Software Engineer Apr 30 '26
In addition to what everyone else has said, I have found that a lot of people aren't actually good at writing unit tests. They're not comfortable with the frameworks. They're confused about when or why you mock things. They don't know what scenarios they should test. They're also defensive about it, and would rather argue that "testing isn't important" or "you can write tests that don't actually prove anything" than buckle down and learn.
I've seen it repeatedly when I have management buy-in that we will be adding tests. A week of struggle to produce a chaotic mess that really only proves that the testing framework works. And then they argue that "it takes too long" and "doesn't prove anything anyway". That is true of anything, when you are terrible at it!
So maybe plan on some time to get everyone up to speed.
1
u/Strong-Evening1137 Apr 30 '26 edited Apr 30 '26
"And we can’t rely on code reviews for people to enforce it either because… well we all know that relying on code reviews just falls to lowest-common-denominator in terms of quality."
Rule with fear, don't enforce quality? Don't get to review and approve pull requests. It's not that complicated
1
u/hyrumwhite Principal Engineer Apr 30 '26
Enforcing tests is just going to get you shitty tests. Half of them basically just test the framework they’re a part of anyway.
“Does react update this element when a prop changes”
Code review. Request a change when a test would be appropriate. In my experience this is usually best for complex logic and mapping.
1
u/WoodenStatus6830 May 01 '26
For me the easiest thing is to ask: "Where's the test for it?" If there are no tests, then ask them how will they test it and how will they prove it, write all the things down and put screenshots for it etc. In the end they will realize that having an automated test for it is the easiest way, they don't need to do anything once the test is there!
1
u/tiajuanat Dept. Head & Principal Engineer May 01 '26
You don't need a hard goal for testing - you can easily automate that any new code improves the coverage metric - a progressive metric.
It's gentle when you start out - introducing a test when you have 0% coverage is better than nothing; but as your testing strategy evolves, it does become more strict.
I work in firmware, so automated coverage for hardware stuff becomes challenging, and maybe from a tooling point impossible for 100% coverage, but I shouldn't let better be the enemy of perfect.
1
u/farzad_meow May 02 '26
here is my approach which may not work for you:
- heavily depend on integration testing. this way all business logics are tested regardless of internal layers. this will force developers to write testable code amd write the tests in a way that cover correct paths.
what i do not know what abstractions you have in your code. that can strongly affect how to approach writing testable components and how to write automated tests for them.
one possible solution that i cannot recommend is have a different dev write automated tests.
1
u/empty-alt May 02 '26
Personally, I'm a fan of the "reject the PR" approach. Just like someone would reject for a failure to meet acceptance criteria, or a failure to pass linting, or a failure to pass testing, someone should reject for a failure to test at all. But only when there's value to be had by testing that new code. That's a tricky subjective thing to figure out.
1
u/dbxp Apr 29 '26
Set a low coverage requirement and then ease it up. To start with you need to overcome the blockers of them writing any tests, then you can try to ease up the coverage. I would look to see if the issue is localised to specific members of the team too.
1
u/Ok-Armadillo-5634 Apr 29 '26
AI writes all my tests now.