Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Code coverage
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Coverage criteria == To measure what percentage of code has been executed by a [[test suite]], one or more ''coverage criteria'' are used. These are usually defined as rules or requirements, which a test suite must satisfy.<ref>{{cite book | author=Paul Ammann, Jeff Offutt | title=Introduction to Software Testing |publisher=Cambridge University Press| year=2013}}</ref> === Basic coverage criteria === There are a number of coverage criteria, but the main ones are:<ref>{{cite book | author=Glenford J. Myers | title=The Art of Software Testing, 2nd edition |publisher=Wiley | year=2004| isbn=0-471-46912-2}}</ref> * '''Function coverage'''{{snd}}has each function (or [[subroutine]]) in the program been called? * '''Statement coverage'''{{snd}}has each [[statement (computer science)|statement]] in the program been executed? * '''Edge coverage'''{{snd}}has every [[Graph theory|edge]] in the [[control-flow graph]] been executed? ** '''Branch coverage'''{{snd}}has each branch (also called the [[DD-path]]) of each control structure (such as in [[Conditional (programming)|''if'' and ''case'' statements]]) been executed? For example, given an ''if'' statement, have both the ''true'' and ''false'' branches been executed? (This is a subset of edge coverage'''.''') * '''Condition coverage'''{{snd}}has each Boolean sub-expression evaluated both to true and false? (Also called predicate coverage.) For example, consider the following [[C (programming language)|C]] function: <syntaxhighlight lang="cpp"> int foo (int x, int y) { int z = 0; if ((x > 0) && (y > 0)) { z = x; } return z; } </syntaxhighlight> Assume this function is a part of some bigger program and this program was run with some test suite. * ''Function coverage'' will be satisfied if, during this execution, the function <code>foo</code> was called at least once. * ''Statement coverage'' for this function will be satisfied if it was called for example as <code>foo(1,1)</code>, because in this case, every line in the function would be executed—including <code>z = x;</code>. * ''Branch coverage'' will be satisfied by tests calling <code>foo(1,1)</code> and <code>foo(0,1)</code> because, in the first case, both <code>if</code> conditions are met and <code>z = x;</code> is executed, while in the second case, the first condition, <code>(x>0)</code>, is not satisfied, which prevents the execution of <code>z = x;</code>. * ''Condition coverage'' will be satisfied with tests that call <code>foo(1,0)</code>, <code>foo(0,1)</code>, and <code>foo(1,1)</code>. These are necessary because in the first case, <code>(x>0)</code> is evaluated to <code>true</code>, while in the second, it is evaluated to <code>false</code>. At the same time, the first case makes <code>(y>0)</code> <code>false</code>, the second case does not evaluate <code>(y>0)</code> (because of the lazy-evaluation of the Boolean operator), the third case makes it <code>true</code>. In programming languages that do not perform [[short-circuit evaluation]], condition coverage does not necessarily imply branch coverage. For example, consider the following [[Pascal (programming language)|Pascal]] code fragment: <syntaxhighlight lang="pascal"> if a and b then </syntaxhighlight> Condition coverage can be satisfied by two tests: * <code>a=true</code>, <code>b=false</code> * <code>a=false</code>, <code>b=true</code> However, this set of tests does not satisfy branch coverage since neither case will meet the <code>if</code> condition. [[Fault injection]] may be necessary to ensure that all conditions and branches of [[exception handling|exception-handling]] code have adequate coverage during testing. === Modified condition/decision coverage === {{Main|Modified condition/decision coverage}} A combination of function coverage and branch coverage is sometimes also called '''decision coverage'''. This criterion requires that every [[Entry and exit points|point of entry and exit]] in the program has been invoked at least once, and every decision in the program has taken on all possible outcomes at least once. In this context, the decision is a [[Boolean expression]] comprising conditions and zero or more Boolean operators. This definition is not the same as branch coverage,<ref name ="Position Paper CAST10">Position Paper CAST-10 (June 2002). ''[http://www.faa.gov/aircraft/air_cert/design_approvals/air_software/cast/cast_papers/media/cast-10.pdf What is a "Decision" in Application of Modified Condition/Decision Coverage (MC/DC) and Decision Coverage (DC)?]''</ref> however, the term ''decision coverage'' is sometimes used as a synonym for it.<ref name="mathworks">MathWorks. ''[http://www.mathworks.com/help/slvnv/ug/types-of-model-coverage.html Types of Model Coverage.]''</ref> '''Condition/decision coverage''' requires that both decision and condition coverage be satisfied. However, for [[safety-critical]] applications (such as [[avionics software]]) it is often required that '''modified condition/decision coverage (MC/DC)''' be satisfied. This criterion extends condition/decision criteria with requirements that each condition should affect the decision outcome independently. For example, consider the following code: <syntaxhighlight lang="pascal"> if (a or b) and c then </syntaxhighlight> The condition/decision criteria will be satisfied by the following set of tests: {| class="wikitable" |- ! a || b || c |- | true || true || true |- | false || false || false |} However, the above tests set will not satisfy modified condition/decision coverage, since in the first test, the value of 'b' and in the second test the value of 'c' would not influence the output. So, the following test set is needed to satisfy MC/DC: {| class="wikitable" |- ! a || b || c |- | false || true || '''false''' |- | false || '''true''' || '''true''' |- | '''false''' || '''false''' || true |- | '''true''' || false || '''true''' |} === Multiple condition coverage === This criterion requires that all combinations of conditions inside each decision are tested. For example, the code fragment from the previous section will require eight tests: {| class="wikitable" |- ! a || b || c |- | false || false || false |- | false || false || true |- | false || true || false |- | false || true || true |- | true || false || false |- | true || false || true |- | true || true || false |- | true || true || true |} === Parameter value coverage === '''Parameter value coverage''' (PVC) requires that in a method taking parameters, all the common values for such parameters be considered. The idea is that all common possible values for a parameter are tested.<ref>{{cite web| url = http://www.rhyous.com/2012/05/08/unit-testing-with-parameter-value-coverage-pvc/| title = Unit Testing with Parameter Value Coverage (PVC)}}</ref> For example, common values for a string are: 1) [[Null object|null]], 2) empty, 3) whitespace (space, tabs, newline), 4) valid string, 5) invalid string, 6) single-byte string, 7) double-byte string. It may also be appropriate to use very long strings. Failure to test each possible parameter value may result in a bug. Testing only one of these could result in 100% code coverage as each line is covered, but as only one of seven options are tested, there is only 14.2% PVC. === Other coverage criteria === There are further coverage criteria, which are used less often: * '''[[Linear Code Sequence and Jump]] (LCSAJ) coverage''' a.k.a. '''JJ-Path coverage'''{{snd}} has every LCSAJ/JJ-path been executed?<ref name="On the relationship between two control-flow coverage criteria: all JJ-paths and MCDC">M. R. Woodward, M. A. Hennell, "On the relationship between two control-flow coverage criteria: all JJ-paths and MCDC", Information and Software Technology 48 (2006) pp. 433-440</ref> * '''Path coverage'''{{snd}}Has every possible route through a given part of the code been executed? * '''Entry/exit coverage'''{{snd}}Has every possible call and return of the function been executed? * '''Loop coverage'''{{snd}}Has every possible loop been executed zero times, once, and more than once? * '''State coverage'''{{snd}}Has each state in a [[finite-state machine]] been reached and explored? * '''Data-flow coverage'''{{snd}}Has each variable definition and its usage been reached and explored?<ref name="A Survey on Data-Flow Testing">Ting Su, Ke Wu, Weikai Miao, Geguang Pu, Jifeng He, Yuting Chen, and Zhendong Su. "A Survey on Data-Flow Testing". ACM Comput. Surv. 50, 1, Article 5 (March 2017), 35 pages.</ref> [[Safety-critical]] or [[Dependability|dependable]] applications are often required to demonstrate 100% of some form of test coverage. For example, the [[European Cooperation for Space Standardization|ECSS]]-E-ST-40C standard demands 100% statement and decision coverage for two out of four different criticality levels; for the other ones, target coverage values are up to negotiation between supplier and customer.<ref name="ECSS-E-ST-40C">ECSS-E-ST-40C: Space engineering - Software. ECSS Secretariat, ESA-ESTEC. March, 2009</ref> However, setting specific target values - and, in particular, 100% - has been criticized by practitioners for various reasons (cf.<ref name="is 100 percent reasonable">C. Prause, J. Werner, K. Hornig, S. Bosecker, M. Kuhrmann (2017): ''[https://www.researchgate.net/profile/Marco_Kuhrmann/publication/319141355_Is_100_Test_Coverage_a_Reasonable_Requirement_Lessons_Learned_from_a_Space_Software_Project/links/599467faaca272ec9087f82a/Is-100-Test-Coverage-a-Reasonable-Requirement-Lessons-Learned-from-a-Space-Software-Project.pdf Is 100% Test Coverage a Reasonable Requirement? Lessons Learned from a Space Software Project]''. In: PROFES 2017. Springer. Last accessed: 2017-11-17</ref>) [[Martin Fowler (software engineer)|Martin Fowler]] writes: "I would be suspicious of anything like 100% - it would smell of someone writing tests to make the coverage numbers happy, but not thinking about what they are doing".<ref name="fowler blog">Martin Fowler's blog: [https://martinfowler.com/bliki/TestCoverage.html TestCoverage.] Last accessed: 2017-11-17</ref> Some of the coverage criteria above are connected. For instance, path coverage implies decision, statement and entry/exit coverage. Decision coverage implies statement coverage, because every statement is part of a branch. Full path coverage, of the type described above, is usually impractical or impossible. Any module with a succession of <math>n</math> decisions in it can have up to <math>2^n</math> paths within it; loop constructs can result in an infinite number of paths. Many paths may also be infeasible, in that there is no input to the program under test that can cause that particular path to be executed. However, a general-purpose algorithm for identifying infeasible paths has been proven to be impossible (such an algorithm could be used to solve the [[halting problem]]).<ref>Dorf, Richard C.: ''Computers, Software Engineering, and Digital Devices'', Chapter 12, pg. 15. CRC Press, 2006. {{ISBN|0-8493-7340-9}}, {{ISBN|978-0-8493-7340-4}}; via [https://books.google.com/books?id=jykvlTCoksMC&dq=%22infeasible+path%22+%22halting+problem%22&pg=PT386 Google Book Search]</ref> [[Basis path testing]] is for instance a method of achieving complete branch coverage without achieving complete path coverage.<ref name="SrikantShankar2002">{{cite book|author1=Y.N. Srikant|author2=Priti Shankar|title=The Compiler Design Handbook: Optimizations and Machine Code Generation|year=2002|publisher=CRC Press|isbn=978-1-4200-4057-9|page=249}}</ref> Methods for practical path coverage testing instead attempt to identify classes of code paths that differ only in the number of loop executions, and to achieve "basis path" coverage the tester must cover all the path classes.{{citation needed|date=July 2014}}{{clarify|date=July 2014}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)