1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.

Slides:



Advertisements
Similar presentations
JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
Advertisements

Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
1 Unit Testing with JUnit CS 3331 Fall 2009 Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available.
J-Unit Framework.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
1-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Approach of Unit testing with the help of JUnit Satish Mishra
JUnit, Revisited 17-Apr-17.
21-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
22-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
23-Jun-15 Unit Testing in Ruby. Programming methodologies The grim facts: The majority of large programming projects fail Projects that succeed are usually.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Software Testing and Quality Assurance: Class Testing Basics
26-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Testowanie kodu Bartosz Baliś, Na podstawie prezentacji Satisha Mishra Iana Sommerville Erica Braude.
1 CSC/ECE 517 Fall 2010 Lec. 2 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
15-Jul-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (testing whatever occurs.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Junit Training Chris Yeung 8 th Sept, Introduction JUnit is a regression testing frameworkJUnit is a regression testing framework Written by Erich.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
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.
Test automation / JUnit Building automatically repeatable test suites.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
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.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
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.
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 A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Unit Testing with FlexUnit
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Default Methods Anonymous Classes Generic Methods Lambdas Design by Contract JUnit.
Test automation / JUnit Building automatically repeatable test suites.
Lecture 5: Test-Driven Development Basics
Unit Testing with JUnit
Computer Science 209 Testing With JUnit.
Software Engineering 1, CS 355 Unit Testing with JUnit
JUnit 28-Nov-18.
JUnit 28-Nov-18.
Test-driven development (TDD)
Introduction to JUnit CS 4501 / 6501 Software Testing
JUnit 7-Dec-18.
JUnit 11-Jan-19.
Test Driven Development
CSE 403 JUnit Reading: These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed permission.
Unit Testing in Ruby 22-Feb-19.
Testing 24-Feb-19.
Test Driven Development
JUnit 18-Apr-19.
JUnit Reading: various web pages
TCSS 360, Spring 2005 Lecture Notes
JUnit SWE 619 Spring 2008.
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
JUnit 31-May-19.
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

2 Lecture Outline Get familiar with a software testing tool (JUnit).

3 Assert Assert within a test: Call the method being tested and get the actual result. assert what the correct result should be with one of the provided assert methods. These steps can be repeated as many times as necessary.

4 JUnit assert methods static void assertTrue(boolean test) static void assertTrue(String message, boolean test) Throws an AssertionFailedError if the test fails (the optional message is included in the Error) static void assertFalse(boolean test) static void assertFalse(String message, boolean test) Throws an AssertionFailedError if the test fails. assertEquals(expected, actual) assertEquals(String message, expected, actual) For objects, uses your equals method, if you have defined it properly, as public boolean equals(Object o)--otherwise it uses ==

5 JUnit assert methods (cont...) assertSame(Object expected, Object actual) assertSame(String message, Object expected, Object actual) Asserts that two objects refer to the same object (using ==). assertNotSame(Object expected, Object actual) assertNotSame(String message, Object expected, Object actual) Asserts that two objects do not refer to the same object.

6 JUnit assert methods (cont...) assertNull(Object object) assertNull(String message, Object object) Asserts that the object is null. assertNotNull(Object object) assertNotNull(String message, Object object) Asserts that the object is not null. fail() fail(String message) Causes the test to fail and throw an AssertionFailedError Useful as a result of a complex test, when the other assert methods aren’t quite what you want.

7 When to use an assert statement Use assertTrue to document a condition that you “know” to be true. Use assertFalse ; in code that you “know” cannot be reached (such as a default case in a switch statement).

8 Example: Counter class We will create and test a trivial “counter” class: The constructor will create a counter and set it to zero. The increment method will add one to the counter and return the new value. The decrement method will subtract one from the counter and return the new value. We write the test methods before we write the code. Don’t be alarmed if, in this simple example, the JUnit tests are more code than the class itself.

9 Example: JUnit tests for Counter class (cont...) public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } protected void tearDown() { } // no resources to release public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } public void testDecrement() { assertTrue(counter1.decrement() == -1); } } Note that each test begins with a brand new counter This means you don’t have to worry about the order in which the tests are run

