VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...
ย
Introduction To J unit
1. Company
LOGO
JUnit
Week 8, Session 3
Lecturer : ACB
- STQA IE 31315 -
Institut Teknologi Del
Jl. Sisingamangaraja
Sitoluama, Laguboti 22381
Toba โ SUMUT
http://www.del.ac.id
2. Topics
๏ง What is JUnit?
๏ง Why JUnit framework?
๏ง JUnit mechanics
๏ง Fixtures
๏ง Test suites
๏ง Test runners
๏ง JUnit classes
5/20/2014 STQA/Week09/JUnit 2
3. What is JUnit?
๏ง An unit-testing tool for Java programs
๏ง Supports Test-Driven-Development
๏ง Agile Method
๏ง Goal: Accelerate programming and
increase the quality of code.
๏ง Not for testing GUI
5/20/2014 STQA/Week09/JUnit 3
4. JUnit (contโd)
๏ง Each method under test is treated
as a unit
๏ง The automated testing scripts are
in the form of methods in another
Java source file
๏ The job of the software
developers/testers is then to
write the automated testing
scripts
5/20/2014 STQA/Week09/JUnit 4
5. Why Junit?
๏ง Without JUnit, you will have to use
println() to print out some result:
5/20/2014 STQA/Week09/JUnit 5
6. Why JUnit (contโd)
Without JUnit:
๏ No explicit concept of test passing or
failure
๏ No mechanism to collect results in
structured fashion
๏ No replicability
5/20/2014 STQA/Week09/JUnit 6
7. Key Goals of JUnit
๏ง Easy to use to create tests
๏ง Create tests that retain their value over
time
๏ง Leverage existing tests to write new
ones (reusable)
5/20/2014 STQA/Week09/JUnit 7
8. What does JUnit Provide?
๏ง API for easily creating Java test cases
5/20/2014 STQA/Week09/JUnit 8
9. What does JUnit Provide?
๏ง Comprehensive assertion facilities
๏ verify expected versus actual results
5/20/2014 STQA/Week09/JUnit 9
10. What does JUnit Provide?
๏ง Test runners for running tests
5/20/2014 STQA/Week09/JUnit 10
11. What does JUnit Provide?
๏ง Reporting
5/20/2014 STQA/Week09/JUnit 11
12. What does JUnit Provide?
Aggregation facility (test suites)
๏ง Used to collect all the test cases
๏ง Suites can contain testCases and
testSuites
โ TestSuite(java.lang.Class theClass,
<java.lang.String name>)
โ addTest(Test test) or
addTestSuite(java.lang.Class testClass)
๏ง Suites can have hierarchy
5/20/2014 STQA/Week09/JUnit 12
13. How to write JUnit-based
testing code (Minimum)
๏ง Include JUnit.jar in the classpath
๏ง Define a subclass of TestCase class
๏ง Define one or more public testXXX()
methods in the subclass
๏ง Write assert methods inside the testXXX
methods()
๏ง Optionally define main() to run the
TestCase in standalone mode
5/20/2014 STQA/Week09/JUnit 13
14. Test methods
๏ง Test methods has name pattern: testXXX()
๏ XXX reflects the method of the target class
๏ง Test methods must have no arguments
๏ง Test methods are type of void
5/20/2014 STQA/Week09/JUnit 14
15. Very Simple Test
import junit.framework.TestCase;
public class SimpleTest extends TestCase {
public SimpleTest(String name) {
super(name);
}
// Test code
public void testSomething() {
System.out.println("About to call assertTrue() method...");
assertTrue(4 == (2 * 2));
}
// You don't have to have main() method, use Test runner
public static void main(String[] args){
junit.textui.TestRunner.run(SimpleTest.class);
}
}
5/20/2014 STQA/Week09/JUnit 15
16. Assert Statements
๏ง JUnit Assertions are methods starting with
assert
๏ง Determines the success or failure of a test
๏ง An assert is simply a comparison between
๏ง an expected value and an actual value
๏ง Two variants
๏ assertXXX(...)
๏ assertXXX(String message, ...)
the message is displayed when the
assertXXX() fails
5/20/2014 STQA/Week09/JUnit 16
17. Assert Statements
(contโd)
๏ง Asserts that a condition is true
โ assertTrue(boolean condition)
โ assertTrue(String message, boolean
condition)
๏ง Asserts that a condition is false
โ assertFalse(boolean condition)
โ assertFalse(String message, boolean
condition)
5/20/2014 STQA/Week09/JUnit 17
20. Fixtures
๏ง setUp() and tearDown() used to initialize
and release common test data
๏ง setUp() is run before every test invocation
๏ง tearDown() is run after every test method
5/20/2014 STQA/Week09/JUnit 20