SlideShare a Scribd company logo
CONCEPTS OF SOFTWARE
ENGINEERING
Testing
Dr. Abdul Karim ABED
Outline
 Testing
 What is Unit Testing
 JUnit 4.x
 Junit in Netbeans
 Test fixtures
 Assertions
 Acceptance Test
Testing
 In general, Testing software is operating the
software under controlled conditions, to (1)
verify that it behaves “as specified”; (2) to
detect errors, and (3) to validate that what has
been specified is what the user actually
wanted.
Testing
 In XP, Testing is done consistently throughout
the process. Programmers design the tests
first and then write the software to fulfill the
requirements of the test. The customer also
provides acceptance tests at each stage to
ensure the desired results are achieved.
XP Process
Pair-up
Select Task
Quick Design
Write Test
Code Refactor
Integrate
Go Home
An XP developer's daily routine
Testing
 Continuous Testing: Before programmers
add a feature, they write a test for it. When the
suite runs, the job is done. Tests in XP come in
two basic flavors.
 Unit Tests are automated tests written by the
developers to test functionality as they write it.
Each unit test typically tests only a single class, or
a small cluster of classes. Unit tests are typically
written using a unit testing framework, such as
JUnit.
Testing
 Acceptance Tests (also known as Functional
Tests) are specified by the customer to test that
the overall system is functioning as specified.
Acceptance tests typically test the entire system,
or some large chunk of it. When all the
acceptance tests pass for a given user story, that
story is considered complete.
 At the very least, an acceptance test could
consist of a script of user interface actions and
expected results that a human can run. Ideally
acceptance tests should be automated, either
using the unit testing framework, or a separate
acceptance testing framework.
8
+ system
increment
Prioritized
functionalities
Write
acceptance
tests
Execute
acceptance
tests
Write
and
execute
unit tests
“Executed after the development”
“Written before”
Iterative Software development
XP Testing
 Every piece of code has a set of automated unit
tests, which are released into the code repository
along with the code.
 The programmers write the unit tests before they
write the code, then add unit tests whenever one
is found to be missing.
 No modification or refactoring of code is
complete until 100% of the unit tests have run
successfully.
XP Testing
 Acceptance tests validate larger blocks of
system functionality, such as user stories.
 When all the acceptance tests pass for a
given user story, that story is considered
complete.
Test-driven development
 Test first: before adding a feature, write a
test for it!
 If code has no automated test case, it is
assumed it does not work
 When the complete test suite passes 100%,
the feature is accepted
What is Unit Testing
 Method of testing that verifies the individual
units of the code is working properly.
 Test the smallest unit in source code
Unit Tests
 Unit Tests automate testing of functionality as
developers write it
 Each unit test typically testing framework, such as
JUnit (xUnit)
 Experiments show that test-driven development
reduces debugging time
 Increases confidence that new features work, and
work with everything
 If a bug is discovered during development, add a
test case to make sure it doesn’t come back!
 tests only a single class, or a small cluster of
classes
Testing with JUnit
 Junit is a unit test environment for Java
programs developed by Erich Gamma and
Kent Beck.
Writing test cases
Executing test cases
Pass/fail? (expected result = obtained
result?)
How to write tests?
All tests must be pass/fail style tests.
A bad test output:
Now Testing Person.java:
First Name: Tanner
Last Name: Burke
Age: 1
A good test output:
Now Testing Person.java:
Failure: Expected Age 2, but was 1 instead.
JUnit 4.x
JUnit is a simple “testing framework” that
provides:
 Annotations for marking methods as tests
 Annotations for marking methods that setting
up and cleaning up test data (“fixtures”)
 methods for making assertions
Simple JUnit
 Create test class and test case.
 Use an assert method for ensuring method
output
 assertEquals()
 assertTrue()
 assertNotNull()
 Can be invoked manually by running the test
class or automated by using ant script
How to write JUnit-based
testing code (Minimum)
Create a Java class
package Test;
public class MyClass
{
public int multiply(int x, int y) {
return x / y;
}
}
How to write JUnit-based
testing code (Minimum)
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MyClassTest
{
@Test
public void testMultiply() /* must use test*/
{
MyClass tester = new MyClass();
assertEquals("Result", 50, tester.multiply(10, 5)); }
}
How to write JUnit-based
testing code (Minimum)
 Run your test
 The test should be failing (indicated via a red
bar). With the following statement:
 Java.lang.AssertionError: Result expected
