What Code Is Deliberately Excluded from Test Coverage and Why? (MSR 2021)

andrehoraa 14 views 31 slides Jul 25, 2024
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

Test coverage is largely used to assess test effectiveness. In practice, not all code is equally important for coverage analysis, for instance, code that will not be executed during tests is irrelevant and can actually harm the analysis. Some coverage tools provide support for code exclusion from co...


Slide Content

What Code Is Deliberately Excluded
from Test Coverage and Why?
MSR 2021
Andre Hora

Test Coverage
measures the
percentage of code
that is (un)covered
by tests
2

Identify untested areas of the code
Ensure that frequently changing
code is covered
Facilitate code review
Make sure that tests are not
getting worse over time
3

Codecov
4

But, not all code is equally important
for coverage analysis (eg, debug)
5

But, not all code is equally important
for coverage analysis (eg, debug)
6
•Coverage tools provide native
support to exclude code from
coverage analysis
•Developers can flag the code to
be ignored from coverage

But, not all code is equally important
for coverage analysis (eg, debug)
7
•Coverage tools provide native
support to exclude code from
coverage analysis
•Developers can flag the code to
be ignored from coverage

But, not all code is equally important
for coverage analysis (eg, debug)
8
•Coverage tools provide native
support to exclude code from
coverage analysis
•Developers can flag the code to
be ignored from coverage

But, not all code is equally important
for coverage analysis (eg, debug)
9
•Coverage tools provide native
support to exclude code from
coverage analysis
•Developers can flag the code to
be ignored from coverage
ignored code

10
To do “good”
To do “evil”
!
"
Code exlusion from test
coverage can be used …

11
To do “good”
To do “evil”
!
"
Code exlusion from test
coverage can be used …
•Fine-configurate coverage reports
•Exclude debug code
•etc.

12
To do “good”
To do “evil”
!
"
Code exlusion from test
coverage can be used …
•“Cheat” to increase coverage
•Avoid testing complex code
•etc.
•Fine-configurate coverage reports
•Exclude debug code
•etc.

Better understanding coverage exclusion practices can
reveal novel usage cases that should be fomented by
developers and harmful cases that should be avoided
13

Study Design

Coverage Assessment
15
1

Coverage Assessment
16
1
Case Study Selection
2
•Starting from 68 popular Python projects
•55/68 (80%) rely on Coverage.py
•20/55 (36%) use coverage exclusion

Coverage Assessment
17
1
Case Study Selection
2
•Starting from 68 popular Python projects
•55/68 (80%) rely on Coverage.py
•20/55 (36%) use coverage exclusion
Research Questions
3
•RQ1 (Frequency): How frequent is code excluded from test coverage?
•RQ2 (Time): When is code excluded from test coverage?
•RQ3 (Code): What code is excluded from test coverage?
•RQ4 (Reasons): Why is code excluded from test coverage?

Results

RQ1
Frequency

RQ1 How frequent is code
excluded from test coverage?
20
Code is frequently excluded from test coverage analysis:
we find 534 individual occurrences in 179 source files of the
20 selected projects.

RQ2
Time

RQ2 When is code excluded
from test coverage?
22
Most code is excluded from coverage analysis since its
creation (75%), meaning they are already created using the
exclusion feature. In 25% of the cases, the exclusion
feature is added over time, on the median, 24 days later.

RQ3
Code

RQ3 What code is excluded
from test coverage?
24
Most of the excluded code from test coverage happens in
conditional statements (42%) and exception handling statements
(29%), which are code snippets known to be harder to test.

RQ3 What code is excluded
from test coverage?
25
What specifically?
Non-runnable, debugging-only, defensive code, platform-
specific, and conditional importing.

RQ4
Reasons

RQ4 Why is code excluded
from test coverage?
27

RQ4 Why is code excluded
from test coverage?
28
Untested code: “Bring coverage up to
100%: Just adds a couple ‘# pragma no-
cover’ comments to skip coverage on
lines that already weren’t covered.”
Complexity: “Adds pragma: no
cover to the recursive functions”.
Deprecated code: “Add pragma no
cover to deprecated flags check”
Parallelism: “very rare multi-thread
only event [...] Disable test cover”
Trivial/Safe Code:“Add some no-cover
pragmas on functions that don’t need tests”

RQ4 Why is code excluded
from test coverage?
29
Mostly because it is already untested (22%), low-level (20%), or
complex (15%). Other rationales are related to deprecated/legacy
code, parallelism, trivial/safe code, and non-determinism.
Untested code: “Bring coverage up to
100%: Just adds a couple ‘# pragma no-
cover’ comments to skip coverage on
lines that already weren’t covered.”
Complexity: “Adds pragma: no
cover to the recursive functions”.
Deprecated code: “Add pragma no
cover to deprecated flags check”
Parallelism: “very rare multi-thread
only event [...] Disable test cover”
Trivial/Safe Code:“Add some no-cover
pragmas on functions that don’t need tests”

1.Developers are using coverage exclusion
to avoid testing and yet increasing
coverage, which is not a best practice
2.We need novel techniques to spot biased
coverage reports
3.It is important to propose project
guidelines to enforce explanations when
using the exclusion feature
4.Detect and flag trivial/safe candidates for
coverage exclusion to produce more
accurate test coverage reports
Takeaways

What Code Is Deliberately Excluded
from Test Coverage and Why?
MSR 2021
Andre Hora
Tags