118 votes
Accepted

Code coverage highlights unused methods - what should I do?

Delete. Commit. Forget. Rationale: Dead code is dead. By its very description it has no purpose. It may have had a purpose at one point, but that is gone, and so the code should be gone. Version ...
l0b0's user avatar
  • 11.3k
55 votes

Code coverage highlights unused methods - what should I do?

All other answers are based on the assumption that the methods in question are really unused. However, the question didn't specify whether this project is self-contained or a library of some sort. If ...
Zoltan's user avatar
  • 631
47 votes

Unexpected Code Coverage Reduction

The problem I see here is that you have made the code coverage a trigger for build failure. I do believe that code coverage should be something that is routinely reviewed, but as you have experienced,...
Berin Loritsch's user avatar
37 votes

Unexpected Code Coverage Reduction

Have you considered not using code coverage metrics? I'm not going to argue that code coverage isn't something that you should look at. It absolutely is. It's good to keep track of what was covered ...
Thomas Owens's user avatar
  • 80.4k
30 votes
Accepted

Should I write automated unit tests that fail when the code changes?

Your aim should be not to write unit tests that fail when the code changes, but unit tests that fail when the behaviour changes. Here, behaviour means anything that an external caller of the method ...
Ben Aaronson's user avatar
  • 6,903
30 votes

Code coverage highlights unused methods - what should I do?

First check that your code coverage tool is correct. I've had situations where they haven't picked up on methods being called via references to the interface, or if the class is loaded dynamically ...
Ewan's user avatar
  • 72.4k
19 votes

Does Code Coverage improve code quality?

Code coverage tells you how much of your code is covered by tests. It does not tell you much about the quality of the tests. For example, a code coverage of, say, 70% might be obtained by automated ...
Giorgio's user avatar
  • 19.6k
18 votes
Accepted

Unexpected Code Coverage Reduction

You can mitigate the effect to some degree by allowing the relative code coverage to reduce when the total number of uncovered lines also reduces, or when the total number of lines reduces, since this ...
Doc Brown's user avatar
  • 203k
15 votes

Code coverage highlights unused methods - what should I do?

As Java is statically compiled, it should be pretty safe to remove the methods. Removing dead code is always good. There is some probability that there is some crazy reflection system which runs them ...
max630's user avatar
  • 2,557
15 votes

Unexpected Code Coverage Reduction

This is called Simpson’s paradox, and is a well known statistical issue with your approach. You could even construct cases where you refactor and afterwards every single method has a higher coverage, ...
Aganju's user avatar
  • 1,463
14 votes
Accepted

Are unit tests written purely to satisfy a code coverage figure technical debt?

Yes, I'd say these are technical debt. Test code is not different than the code being tested in that it must also be maintained and understood. The fact that you don't understand the point of the ...
Andy's user avatar
  • 2,014
13 votes
Accepted

When does it make sense to include test code in coverage?

I wouldn't say reasons exactly but they do provide a good sense check. All tests being the same (not attributed), you'd expect this to be 100% for a full run. Anything less than this suggests you have ...
Robbie Dee's user avatar
  • 9,737
13 votes

Does Code Coverage improve code quality?

As a reductio ad absurdum: the following test covers 60% of the lines of the function: def abs(x): if x < 0: return -x else: return x assertEquals(abs(-10), 10) whereas ...
RemcoGerlich's user avatar
  • 3,290
13 votes
Accepted

Does Code Coverage improve code quality?

I'm assuming you are referring to a Code Coverage metric in the context of unit testing. If so, I think you indirectly have already answered your question here: First project just used targeted ...
Marcin Kłopotek's user avatar
11 votes
Accepted

If 100% test coverage is possible

There are strategies that can produce deep code coverage metrics. Mutation testing is one example. Roughly speaking, mutation testing ensures that any logical change to the program results in a failed ...
svidgen's user avatar
  • 14.6k
10 votes
Accepted

How does branch coverage handle loops?