<50> but was: <2>
Junit in Netbeans
 You don’t need to load the jar into netbeans
project.
 By default the jar is embedded in test libarary
folder
 And also netbeans has test class and test
case code generation menu
Lets Do The Code
 Lets start with heating up our Netbeans 6.1
and create new java project.
 Make a simple class having both return valued
and void method.
 Let the return valued method do simple
process for example addition or substraction.
 Just print something in the void method.
SimpleMath.java
Create Unit Test
 Choose this menu in netbeans
 Tools > Create Junit Test
 Or just simply press Ctrl + Shift + U.
 A window dialogue will appear, choose
suitable options.
 Or you can leave it as is.
 Test case will automatically build inside the
test package folder.
Unit Test Menu
Unit Test Window
SimpleMathTest.java
Unit Testing
 Assign the variable value for the test case.
 Remove the fail() method in return valued
method test.
 Run the test class using Shift + F6.
 See the test result
Test Result
30
public class Add {
public static int sum (int a, int b) {
return a+b;
}
}
Another example (Junit 4.x)
import math.Add;
import org.junit.*;
import static org.junit.Assert.*;
public class Testcase1_add_Junit4 {
@Test
public void testAdd() {
Add add=new Add();
int sum=add.sum(3, 2);
assertEquals(5, sum);
}
}
31
public class Add {
public static int sum (int a, int b) {
return a- b;
}
}
Another example (Junit 4.x)
import math.Add;
import org.junit.*;
import static org.junit.Assert.*;
public class Testcase1_add_Junit4 {
@Test
public void testAdd() {
Add add=new Add();
int sum=add.sum(3, 2);
assertEquals(5, sum);
}
}
32
Another example (Junit 4.x)
import junit.framework.TestCase;
import math.Add;
public class TestCase1_add_Junit3
extends TestCase {
public void testAdd() {… }
public void testAdd_2() {
Add add=new Add();
int sum=add.sum(3, -2);
assertEquals(5, sum);
}
}
Test case 2
Example: Old way vs. new way
 int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
 void testMax() {
int x = max(3, 7);
if (x != 7) {
System.out.println("max(3, 7) gives "
+ x);
}
x = max(3, -7);
if (x != 3) {
System.out.println("max(3, -7) gives "
+ x);
}
}
 public static void main(String[] args) {
new MyClass().testMax();
}
 @Test
void testMax() {
assertEquals(7, max(3, 7));
assertEquals(3, max(3, -7));
}
Test fixtures
 A test fixture is the context in which a test case
runs.
 Typically, test fixtures include:
 Objects or resources that are available for use by
any test case.
 Activities required to make these objects available
and/or resource allocation and de-allocation.
Test fixtures
Before
 You can define one or more methods to be executed
before each test; typically such methods initialize
values, so that each test starts with a fresh set
@Before public void
createOutputFile()
{
output = new File(...);
}
Test fixtures
After
 You can define one or more methods to be
executed after each test; typically such methods
release resources, such as files
@After public void
deleteOutputFile()
{
output.delete();
}
Test fixtures
BeforeClass
 If you wish, you can declare one method to be
executed just once, when the class is first loaded
 This is for expensive setup, such as connecting to a
database
 @BeforeClass
public static void setUpClass() throws
Exception {
// one-time initialization code
}
Test fixtures
AfterClass
 If you wish, you can declare one method to be
executed just once, to do cleanup after all the
tests have been completed
 @AfterClass
public static void tearDownClass() throws
Exception {
// one-time cleanup code
}
Example
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
public class JunitTest1
{
private Collection collection;
private Collection collection;
@BeforeClass public static void oneTimeSetUp()
{ // one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
Example
@AfterClass
public static void oneTimeTearDown )({
//one-time cleanup code
System.out.println(“("@AfterClass - oneTimeTearDown");
}
@Before public void setUp()
{ collection = new ArrayList();
System.out.println("@Before - setUp");
}
@After public void tearDown()
{ collection.clear();
System.out.println("@After - tearDown"); }
Example
@Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
System.out.println("@Test -testEmptyCollection"); }
@Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
System.out.println("@Test - testOneItemCollection");
} }
Example
 Results:
