Software Engineering and Architecture Code Coverage Quality of

  • Slides: 31
Download presentation
Software Engineering and Architecture Code Coverage Quality of your Test Cases (? )

Software Engineering and Architecture Code Coverage Quality of your Test Cases (? )

Black. Box and White. Box • Whitebox testing (glassbox / structural) input output –

Black. Box and White. Box • Whitebox testing (glassbox / structural) input output – If we know the structure of code, we may construct test cases that ensures we run through all parts of it – ensure all ‘structural’ elements are ”working”. CS@AU Henrik Bærbak Christensen 2

Program structures • Basically any program is a combination of only three structures, or

Program structures • Basically any program is a combination of only three structures, or basic primes – sequential: – decision: a switch – iteration: a loop a block of code {. . . } if, switch, . . . while, repeat, do, • WB focus on these basic primes and allow us to – evaluate test sets with respect to their ability to exercise these structures – thus – evaluate quality of test sets (Hm…. ) – and thus judge if a test set should be improved

Adequacy • A necessary result of this focus is on adequacy (Da: Tilstrækkelighed(? ),

Adequacy • A necessary result of this focus is on adequacy (Da: Tilstrækkelighed(? ), dækning) • Example: – A test set T ensures that 100% of all statements in the production code P are executed. – T is statement adequate for P. • More often used term: “T ensures statement coverage for P” – If less than 100% are executed then T is not statement adequate for P.

Criteria • There are numerous adequacy criteria. WB focus on program-based criteria but others

Criteria • There are numerous adequacy criteria. WB focus on program-based criteria but others are useful also in BB and other types of testing. • WB criteria – statement coverage – decision coverage – path coverage, and many more • Other types of criteria – use case coverage (system level) – interface coverage (integration level)

Aspects of WB • WB look at code – Corollary: • WB does not

Aspects of WB • WB look at code – Corollary: • WB does not start until late in the development process • BB can be started earlier than WB – Corollary: • WB is only feasible for smaller UUTs – because the flow graphs explode in large units • BB can be used all the way to system level – Corollary: • WB is expensive for unstable UUTs – because implementation changes invalidate the analysis! • BB survives if the behavior + interface are stable

WB Coverage types • Overall there a number of metrics for coverage: – –

WB Coverage types • Overall there a number of metrics for coverage: – – – statement coverage decision coverage condition coverage decision/condition coverage multiple-condition coverage path coverage • The all relate to the flow graph of code.

Flow graph • The flow graph is simply the route diagrams that has gone

Flow graph • The flow graph is simply the route diagrams that has gone out of fashion. • It defines a graph where nodes are basic primes (block, decision, iteration) and edges are control flow between them (the Program. Counter ).

Example Danish Tax

Example Danish Tax

Source of Complex Algorithms • Danish Topskat • Limitation – Only look at unmarried

Source of Complex Algorithms • Danish Topskat • Limitation – Only look at unmarried person (which simplifies it greatly) – Exercise: complete the analysis CS@AU Henrik Bærbak Christensen 10

Design • Direct – functional – approach • EC analysis? – Booleans • Below

Design • Direct – functional – approach • EC analysis? – Booleans • Below and above ‘bundfradrag’ – 531. 000 for personal income – 45. 800 for capital income true/false • However, we will look at the code for a WB analysis • Exercise: Do the EC analysis and compare… CS@AU Henrik Bærbak Christensen 11

Code and Flow Graph CS@AU Henrik Bærbak Christensen 12

Code and Flow Graph CS@AU Henrik Bærbak Christensen 12

Paths in Flow Graph • We can name paths in the flowgraph by the

Paths in Flow Graph • We can name paths in the flowgraph by the subpaths a c b • E. g – – CS@AU Acd Abe Abd Ace e d Henrik Bærbak Christensen 13

Statement coverage • Statement coverage (SC): • Requires every statement in the program to

Statement coverage • Statement coverage (SC): • Requires every statement in the program to be executed at least once • Exercise: – which path satisfy SC criterion?

Statement coverage • SC criterion is pretty weak. • Path ace is enough. •

Statement coverage • SC criterion is pretty weak. • Path ace is enough. • A single test case suffice

But SC is a Weak Coverage • Easy to introduce an error that our

But SC is a Weak Coverage • Easy to introduce an error that our test case will not find CS@AU Henrik Bærbak Christensen 16

Decision coverage • Decision coverage (branch coverage): • Requires each decision has a true

Decision coverage • Decision coverage (branch coverage): • Requires each decision has a true and false outcome at least once • Decision 1: – pi > TTL • Decision 2 – nci > TTNCIL • Exercise: – which paths satisfy DC criterion?

Decision coverage • DC criterion is better • TC 1: D 1 true D

Decision coverage • DC criterion is better • TC 1: D 1 true D 2 true • TC 2: D 1 false D 2 false – Will find our ‘taxbasis = 100. 000’ bug

Decision coverage • Usually decision coverage satisfy statement coverage. • However, beware of –

Decision coverage • Usually decision coverage satisfy statement coverage. • However, beware of – exception handling code • because the switch is not apparent in the code! – thus exception handling statements are not covered. . .

Decision coverage • But an extremely import ‘bug’ remains in the code that DC’s

Decision coverage • But an extremely import ‘bug’ remains in the code that DC’s does not find? • Which

White. Box Test… • Looks at the code that is • Not the specification

White. Box Test… • Looks at the code that is • Not the specification … • Thus the whole un-implemented part of handling married people is not treated … CS@AU Henrik Bærbak Christensen 21

And… • The other coverage metrics are not considered in SWEA. • And most

And… • The other coverage metrics are not considered in SWEA. • And most Code Coverage tools out there cannot compute them… – Intelli. J’s code coverage tool cannot even compute decision coverage… • From a practical point of view – Complex to compute and make test cases for – I doubt the return on investment, but – a personal gut feeling. . . CS@AU Henrik Bærbak Christensen 22

Ja. Co Java Coverage Tool CS@AU Henrik Bærbak Christensen

Ja. Co Java Coverage Tool CS@AU Henrik Bærbak Christensen

Ja. Co • Measures – Statement coverage (*) • (*) Actually bytecode! – Decision

Ja. Co • Measures – Statement coverage (*) • (*) Actually bytecode! – Decision coverage • Call it ‘branch coverage’ • Exceptions not counted • Green: • Yellow: Covered Partially Cov. – ‘some branches’ covered • Red: CS@AU Not Covered Henrik Bærbak Christensen 24

Our Tax Code CS@AU Henrik Bærbak Christensen 25

Our Tax Code CS@AU Henrik Bærbak Christensen 25

Ja. Co Note: Switches are weird! • Ja. Co measures bytecode coverage! • Switches

Ja. Co Note: Switches are weird! • Ja. Co measures bytecode coverage! • Switches on strings are rewritten to hashcodes • Thus, often yelllow even though the test is actually adequate CS@AU Henrik Bærbak Christensen 26

How to Use • Again – 100% statement coverage tells very little about the

How to Use • Again – 100% statement coverage tells very little about the functional quality of the code!!! • But – 15% coverage tells us that we need a lot more testing !!! • It is a technique to help us find more/better test cases… CS@AU Henrik Bærbak Christensen 27

How to Use • And you also get a nice browsable version of your

How to Use • And you also get a nice browsable version of your code… CS@AU Henrik Bærbak Christensen 28

Beware of… • Intelli. J and Eclipse can generate code for ‘equals’ and ‘hash.

Beware of… • Intelli. J and Eclipse can generate code for ‘equals’ and ‘hash. Code’ • I assume it is correct code! – Thus I do not test them • Annoying low coverage CS@AU Henrik Bærbak Christensen 29

Summary

Summary

Code Coverage Metrics • Statement Coverage – Tests execute every statement at least once

Code Coverage Metrics • Statement Coverage – Tests execute every statement at least once • Decision Coverage – Tests execute every decision to true AND false at least once • Code Coverage Tool – Is nice to review in order to inspect potential code blocks that you have forgotten to write test code for… CS@AU Henrik Bærbak Christensen 31