Branch coverage and path coverage are distinct concepts. Branch coverage To measure branch coverage, we look for all the points where branching can take place. At each branching point, both branches ...
amon's user avatar
  • 134k
10 votes
Accepted

Should integration test coverage allow for lower unit test code coverage?

If a project has 30% coverage by unit tests, 40% due to integration tests, is it fair to say the total is 70% as so moderately well covered? Only if there is zero overlap between the 40% covered by ...
Jörg W Mittag's user avatar
9 votes

Code coverage highlights unused methods - what should I do?

What would the best approach be? Write unit tests for them, or question why they're there? Deleting code is a good thing. When you can't delete the code, you can certainly mark it as @Deprecated, ...
VoiceOfUnreason's user avatar
9 votes

Code coverage highlights unused methods - what should I do?

A code coverage tool is not all-knowing, all-seeing. Just because your tool claims that the method is not called, that doesn't mean it isn't called. There is reflection, and depending on the language ...
gnasher729's user avatar
  • 43.6k
9 votes

If 100% test coverage is possible

100% test coverage is possible, and despite 100% coverage, your program may still have errors. See shortcomings of test coverage at "Does path coverage guarantee finding all bugs?" Similar to how a ...
Peeyush Kushwaha's user avatar
8 votes
Accepted

How to comprehensively test software that doesn't play well with testing?

I'm afraid there is no easy solution. "write testable code" really is the only way to do it. Writing testable code is non-trivial, and retro-fitting tests is hard. Many of the advances in modern ...
Robin Bennett's user avatar
7 votes
Accepted

What is the method to articulate the tradeoffs between short-fast mocked JUnit tests and slower Integration tests?

Stop, they're both right. Every class deserves unit tests that exercise every line of code. Every integration point deserves to be exercised in an integration test. The only trade off is when you ...
candied_orange's user avatar
7 votes
Accepted

What is the advantage of instruction (byte code) coverage over line and branch coverage?

There is no advantage of byte-code or line coverage over branch coverage. If one element (line or instruction) of a branch is covered, then all elements of that branch are covered as well. If a ...
mouviciel's user avatar
  • 15.5k
7 votes

Rerun unit tests affected by change

No, it wouldn't be a good idea. The point of a test suite is to ensure that development doesn't introduce defects anywhere without having to reason about which parts of the program affect which. If ...
Kilian Foth's user avatar
7 votes

How to comprehensively test software that doesn't play well with testing?

You are not alone Though a redundant mention, anyone reading this question while not having this kind of trouble ought to remember that real-life software development is full of cases of multi-year ...
Vector Zita's user avatar
  • 2,392
6 votes

Should one test generated code?

There is no straight answer to this. There is generated code, and then there is generated code. We could be talking about a bit of framework code, initializations for something you dragged together in ...
Martin Maat's user avatar
  • 18.3k
6 votes
Accepted

How is code coverage measured?

Code coverage tools work in two flavours: either the code is instrumented to record coverage statistics, or the program is run under a debugger or profiler, or tracing mechanism. Coverage ...
amon's user avatar
  • 134k
6 votes

Code coverage highlights unused methods - what should I do?

Depending on the environment the software runs in, you could log if the method is ever called. If it's not called within a suitable period of time, then the method can be safely removed. This is a ...
Jamie Bull's user avatar
5 votes

What is the advantage of instruction (byte code) coverage over line and branch coverage?

The advantage the docs mention is that line coverage counts x = 1; y = 2; z = 4; as one line, but x = 1; y = 2; z = 4; as three. On the plus side, bytecode-based measures would not vary between ...
Jonathan Eunice's user avatar
5 votes
Accepted

How to test local/nested conditional logic branches and variables that cannot be easily manipulated?

You want to use dependency injection. I'll give you an example in C#, since I'm most familiar with it. Your first example: class YourClass { ITimeService _timeService; IOutputService ...
Eternal21's user avatar
  • 1,584

Only top scored, non community-wiki answers of a minimum length are eligible