@BeforeClass - oneTimeSetUp
@Before – setUp
@Test - testEmptyCollection
@After – tearDown
@Before - setUp
@Test – testOneItemCollection
@After - tearDown
@AfterClass - oneTimeTearDown
Assertions
 Assertions are defined in the JUnit class
Assert
 If an assertion is true, the method continues
executing.
 If any assertion is false, the method stops
executing at that point, and the result for the test
case will be fail.
 If any other exception is thrown during the
method, the result for the test case will be error.
 If no assertions were violated for the entire
method, the test case will pass.
 All assertion methods are static methods
Assertions
 A test method is annotated with @Test, takes no
parameters, and returns no result
@Test
public void sum() {
assertEquals(15, program.sum(array));
assertTrue(program.min(array) > 0);
}
Assertion
 Boolean conditions are true or false
assertTrue(condition)
assertFalse(condition)
 Objects are null or non-null
assertNull(object)
assertNotNull(object)
 Objects are identical (i.e. two references to the
same object), or not identical.
assertSame(expected, actual)
 true if: expected == actual
assertNotSame(expected, actual)
Assertion methods (2)
 “Equality” of objects:
assertEquals(expected, actual)
 valid if: expected.equals( actual )
 “Equality” of arrays:
assertArrayEquals(expected, actual)
 arrays must have same length
 for each valid value for i, check as appropriate:
assertEquals(expected[i],actual[i])
or
assertArrayEquals(expected[i],actual[i])
Floating point assertions
 When comparing floating point types (double or
float), there is an additional required parameter
delta.
 The assertion evaluates
Math.abs( expected – actual ) <= delta
to avoid problems with round-off errors with floating
point comparisons.
 Example:
assertEquals( aDouble, anotherDouble, 0.0001
)
When is JUnit appropriate?
 As the name implies…
 for unit testing of small amounts of code
 On its own, it is not intended for complex testing,
system testing, etc.
 In the test-driven development methodology, a JUnit
test should be written first (before any code), and
executed.
 Then, implementation code should be written that would be
the minimum code required to get the test to pass – and no
extra functionality.
 Once the code is written, re-execute the test and it should
pass.
 Every time new code is added, re-execute all tests again to
be sure nothing gets broken.
Acceptance Test
 Acceptance tests are created from user
stories. During an iteration the user stories
selected during the iteration planning meeting
will be translated into acceptance tests.
Acceptance Test
Acceptance Tests in XP
 Simple version
 Customer writes the acceptance tests with help from the
developer and the user stories
 Developers write code to make the acceptance tests pass,
reports results to the customer
Acceptance Test
 The customer specifies scenarios to test when
a user story has been correctly implemented.
 A story can have one or many acceptance
tests, what ever it takes to ensure the
functionality works.
Example
 One story that all the groups wrote a test for
was "Allow customer to make a deposit." A
sample acceptance test for this story follows.
Example
 Acceptance test
 Establish account
 Make deposit
 Verify balance is correct
Example
 An example of a more specific version of the
same acceptance test follows.
 Acceptance test
 Establish account
 Account number = 1231234556
 Account owner = John Smith Starting
 balance = 921.53
 Make deposit Deposit
 amount = 51.21
 Verify balance is correct
 Updated balance = 972.74
Acceptance Test
 Acceptance tests are black box system tests.
Each acceptance test represents some
expected result from the system.
Black box
… requirements
Actual output
compared
with
required output
White box
Result
…design
elements
Confirmation
of expected
behavior
Testing Techniques
White Box Testing
 Software testing approach that uses inner
structural and logical properties of the program
for verification and deriving test data
Black-box Testing
 Unlike white-box testing, no knowledge
about the internals of the code
 Test cases are designed based on
specifications
 Example: search for a value in an array
 Postcondition: return value is the index of some
occurrence of the value, or -1 if the value does not
occur in the array
Acceptance Test
 Customers are responsible for verifying the
correctness of the acceptance tests and
reviewing test scores to decide which failed
tests are of highest priority
Acceptance Test
 A user story is not considered complete until it
has passed its acceptance tests. This means
that new acceptance tests must be created
each iteration or the development team will
report zero progress.
Benefits to Acceptance Tests
 Can serve as a contract for the
