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 ...
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 ...
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,...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
test-coverage × 77testing × 37
unit-testing × 36
tdd × 8
java × 5
code-quality × 5
integration-tests × 4
test-automation × 3
junit × 3
python × 2
refactoring × 2
code-reviews × 2
mocking × 2
conditions × 2
quality × 2
functional-testing × 2
integration-testing × 2
cyclomatic-complexity × 2
end-to-end × 2
object-oriented × 1
algorithms × 1
javascript × 1
programming-practices × 1
.net × 1
performance × 1