Testing Acknowledgement: Original slides by Jory Denny.

Slides:



Advertisements
Similar presentations
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Advertisements

Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
JUnit, Revisited 17-Apr-17.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Principles of Object Oriented Programming Practical session 2 – part A.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
Chapter 2 Additional Features of Java Programming.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
C++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Testing code COMP204. How to? “manual” –Tedious, error-prone, not repeatable “automated” by writing code: –Assertions –Junit.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
Automated Testing with PHPUnit. How do you know your code works?
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 42 Testing Using JUnit.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
User-Written Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Test Driven Development (TDD)
(like an array on steroids)
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
Development and Testing Ch4.1. Algorithm Analysis
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Chapter 4 Loops DDC 2133 Programming II.
An Automated Testing Framework
Interfaces and Inheritance
CS 240 – Lecture 11 Pseudocode.
Unit testing C# classes
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Writing Methods AP Computer Science A.
JUnit 28-Nov-18.
Test-driven development (TDD)
Introduction to JUnit CS 4501 / 6501 Software Testing
Programming Funamental slides
Coding Concepts (Basics)
How to Run a Java Program
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
TDD & ATDD 1/15/2019.
Section 3 Graphs & Testing
Chapter 50 Testing Using JUnit
Building Java Programs
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CMPE212 – Reminders Assignment 2 due this Friday.
CS 240 – Advanced Programming Concepts
Section 4: Graphs and Testing
CSE 143 Lecture 5 More ArrayIntList:
JUnit Reading: various web pages
Creating and Modifying Text part 3
Testing Slides adopted from John Jannotti, Brown University
Principles of Object Oriented Programming
Presentation transcript:

testing Acknowledgement: Original slides by Jory Denny

Development (one out of many perspectives) Solve Implement Write test Write code Repeat Integrate Release

Test Driven Development (TDD) 1. Write a test (or multiple tests) Note test should fail first! 2. Write the code Get the test to pass Refactor Clean the code Write more tests

Practical Example Lets practice some TDD on the following example You are tasked with implementing a feature for a company that determines the total amount of time a worker spends working in a week (for his salary for example). He says the number of hours worked each day is already being measured and is stored in an internal array in the code base.

Practical Example How do we solve this? Compute the sum!

Practical Example First we write a test public static double sum(double[] arr) { return Double.POSITIVE_INFINITY; //note this clearly does not work and is thus failing } public static void main() { double[] arr = {0, 1, 1, 2, 3, 5, 8}; if(sum(arr) != 20) System.out.println("Test failed?!?!?! I suck!"); //you don't, its supposed to fail!

Practical Example Before we continue, lets review Positives Scaffolding, function interface, and test all implemented We know it is good design Tests to tell if the code is correct, before we struggle with debugging many lines of code Negatives Code isn’t written until later…..but is that really that bad? NO In fact, with TDD you code FASTER and more EFFECTIVELY than without it

Practical Example Now the code – and then run the test! public static double sum(double[] arr) { double s = 0; for(double x : arr) s += x; return s; }

Things to remember Always have code that compiles Test writing is an art that takes practice (and more learning!) Compile and test often!

Testing Frameworks Many frameworks exist CppUnit, JUnit, etc. We will be using JUnit A unit test is a check of one behavior of one “unit” (e.g., function) of your code

Testing Frameworks SETT – unit testing paradigm Setup – create data for input and predetermine the output Execute – call the function in question Test – analyze correctness and determine true/false for test Teardown – cleanup any data

Unit test example public static boolean testSum() { //setup double[] arr = {0, 1, 1, 2, 3, 5, 8}; double ans = 20; //execute double s = sum(arr); //test return s == ans; }

TDD - Exercise Write a Java function to find the median of an array of integers Do test driven development, starting with a good unit test(s) After test is created and checked, code the function

TDD - Exercise [Median] In a group of n elements, the median is the middle element, such that the number of elements less than or equal the median is equal to the number of elements larger than or equal the median. If n is odd, we take the middle element. If n is even, the median is defined as the average of the two middle elements. For example, the median of the sequence {1, 1, 2, 3, 5} is 2 and the median of the sequence {1, 1, 2, 3, 5, 8} is 2.5

TDD - Exercise public static boolean testMedian1() { //setup double[] arr = {1, 1, 2, 3, 5}; double ans = 2; //execute double s = median(arr); //test return s == ans; }

JUnit Basics JUnit is the de facto framework for testing Java programs. JUnit is a third-party open source library packed in a jar file. The jar file contains a tool called test runner, which is used to run test programs. Suppose you have a class named A. To test this class, you write a test class named ATest. This test class, called a test class, contains the methods you write for testing class A. The test runner executes ATest to generate a test report, as shown in Figure 44.1.

A JUnit Test Class To use JUnit, create a test class. By convention, if the class to be tested is named A, the test class should be named ATest. A simple template of a test class may look like this: package mytest; import org.junit.*; import static org.junit.Assert.*; public class ATest { @Test public void m1() { // Write a test method } public void m2() { // Write another test method @Before public void setUp() throws Exception { // Common objects used by test methods may be set up here

Run the Test To run the test from the console, use the following command: java org.junit.runner.JUnitCore mytest.ATest

Test ArrayList Listing 44.1 is an example of a test class for testing java.util.ArrayList. @Before public void setUp() throws Exception { } @Test public void testInsertion() { list.add("Beijing"); assertEquals("Beijing", list.get(0)); list.add("Shanghai"); list.add("Hongkong"); assertEquals("Hongkong", list.get(list.size() - 1)); public void testDeletion() { list.clear(); assertTrue(list.isEmpty()); list.add("A"); list.add("B"); list.add("C"); list.remove("B"); assertEquals(2, list.size());