10 Example: The Counter class itself (cont...)  public class Counter { int count = 0; public int increment() { return ++count; } public int decrement() { return --count; } public int getCount() { return count; }  } Is JUnit testing overkill for this little class? The Extreme Programming view is: If it isn’t tested, assume it doesn’t work You are not likely to have many classes this trivial in a real program, so writing JUnit tests for those few trivial classes is no big deal Often even XP programmers don’t bother writing tests for simple getter methods such as getCount () We only used assertTrue in this example, but there are additional assert methods

11 Test suites Obviously you have to test your code to get it working in the first place: You can do ad hoc testing (running whatever tests occur to you at the moment). You can build a test suite (a thorough set of tests that can be run at any time).

12 Test suites: advantages and disadvantages Disadvantages of a test suite: It’s a lot of extra programming: This is true, but use of a good test framework can help quite a bit. You don’t have time to do all that extra work: False--Experiments repeatedly show that test suites reduce debugging time more than the amount spent building the test suite. Advantages of a test suite: Reduces total number of bugs in delivered code.

13 TestRunners: text Text: Lightweight, quick quiet. Run from command line. java StringTest Time: 0.05 Tests run: 7, Failures: 0, Errors: 0

14 TestRunners: swing Run with java junit.swingui.TestRunner.

15 Example: point class public class Point { private int x, y; public Point(int x, int y){ this.x = x; this.y = y;} public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public int getX() { return x; } public int getY() { return y; } }

16 Example (cont...): point test without using JUnit public class PointTestNoJunit { public static void main(String [] args) { testSetX(new Point(10, 20), 0); testSetX(new Point(10, 20), 1); testSetX(new Point(10, 20), 30); } private static void testSetX(Point p, int x) { System.out.print("Before: " + toString(p) + ";"); p.setX(x); System.out.println(" After setX(" + x + "): " + toString(p)); } private static String toString(Point p) { return " "; }

17 Example (cont...): point test without using JUnit — output Before: ; After setX(0): Before: ; After setX(1): Before: ; After setX(30): Before: ; After setY(0): Before: ; After setY(1): Before: ; After setY(30):

18 Example (cont...): point test using JUnit import junit.framework.*; /** A JUnit test class to test the class Point. */ public class PointTest extends TestCase { /** Tests constructor. */ public void testPoint() { Point p = new Point(10,20); assertEquals(10, p.getX()); assertEquals(20, p.getY()); } /** Tests setX */ public void testSetX() { Point p = new Point(10, 20); p.setX(30); assertEquals(30, p.getX()); assertEquals(20, p.getY()); } //other test methods, e.g., for setY(), getX(), and getY(). // cont…

19 Example (cont...): point test suite using JUnit /** Returns the test suite for this test class. */ public static Test suite() { return new TestSuite(PointTest.class); } /** Run the tests. */ public static void main(String[] args) { junit.textui.TestRunner.run(suite()); // junit.swingui.TestRunner.run(suite()); }

20 Example (cont...): point test using JUnit — output C:/> javac Point.java PointTest.java C:/> java PointTest...F..... Time: There was 1 failure: testSetX1(PointTest)junit.framework.AssertionFailedError: expected: but was: at PointTest.testSetX1(PointTest.java:58) … at PointTest.main(PointTest.java:17) FAILURES!!! Tests run: 8, Failures: 1, Errors: 0

21 Example (cont...): point test using JUnit —Test Fixture public class PointTest extends TestCase { private Point p; // test fixture variable public void setUp() { // initializes text fixture variables p = new Point(10, 10); } public void tearDown() { }//clean up text fixture variables public void testSetX() { // tests SetX p.setX(20); assertEquals(20, p.getX()); } public void testSetY() { // tests SetY p.setY(30); assertEquals(30, p.getY()); } // template and other test methods here… }

22 JUnit Testing Tips Code a little, test a little, code a little, test a little... Run your tests as often as possible, at least as often as you run the compiler. Begin by writing tests for the areas of the code that you’re the most worried about...write tests that have the highest possible return on your testing investment.

23 JUnit Testing tips When you need to add new functionality to the system, write the tests first. If you find yourself debugging using System.out.println(), write a test case instead. When a bug is reported, write a test case to expose the bug. Don’t deliver code that doesn’t pass all the tests. Separate production and test code: But typically in the same packages.