COMP 103 Testing with JUnit 2016-T2 extras

Slides:



Advertisements
Similar presentations
J-Unit Framework.
Advertisements

JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
ArrayList, Multidimensional Arrays
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
2011-T1 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
JUnit Adam Heath. What is JUnit?  JUnit is a unit testing framework for the Java programming language  It allows developers to swiftly and easily test.
A tool for test-driven development
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington 
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 42 Testing Using JUnit.
CMSC 202 ArrayList Aug 9, 2007.
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
Dept of Computer Science University of Maryland College Park
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
This presentation is created for the course COP4331 at UCF
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Introduction to Analysing Costs
Implementing ArrayList Part 1
Problem Solve Suppose a librarian needs to keep track of all the books in a library. How would we write a program to solve this problem?
Chapter 5 Hierarchies IS-A associations superclasses subclasses
COMP 103 Maps, Stacks Thomas Kuehne 2016-T2 Lecture 5
Junit with.
JUnit Automated Software Testing Framework
Introduction to Software Testing Chapter 3 Test Automation
CMSC 202 ArrayList Aug 9, 2007.
CMSC 202 ArrayList Aug 9, 2007.
Section 3 Graphs & Testing
class PrintOnetoTen { public static void main(String args[]) {
Chapter 50 Testing Using JUnit
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Objects with ArrayLists as Attributes
Presentation transcript:

COMP 103 Testing with JUnit 2016-T2 extras Marcus Frean, Rashina Hoda, and Peter Andreae School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 extras

How can we test a program? 2 How to test a program? Test the program as a whole? Test all classes separately?  both! Testing a class separately Need to create instances of the class and call methods on it May need set up code to fill the fields of the instance objects Should test all methods Should test methods under different situations Should report on the errors it finds  would normally require writing a test program

eg: testing a Collection 3 May use a test program with a main method public class ArrayListTest { public static void main(String args) { UI.println("Testing ArrayList…"); List<String> list = new ArrayList<String>(); if (! list.isEmpty() ) UI.println(" *** New list is not empty"); if (list.size() !=0 ) UI.println(" *** New list has a size != 0 "); for (int i=0; i<20; i++) { list.add(i, "v"+i); } for (int i=0; i<20; i++) { if ( ! list.get(i).equals("v"+i) ) UI.printf(" *** %d'th value of list should be v%d", i, i); if ( list.size() != 20 ) UI.println(" *** List size should be 20 after adding 20 values"); :

JUnit documentation 4

JUnit makes testing easier 5 A special class Assert, with special methods made for testing assertTrue("The new list should be empty.", myList.isEmpty()); assertFalse("After adding, list should not be empty.", myList.isEmpty()); assertEquals("Size should be 1.", myList.size(), 1); assertNull("Item 10 should be null.", myList.get(10)); The 1st argument is the message to display if test fails. test methods can be run automatically BlueJ has dedicated Junit support

The necessary ingredients 6 To define a Junit class we need import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.junit.After; import org.junit.runner.JUnitCore; and then “@Before” before a “setup” method, e.g. make an empty Set “@Test” before each test method We don’t need to use @After, unless we need to “clean up” See ArraySetTestCore.java (and ...Completion.java) for examples

Using JUnit public class ArrayListTest { 7 public class ArrayListTest { private List<String> list; @Before public void initialiseEmptyList() { list = new ArrayList <String>(); } @Test public void testIsEmptyOnEmptyList() { assertTrue("A new list should be empty", list.isEmpty()); } @Test public void testAdd() { for (int i = 0; i < 20; i++) { String value = "v" + i; list.add(value); assertFalse("List should not be empty after add", list.isEmpty()); assertEquals("Size should be larger after add", (i+1), list.size()); assertEquals("Item should be in list", value, list.get(list.size()-1));

BlueJ knows about JUnit tests 8

BlueJ displaying test results 9 test could not be executed test failed