client/developers
 Requires stories are testable
 User stories are understandings; acceptance
tests are requirements the developers must meet
 Client can track progress by observing the
total number of acceptance tests growing and
% of passing tests increasing
 Developers get more confidence that work is
being done and can cross stories off their list
when acceptance tests pass
Sample Acceptance Test
 Writing cash register software
 Acceptance Test: Shopping cart for generating a
receipt
 Create a shopping cart with:
 1 lb. coffee, 3 bags of cough drops, 1 gallon milk
 Prices: Coffee $6/lb, cough drops $2.49/bag, milk
$4.95/gallon
 Verify total is $18.42
 Test might span multiple stories (fill shopping cart,
checkout, view receipt…)
 Other tests might verify sales tax is calculated
correctly, coupons properly discounted, etc.
Acceptance Tests Are Important
 Gives customer some satisfaction that features
are correctly implemented
 Not the same as Unit Test
 Unit tests could pass but acceptance tests fail,
especially if acceptance test requires the
integration of components that were unit-tested

More Related Content

PDF
Unit testing with Junit
PDF
junit-160729073220 eclipse software testing.pdf
PPSX
PPT
05 junit
PDF
Introduction to test automation in java and php
PPTX
Testing with Junit4
PPTX
Introduction to JUnit
PDF
L08 Unit Testing
Unit testing with Junit
junit-160729073220 eclipse software testing.pdf
05 junit
Introduction to test automation in java and php
Testing with Junit4
Introduction to JUnit
L08 Unit Testing

Similar to 8-testing.pptx (20)

PPT
3 j unit
PPS
J unit presentation
PPS
JUnit Presentation
PPTX
Unit Testing in Java
PPTX
The Test way
PDF
JUnit Testing Framework A Complete Guide.pdf
PDF
JUnit Testing Framework A Complete Guide.pdf
PPTX
Java Unit Testing
PPTX
Junit4&testng presentation
PPTX
unit 1 (1).pptx
DOCX
Junit With Eclipse
PDF
JUnit with_mocking
PDF
SE2_Lec 21_ TDD and Junit
PPT
PPT
Junit Interview Questions-ppt
PPT
Intro to junit
PPTX
JUnit- A Unit Testing Framework
PPTX
Test-Driven Development
3 j unit
J unit presentation
JUnit Presentation
Unit Testing in Java
The Test way
JUnit Testing Framework A Complete Guide.pdf
JUnit Testing Framework A Complete Guide.pdf
Java Unit Testing
Junit4&testng presentation
unit 1 (1).pptx
Junit With Eclipse
JUnit with_mocking
SE2_Lec 21_ TDD and Junit
Junit Interview Questions-ppt
Intro to junit
JUnit- A Unit Testing Framework
Test-Driven Development
Ad

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
iTop VPN Crack Latest Version Full Key 2025
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Website Design Services for Small Businesses.pdf
PDF
AutoCAD Professional Crack 2025 With License Key
PDF
Designing Intelligence for the Shop Floor.pdf
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
iTop VPN Crack Latest Version Full Key 2025
Reimagine Home Health with the Power of Agentic AI​
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
17 Powerful Integrations Your Next-Gen MLM Software Needs
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Computer Software and OS of computer science of grade 11.pptx
Oracle Fusion HCM Cloud Demo for Beginners
Advanced SystemCare Ultimate Crack + Portable (2025)
Autodesk AutoCAD Crack Free Download 2025
Monitoring Stack: Grafana, Loki & Promtail
Website Design Services for Small Businesses.pdf
AutoCAD Professional Crack 2025 With License Key
Designing Intelligence for the Shop Floor.pdf
Ad

