CSE 143 Lecture 14: testing.

Slides:



Advertisements
Similar presentations
Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
Advertisements

Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, and Kellen Donohue Section 4: Graphs and Testing.
J-Unit Framework.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION SOFTWARE TESTING Autumn 2011 Creative Commons Attribution-Share Alike 3.0 Unported LicenseCreative Commons Attribution-Share.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2006 Doc 2 Terms & Testing Jan 24, 2006 Copyright ©, All rights reserved SDSU.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Intoduction to Unit Testing Using JUnit to structure Unit Testing SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick.
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.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
1 CSE 331 Unit Testing with JUnit slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
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.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
SE 433/333 Software Testing & Quality Assurance Dennis Mumaugh, Instructor Office: CDM, Room 428 Office Hours: Tuesday, 4:00 –
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
JUnit Testing Why we do this and how we can get better.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Section 4: Graphs and Testing
CompSci 280 S Introduction to Software Development
Unit Testing.
SE 433/333 Software Testing & Quality Assurance
Software Construction Lab 10 Unit Testing with JUnit
They both stop working when you open Windows!
Testing Tutorial 7.
October 2nd – Dictionary ADT
Unit Testing and Debugging
Introduction to JUnit CS 4501 / 6501 Software Testing
This presentation is created for the course COP4331 at UCF
CSE332: Data Abstractions Section 4
Unit Testing and Debugging
Software Engineering 1, CS 355 Unit Testing with JUnit
Data Structures and Algorithms
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Junit with.
this keyword this : A reference to the implicit parameter Syntax:
this keyword this : A reference to the implicit parameter Syntax:
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Testing and debugging A short interlude 2-Dec-18.
Unit Testing with JUnit
Introduction to JUnit IT323 – Software Engineering II
Automation of Testing in the Distributed Common Ground System (Army)
Automation of Testing in the Distributed Common Ground System (Army)
Testing Acknowledgement: Original slides by Jory Denny.
Section 3 Graphs & Testing
CSE 143 Lecture 4 More ArrayIntList:
Chapter 50 Testing Using JUnit
CS 240 – Advanced Programming Concepts
Section 4: Graphs and Testing
Go to pollev.com/cse143.
Testing and debugging A short interlude 17-Apr-19.
CSE 143 Lecture 5 More ArrayIntList:
Building Java Programs
JUnit Reading: various web pages
Building Java Programs
slides created by Ethan Apter
CSE 143 Lecture 6 interfaces; eclipse; testing
Defensive Programming
Presentation transcript:

CSE 143 Lecture 14: testing

Unit testing unit testing: Looking for errors in a subsystem in isolation. generally a "subsystem" means a particular class or object the Java library JUnit helps us to easily perform unit testing the basic idea: For a given class Foo, create another class FooTest to test it that contains "test case" methods to run. Each method looks for particular results and passes / fails. JUnit provides "assert" commands to help us write tests.

JUnit and JGrasp To add JUnit to a JGrasp project: Download the JUnit jar file from http://junit.org/ and save it on your computer Tools  JUnit Configure You will now see a dialog box pop up. In the textfield labeled “JUnit Home” browse for the folder you stored the jar file in. If you have properly configured JGrasp green, red and white Junit buttons will appear.

A JUnit test class A method with @Test is flagged as a JUnit test case import org.junit.*; import static org.junit.Assert.*; public class name { ... @Test public void name() { // a test case method } A method with @Test is flagged as a JUnit test case all @Test methods run when JUnit runs your test class

JUnit assertion methods assertTrue(test) fails if the boolean test is false assertFalse(test) fails if the boolean test is true assertEquals(expected, actual) fails if the values are not the same fail() immediately causes current test to fail other assertion methods: assertNull, assertNotNull, assertSame, assertNotSame, assertArrayEquals The idea: Put assertion calls in your @Test methods to check things you expect to be true. If they aren't, the test will fail. Why is there no pass method? Each method can also be passed a string to show if it fails: e.g. assertEquals("message", expected, actual)

ArrayIntList JUnit test import org.junit.*; import static org.junit.Assert.*; public class TestArrayIntList { @Test public void testAddGet1() { ArrayIntList list = new ArrayIntList(); list.add(42); list.add(-3); list.add(15); assertEquals(42, list.get(0)); assertEquals(-3, list.get(1)); assertEquals(15, list.get(2)); } public void testIsEmpty() { assertTrue(list.isEmpty()); list.add(123); assertFalse(list.isEmpty()); ...

Running a test Click the red, white and green plus button to compile the tests. Click the red, green and white running man to run them. the JUnit bar will show green if all tests pass, red if any fail the Failure Trace shows which tests failed, if any, and why

Testing for exceptions @Test(expected = ExceptionType.class) public void name() { ... } will pass if it does throw the given exception, and fail if not use this to test for expected errors @Test(expected = ArrayIndexOutOfBoundsException.class) public void testBadIndex() { ArrayIntList list = new ArrayIntList(); list.get(4); // should fail

Tests with a timeout @Test(timeout = 5000) public void name() { ... } The above method will be considered a failure if it doesn't finish running within 5000 ms private static final int TIMEOUT = 2000; ... @Test(timeout = TIMEOUT) Times out / fails after 2000 ms

Tips for testing You cannot test every possible input, parameter value, etc. So you must think of a limited set of tests likely to expose bugs. Think about boundary cases positive; zero; negative numbers right at the edge of an array or collection's size Think about empty cases and error cases 0, -1, null; an empty list or array test behavior in combination maybe add usually works, but fails after you call remove make multiple calls; maybe size fails the second time only