Download Testing - Programming Language Technologies and Paradigms | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Testing
Feb. 15, 2007
Some slides adapted from FSE’98 Tutorial by Michal Young and
Mauro Pezze’
Testing
Execute program on sample input data
– Check if output correct (acceptable)
Goals
– Increase confidence program works correctly
• Acceptance Testing
– Find bugs in program
• Debug Testing
Example (Black Box)?
% java TestServlet HelloWorld /FooBar/Test
out
Hello /FooBar/TestContent-Type: text/plainHTTP/1.0 200
% diff out expectedOutput
Sorting
Program
Sorting
Program
A Real Testing Example
SPECS:
of numbers;Takes a list
returns a
sorted list.
Just a list.
Repeated entry.A sorted list.
Empty list.
Negative numbers.
Test Cases
Output
Output
Output
Output
Output
Limitations of Testing
Program runs on (very small)
subset
of input data
– Exhaustive testing usually impossible
• Too large input space (possibly infinite)
Many situations hard to test
– Inadequate test environment (e.g., lack of hardware)– Hard-to-reach states (e.g., error states)– Parallel code (due to non-determinism)
Testing cannot prove absence of bugs
– Especially a problem in security
Black Box Testing
Pick subcomponent of program
– Internals of component not considered
Give it inputs
Compare against expected outputs
inputs
outputs
being testedsubcomponent
Is it correct?
oracle
Black Box Testing
Pick subcomponent of program
– Internals of component not considered
Give it inputs
Compare against expected outputs
– Depends on the software specification …– But how do I know what the expected outputs are?
The Test Case Generation Problem
What tests will show that my program works?
– Must consider “operational scenarios”
• What is the correct action or output?• What is legitimate input?
– Must consider “abnormal behaviors” as well
behaviors of my program have been tested?How can I make sure that all of the important
– Usually, you can’t!
White/Glass Box Testing
Pick subcomponent of program
Give it inputs
– Based on component code
• If you don’t execute the code, you don’t know
whether or not it works
Compare against correct outputs (properties)
inputs
outputs
being testedsubcomponent
Is it correct?
oracle
Statement Coverage
Faults handling positive values of a[i] not revealed One test case (n=1, a[0]=-7, x=9) covers all statements
{int select(int[] a, int n, int x)
{while (i<n && a[i] <x)int i=0;
if (a[i]<0)
a[i] = - a[i];
i++;
return 1;}
i++
i<n and a[i] <x
a[i]<
a[i] = - a[i];
return 1
true
false
true
false
i=
Branch Coverage
i=
i<n and a[i] <x
a[i]<
a[i] = - a[i];
return 1
true
false
true
false
Faults exiting the loop with a[i] <x not revealedFaults handling positive values of a[i] revealed.Must add test case (n=1, a[0]=7, x=9) to cover false branch of if
{int select(int[] a, int n, int x)
{while (i<n && a[i] <x)int i=0;
if (a[i]<0)
a[i] = - a[i];
i++;
return 1;}
i++
Condition Coverage
i=
i<n and a[i] <x
a[i]<
a[i] = - a[i];
return 1
true
false
true
false
Faults that arise after several loop iterations not revealed.Must add tests that cause loop to exit for a value greater than X.Both i<n and a[i]<x must be false and true for different tests.
{int select(int[] a, int n, int x)
{while (i<n && a[i] <x)int i=0;
if (a[i]<0)
a[i] = - a[i];
i++;
return 1;}
i++
Structural Coverage Testing
Adequacy criteria
– If significant parts of program structure are not tested,
testing is surely inadequate
Control flow coverage criteria
– Condition coverage– Branch (edge) coverage– Statement (node, basic block) coverage
and the inadequateAttempted compromise between the impossible
Granularity of Tests
Unit testing
– Individual components of a program are tested
• Processes of a distributed system• Classes/packages• Methods
Integration testing
– Test case inputs to subsystem, multiple subsystems, or
the whole program, and outputs examined
White/Glass Box vs. Black Box
• Black box
– scales up– depends on spec
• different techniques at
different granularity levels
– cannot reveal code coverage
problems
• same specification
different modulesimplemented with
• White box
– depends on control or data
flow coverage
– does not scale up
• mostly applicable at unit
leveland integration testing
– cannot reveal missing path
errors
• part of the specification
that is not implemented
Testing Activities
Test case execution is only a part of the process
Must also consider
– Test result evaluation– Test case generation
Planning is essential
– To coordinate complementary analysis and testing– To build a testable product– To choose appropriate techniques at each stage– To achieve early and continuous visibility
Example
SXEOLFFODVV0DWK^
SXEOLFFODVV0DWK^
SXEOLFFODVV0DWK^
SXEOLFFODVV0DWK^
SULYDWHSULYDWHSULYDWHSULYDWHLQW
LQWLQWLQW QXPEHU
QXPEHUQXPEHUQXPEHU
SULYDWHSULYDWHSULYDWHSULYDWHLQW
LQWLQWLQW QXP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
^$`
^`
^$`
^`
LQWLQWLQWLQW H[SHFWHG
H[SHFWHG H[SHFWHG
H[SHFWHG
DVVHUW(TXDOVH[SHFWHGDVVHUW(TXDOVH[SHFWHGDVVHUW(TXDOVH[SHFWHGDVVHUW(TXDOVH[SHFWHGWHVW9DUPXOWLSO\
WHVW9DUPXOWLSO\WHVW9DUPXOWLSO\WHVW9DUPXOWLSO\
SXEOLFYRLGSXEOLFYRLGSXEOLFYRLGSXEOLFYRLGWHVWGLYLGH````
WHVWGLYLGHWHVWGLYLGHWHVWGLYLGH^
^^^
^`
^`
^`
^`
^`
^`
^`
^`
DVVHUW(TXDOVQHZDVVHUW(TXDOVQHZDVVHUW(TXDOVQHZDVVHUW(TXDOVQHZ 'RXEOHH[SHFWHGGRXEOHH[SHFWHG GRXEOHH[SHFWHG GRXEOHH[SHFWHG GRXEOHH[SHFWHG
'RXEOHH[SHFWHG 'RXEOHH[SHFWHG
'RXEOHH[SHFWHGQHZ
QHZQHZQHZ
SXEOLFYRLGSXEOLFYRLGSXEOLFYRLGSXEOLFYRLGWHVWPRG````'RXEOHWHVW9DUGLYLGH'RXEOHWHVW9DUGLYLGH'RXEOHWHVW9DUGLYLGH'RXEOHWHVW9DUGLYLGH
WHVWPRGWHVWPRGWHVWPRG^
^^^
^(`
^`
^(`
^`
^(`
^`
^(`
^`
LQWLQWLQWLQW H[SHFWHG
H[SHFWHG H[SHFWHG
H[SHFWHG
DVVHUW(TXDOVH[SHFWHGDVVHUW(TXDOVH[SHFWHGDVVHUW(TXDOVH[SHFWHGDVVHUW(TXDOVH[SHFWHGWHVW9DUPRG
WHVW9DUPRGWHVW9DUPRGWHVW9DUPRG
To Execute Tests within a Class
Pick a Test Runner:
– junit.awtui.TestRunner
– Graphical
– junit.swingui.TestRunner
– Graphical
– junit.textui.TestRunner
– Textual
Invoke on the test case class
OK (2 tests)Time: 0.03.. > java junit.textui.TestRunner ListTest
Junit
Components
Test cases (class
TestCase
– Can reuse test case setup (optional)– Individual tests
Test suites (class
TestSuite
– Test case container
Test runner (various classes)
– Executes test suites and presents results
Each Test Has Three Parts
Code that creates test objects
– Create a subclass of junit.framework.TestCase
Code that executes the test
– Override the method runTest() (which executes the test)
Code that verifies the result
– E.g., use junit.framework.assertTrue() to check results
(throws exception is test fails)
Setup/Teardown
Creating objects for each test insufficient
– Instead, group setup (and teardown) code in one place– Setup overhead grows as number of tests grows
and reuse
junit.framework.TestCase.run() executes test case:
– public void
run
() { setUp(); runTest();
tearDown(); }
• Do not override this method!
– Put cleanup code in tearDown() method– Put setup code in setUp() method
More Asserts
Junit has several different tests
– assertNotSame(o1, o2) -- assert o1 != o2– assertSame(o1, o2) -- assert o1==o2– assertNull(o) -- assert o == null– assertNotNull(o) -- assert o != null– assertEquals(o1, o2) -- assert that o1.equals(o2)– assertFalse(b) -- asserts that b is false– assertTrue(b) -- asserts that b is true
Using a Test Suite
Test runners will use static suite() method
If no suite() method, suite selected automatically
– Every method that is
public
, returns
void
, takes no
arguments, and begins with “test”
– This is the way to go – for project 2, use this style
Then use junit.*.TestRunner TestClass
– Or use DrJava, Eclipse, etc