8-testing.pptx

  • 2. Outline  Testing  What is Unit Testing  JUnit 4.x  Junit in Netbeans  Test fixtures  Assertions  Acceptance Test
  • 3. Testing  In general, Testing software is operating the software under controlled conditions, to (1) verify that it behaves “as specified”; (2) to detect errors, and (3) to validate that what has been specified is what the user actually wanted.
  • 4. Testing  In XP, Testing is done consistently throughout the process. Programmers design the tests first and then write the software to fulfill the requirements of the test. The customer also provides acceptance tests at each stage to ensure the desired results are achieved.
  • 5. XP Process Pair-up Select Task Quick Design Write Test Code Refactor Integrate Go Home An XP developer's daily routine
  • 6. Testing  Continuous Testing: Before programmers add a feature, they write a test for it. When the suite runs, the job is done. Tests in XP come in two basic flavors.  Unit Tests are automated tests written by the developers to test functionality as they write it. Each unit test typically tests only a single class, or a small cluster of classes. Unit tests are typically written using a unit testing framework, such as JUnit.
  • 7. Testing  Acceptance Tests (also known as Functional Tests) are specified by the customer to test that the overall system is functioning as specified. Acceptance tests typically test the entire system, or some large chunk of it. When all the acceptance tests pass for a given user story, that story is considered complete.  At the very least, an acceptance test could consist of a script of user interface actions and expected results that a human can run. Ideally acceptance tests should be automated, either using the unit testing framework, or a separate acceptance testing framework.
  • 9. XP Testing  Every piece of code has a set of automated unit tests, which are released into the code repository along with the code.  The programmers write the unit tests before they write the code, then add unit tests whenever one is found to be missing.  No modification or refactoring of code is complete until 100% of the unit tests have run successfully.
  • 10. XP Testing  Acceptance tests validate larger blocks of system functionality, such as user stories.  When all the acceptance tests pass for a given user story, that story is considered complete.
  • 11. Test-driven development  Test first: before adding a feature, write a test for it!  If code has no automated test case, it is assumed it does not work  When the complete test suite passes 100%, the feature is accepted
  • 12. What is Unit Testing  Method of testing that verifies the individual units of the code is working properly.  Test the smallest unit in source code
  • 13. Unit Tests  Unit Tests automate testing of functionality as developers write it  Each unit test typically testing framework, such as JUnit (xUnit)  Experiments show that test-driven development reduces debugging time  Increases confidence that new features work, and work with everything  If a bug is discovered during development, add a test case to make sure it doesn’t come back!  tests only a single class, or a small cluster of classes
  • 14. Testing with JUnit  Junit is a unit test environment for Java programs developed by Erich Gamma and Kent Beck. Writing test cases Executing test cases Pass/fail? (expected result = obtained result?)
  • 15. How to write tests? All tests must be pass/fail style tests. A bad test output: Now Testing Person.java: First Name: Tanner Last Name: Burke Age: 1 A good test output: Now Testing Person.java: Failure: Expected Age 2, but was 1 instead.
  • 16. JUnit 4.x JUnit is a simple “testing framework” that provides:  Annotations for marking methods as tests  Annotations for marking methods that setting up and cleaning up test data (“fixtures”)  methods for making assertions
  • 17. Simple JUnit  Create test class and test case.  Use an assert method for ensuring method output  assertEquals()  assertTrue()  assertNotNull()  Can be invoked manually by running the test class or automated by using ant script
  • 18. How to write JUnit-based testing code (Minimum) Create a Java class package Test; public class MyClass { public int multiply(int x, int y) { return x / y; } }
  • 19. How to write JUnit-based testing code (Minimum) import org.junit.Test; import static org.junit.Assert.assertEquals; public class MyClassTest { @Test public void testMultiply() /* must use test*/ { MyClass tester = new MyClass(); assertEquals("Result", 50, tester.multiply(10, 5)); } }
  • 20. How to write JUnit-based testing code (Minimum)  Run your test  The test should be failing (indicated via a red bar). With the following statement:  Java.lang.AssertionError: Result expected <50> but was: <2>
  • 21. Junit in Netbeans  You don’t need to load the jar into netbeans project.  By default the jar is embedded in test libarary folder  And also netbeans has test class and test case code generation menu
  • 22. Lets Do The Code  Lets start with heating up our Netbeans 6.1 and create new java project.  Make a simple class having both return valued and void method.  Let the return valued method do simple process for example addition or substraction.  Just print something in the void method.
  • 24. Create Unit Test  Choose this menu in netbeans  Tools > Create Junit Test  Or just simply press Ctrl + Shift + U.  A window dialogue will appear, choose suitable options.  Or you can leave it as is.  Test case will automatically build inside the test package folder.
  • 28. Unit Testing  Assign the variable value for the test case.  Remove the fail() method in return valued method test.  Run the test class using Shift + F6.  See the test result
  • 30. 30 public class Add { public static int sum (int a, int b) { return a+b; } } Another example (Junit 4.x) import math.Add; import org.junit.*; import static org.junit.Assert.*; public class Testcase1_add_Junit4 { @Test public void testAdd() { Add add=new Add(); int sum=add.sum(3, 2); assertEquals(5, sum); } }
  • 31. 31 public class Add { public static int sum (int a, int b) { return a- b; } } Another example (Junit 4.x) import math.Add; import org.junit.*; import static org.junit.Assert.*; public class Testcase1_add_Junit4 { @Test public void testAdd() { Add add=new Add(); int sum=add.sum(3, 2); assertEquals(5, sum); } }
  • 32. 32 Another example (Junit 4.x) import junit.framework.TestCase; import math.Add; public class TestCase1_add_Junit3 extends TestCase { public void testAdd() {… } public void testAdd_2() { Add add=new Add(); int sum=add.sum(3, -2); assertEquals(5, sum); } } Test case 2
  • 33. Example: Old way vs. new way  int max(int a, int b) { if (a > b) { return a; } else { return b; } }  void testMax() { int x = max(3, 7); if (x != 7) { System.out.println("max(3, 7) gives " + x); } x = max(3, -7); if (x != 3) { System.out.println("max(3, -7) gives " + x); } }  public static void main(String[] args) { new MyClass().testMax(); }  @Test void testMax() { assertEquals(7, max(3, 7)); assertEquals(3, max(3, -7)); }
  • 34. Test fixtures  A test fixture is the context in which a test case runs.  Typically, test fixtures include:  Objects or resources that are available for use by any test case.  Activities required to make these objects available and/or resource allocation and de-allocation.
  • 35. Test fixtures Before  You can define one or more methods to be executed before each test; typically such methods initialize values, so that each test starts with a fresh set @Before public void createOutputFile() { output = new File(...); }
  • 36. Test fixtures After  You can define one or more methods to be executed after each test; typically such methods release resources, such as files @After public void deleteOutputFile() { output.delete(); }
  • 37. Test fixtures BeforeClass  If you wish, you can declare one method to be executed just once, when the class is first loaded  This is for expensive setup, such as connecting to a database  @BeforeClass public static void setUpClass() throws Exception { // one-time initialization code }
  • 38. Test fixtures AfterClass  If you wish, you can declare one method to be executed just once, to do cleanup after all the tests have been completed  @AfterClass public static void tearDownClass() throws Exception { // one-time cleanup code }
  • 39. Example import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class JunitTest1 { private Collection collection; private Collection collection; @BeforeClass public static void oneTimeSetUp() { // one-time initialization code System.out.println("@BeforeClass - oneTimeSetUp"); }
  • 40. Example @AfterClass public static void oneTimeTearDown )({ //one-time cleanup code System.out.println(“("@AfterClass - oneTimeTearDown"); } @Before public void setUp() { collection = new ArrayList(); System.out.println("@Before - setUp"); } @After public void tearDown() { collection.clear(); System.out.println("@After - tearDown"); }
  • 41. Example @Test public void testEmptyCollection() { assertTrue(collection.isEmpty()); System.out.println("@Test -testEmptyCollection"); } @Test public void testOneItemCollection() { collection.add("itemA"); assertEquals(1, collection.size()); System.out.println("@Test - testOneItemCollection"); } }
  • 42. Example  Results: @BeforeClass - oneTimeSetUp @Before – setUp @Test - testEmptyCollection @After – tearDown @Before - setUp @Test – testOneItemCollection @After - tearDown @AfterClass - oneTimeTearDown
  • 43. Assertions  Assertions are defined in the JUnit class Assert  If an assertion is true, the method continues executing.  If any assertion is false, the method stops executing at that point, and the result for the test case will be fail.  If any other exception is thrown during the method, the result for the test case will be error.  If no assertions were violated for the entire method, the test case will pass.  All assertion methods are static methods
  • 44. Assertions  A test method is annotated with @Test, takes no parameters, and returns no result @Test public void sum() { assertEquals(15, program.sum(array)); assertTrue(program.min(array) > 0); }
  • 45. Assertion  Boolean conditions are true or false assertTrue(condition) assertFalse(condition)  Objects are null or non-null assertNull(object) assertNotNull(object)  Objects are identical (i.e. two references to the same object), or not identical. assertSame(expected, actual)  true if: expected == actual assertNotSame(expected, actual)
  • 46. Assertion methods (2)  “Equality” of objects: assertEquals(expected, actual)  valid if: expected.equals( actual )  “Equality” of arrays: assertArrayEquals(expected, actual)  arrays must have same length  for each valid value for i, check as appropriate: assertEquals(expected[i],actual[i]) or assertArrayEquals(expected[i],actual[i])
  • 47. Floating point assertions  When comparing floating point types (double or float), there is an additional required parameter delta.  The assertion evaluates Math.abs( expected – actual ) <= delta to avoid problems with round-off errors with floating point comparisons.  Example: assertEquals( aDouble, anotherDouble, 0.0001 )
  • 48. When is JUnit appropriate?  As the name implies…  for unit testing of small amounts of code  On its own, it is not intended for complex testing, system testing, etc.  In the test-driven development methodology, a JUnit test should be written first (before any code), and executed.  Then, implementation code should be written that would be the minimum code required to get the test to pass – and no extra functionality.  Once the code is written, re-execute the test and it should pass.  Every time new code is added, re-execute all tests again to be sure nothing gets broken.
  • 49. Acceptance Test  Acceptance tests are created from user stories. During an iteration the user stories selected during the iteration planning meeting will be translated into acceptance tests.
  • 51. Acceptance Tests in XP  Simple version  Customer writes the acceptance tests with help from the developer and the user stories  Developers write code to make the acceptance tests pass, reports results to the customer
  • 52. Acceptance Test  The customer specifies scenarios to test when a user story has been correctly implemented.  A story can have one or many acceptance tests, what ever it takes to ensure the functionality works.
  • 53. Example  One story that all the groups wrote a test for was "Allow customer to make a deposit." A sample acceptance test for this story follows.
  • 54. Example  Acceptance test  Establish account  Make deposit  Verify balance is correct
  • 55. Example  An example of a more specific version of the same acceptance test follows.  Acceptance test  Establish account  Account number = 1231234556  Account owner = John Smith Starting  balance = 921.53  Make deposit Deposit  amount = 51.21  Verify balance is correct  Updated balance = 972.74
  • 56. Acceptance Test  Acceptance tests are black box system tests. Each acceptance test represents some expected result from the system.
  • 57. Black box … requirements Actual output compared with required output White box Result …design elements Confirmation of expected behavior Testing Techniques
  • 58. White Box Testing  Software testing approach that uses inner structural and logical properties of the program for verification and deriving test data
  • 59. Black-box Testing  Unlike white-box testing, no knowledge about the internals of the code  Test cases are designed based on specifications  Example: search for a value in an array  Postcondition: return value is the index of some occurrence of the value, or -1 if the value does not occur in the array
  • 60. Acceptance Test  Customers are responsible for verifying the correctness of the acceptance tests and reviewing test scores to decide which failed tests are of highest priority
  • 61. Acceptance Test  A user story is not considered complete until it has passed its acceptance tests. This means that new acceptance tests must be created each iteration or the development team will report zero progress.
  • 62. Benefits to Acceptance Tests  Can serve as a contract for the client/developers  Requires stories are testable  User stories are understandings; acceptance tests are requirements the developers must meet  Client can track progress by observing the total number of acceptance tests growing and % of passing tests increasing  Developers get more confidence that work is being done and can cross stories off their list when acceptance tests pass
  • 63. Sample Acceptance Test  Writing cash register software  Acceptance Test: Shopping cart for generating a receipt  Create a shopping cart with:  1 lb. coffee, 3 bags of cough drops, 1 gallon milk  Prices: Coffee $6/lb, cough drops $2.49/bag, milk $4.95/gallon  Verify total is $18.42  Test might span multiple stories (fill shopping cart, checkout, view receipt…)  Other tests might verify sales tax is calculated correctly, coupons properly discounted, etc.
  • 64. Acceptance Tests Are Important  Gives customer some satisfaction that features are correctly implemented  Not the same as Unit Test  Unit tests could pass but acceptance tests fail, especially if acceptance test requires the integration of components that were unit-tested