software testing junit testing - team project 2016mmk/teaching/tp/09-junit.pdf · software testing...
TRANSCRIPT
Software Testing – JUnit TestingTeam Project 2016
Presented by Manfred Kerberslides adapted from slides by Christoph Lange
(reusing material from Chris Bowers)
Version: 22 January 2016
1 / 29 c©Manfred Kerber, 2016
Testing Methods
White box
Test the code explicitly, “from within”Cover all possible paths of execution“Does this method do what I intended?”
Black box
Independent of code, “like an outsider”Specification based“Does this software fulfil my requirements?”
Both can be automated
2 / 29 c©Manfred Kerber, 2016
Purpose of black-box testing
User testing
UsabilityUser experienceUser acceptance testing
Security
Ethical hacking – penetration testsIdentify vulnerabilities
Performance
Load, stress testing becomes soak testing
3 / 29 c©Manfred Kerber, 2016
Purpose of white-box testing
“Defensive programming”
Test all paths through the code
e.g. both branches of an if-then-elseTools exist to check test coverage
4 / 29 c©Manfred Kerber, 2016
Test-driven development (1)
A programmer has to be able to demonstrate that his programhas the required properties. If this comes as an afterthought, it isall but certain that he won’t be able to meet this obligation onlyif he allows this obligation to influence his design, there is hopethat he can meet it. Pure a posteriori verification denies you thatwholesome influence and is therefore putting the cart before thehorse ... –E. W. Dijkstra (2000)
You don’t write code with its purpose as an afterthought. You thinkabout it first. Right?
But ... the “purpose” can change frequently.
Need a formal, precise and verifiable way to define purpose.
5 / 29 c©Manfred Kerber, 2016
Test-driven development (2)
Program by intent1 Start by defining a set of test cases
Pay attention to border cases!
2 Write code that passes the tests
public class TestWordStemmer {
public void testStemmer() {
WordStemmer stemmer = new WordStemmer ();
assert stemmer.stem("helping") == "help";
assert stemmer.stem("hungrily") == "hungry";
assert stemmer.stem("friendliness") == "friend";
assert stemmer.stem("play") == "play";
assert stemmer.stem("playing") == "play";
assert stemmer.stem("player") == "play";
// ...
}
}
6 / 29 c©Manfred Kerber, 2016
Test-driven development (3)
Use tests as a template to create code
public class WordStemmer {
public String stem(String word) {
String stemmed_word;
// do stemming
return stemmed_word;
}
}
7 / 29 c©Manfred Kerber, 2016
Design patterns
Abstraction of approach to solving common problems
Can define:
High level strategySoftware architectureWork flow (events)
Finding the right pattern can make defining test cases easier
9 / 29 c©Manfred Kerber, 2016
Unit testing
Unit – small functional part of application that can be testedindependently
A unit could be an individual class or method
A set of test cases are constructed to form a test harness
10 / 29 c©Manfred Kerber, 2016
JUnit testing with Eclipse
JUnit: unit testing library for Java
Integrated into NetBeans/Eclipse/XCode/... IDEs
Separates source code from testing code
IDEs provide lots of nifty inspection tools
Well documented
11 / 29 c©Manfred Kerber, 2016
Live Eclipse example (1)
Outline:
1 creating JUnit test
2 writing test cases3 coding by intent
1 getting it wrong ⇒ test fails2 getting it right ⇒ test passes
12 / 29 c©Manfred Kerber, 2016
JUnit assertions
Statement What it does
fail() Lets the test fail. Useful for checking codeis not reached under certain conditions.
assertTrue(boolCond) Checks that a condition is trueassertEquals(expected, Checks that two values are the same. Not a deepactual, [tolerance]) check, i.e., be careful with non-primitive values.assertNull(object) Check that an object is nullassertNotNull(object) Check that an object is not nullassertSame(expected, Check that both object references are the same.actual)assertNotSame(expected, Check that both object references are not the same.actual)
All methods take an optional first argument message.
Tolerance is for floating point numbers.
25 / 29 c©Manfred Kerber, 2016
JUnit annotations
Statement What it does
@Test Marks a test method@Test(timeout=1000) Test fails if timeout (in ms) exceeded@Test(expException) Test fails if defined exception is not thrown@Before @After Code that should be executed before or
after every test@BeforeClass @AfterClass One-off setup/teardown code (e.g.
database login)@Ignore Ignore this test
Usage:
@Test(timeout=1000)
public void underOneSecond() {}
26 / 29 c©Manfred Kerber, 2016
JUnit can’t test everything
Input/output is hard to test
need to maintain separate test filesor prepare strings/arrays that simulate file contents (if code to betested supports streams)
GUIs are even harder to test
separation of GUI and underlying logic helps (e.g. model-view-controllerdesign pattern)JUnit extensions (e.g. Abbot) facilitate GUI testing
27 / 29 c©Manfred Kerber, 2016
Just getting you started
Some useful resources:
JUnit homepage
http://junit.org
2-page summary (includes Eclipse basics)
https://www.cs.bham.ac.uk/internal/courses/java/msc/resources/junit-
basics.pdf
NetBeans JUnit tutorialhttp://netbeans.org/kb/docs/java/junit-intro.html
Eclipse JUnit tutorial
http://www.vogella.com/articles/JUnit/article.html
28 / 29 c©Manfred Kerber, 2016