A tool for test-driven development

Slides:



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

J-Unit Framework.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
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.
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
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
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.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
© 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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing in Extreme Programming
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.
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
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
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,
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
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.
Software Design– Unit Testing SIMPLE PRIMER ON Junit Junit is a free simple library that is added to Eclipse to all automated unit tests. The first step,
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.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
CompSci 280 S Introduction to Software Development
Software Construction Lab 10 Unit Testing with JUnit
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
Overview of Eclipse Lectures
JUnit 28-Nov-18.
JUnit 28-Nov-18.
JUnit 7-Dec-18.
JUnit 11-Jan-19.
Introduction to JUnit IT323 – Software Engineering II
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.
Test Driven Development
JUnit 18-Apr-19.
TCSS 360, Spring 2005 Lecture Notes
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
JUnit 31-May-19.
Presentation transcript:

A tool for test-driven development JUnit A tool for test-driven development

History Kent Beck developed the first xUnit automated test tool for Smalltalk in mid-90’s Beck and Gamma (of design patterns Gang of Four) developed JUnit on a flight from Zurich to Washington, D.C. Martin Fowler: “Never in the field of software development was so much owed by so many to so few lines of code.” Junit has become the standard tool for Test-Driven Development in Java (see Junit.org) Junit test generators now part of many Java IDEs (Eclipse, BlueJ, Jbuilder, DrJava) Xunit tools have since been developed for many other languages (Perl, C++, Python, Visual Basic, C#, …)

Why create a test suite? Obviously you have to test your code—right? You can do ad hoc testing (running whatever tests occur to you at the moment), or You can build a test suite (a thorough set of tests that can be run at any time) Disadvantages of a test suite It’s a lot of extra programming 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 Makes code much more maintainable and refactorable

Architectural overview JUnit test framework is a package of classes that lets you write tests for each method, then easily run those tests TestRunner runs tests and reports TestResults You test your class by extending abstract class TestCase To write test cases, you need to know and understand the Assert class

Writing a TestCase To start using JUnit, create a subclass of TestCase, to which you add test methods Here’s a skeletal test class: import junit.framework.TestCase; public class TestBowl extends TestCase { } //Test my class Bowl Name of class is important – should be of the form TestMyClass or MyClassTest This naming convention lets TestRunner automatically find your test classes

Writing methods in TestCase Pattern follows programming by contract paradigm: Set up preconditions Exercise functionality being tested Check postconditions Example: public void testEmptyList() { Bowl emptyBowl = new Bowl(); assertEquals(“Size of an empty list should be zero.”, 0, emptyList.size()); assertTrue(“An empty bowl should report empty.”, emptyBowl.isEmpty()); } Things to notice: Specific method signature – public void testWhatever() Allows them to be found and collected automatically by JUnit Coding follows pattern Notice the assert-type calls…

Assert methods Each assert method has parameters like these: message, expected-value, actual-value Assert methods dealing with floating point numbers get an additional argument, a tolerance Each assert method has an equivalent version that does not take a message – however, this use is not recommended because: messages helps documents the tests messages provide additional information when reading failure logs

Assert methods assertTrue(String message, Boolean test) assertFalse(String message, Boolean test) assertNull(String message, Object object) assertNotNull(String message, Object object) assertEquals(String message, Object expected, Object actual) (uses equals method) assertSame(String message, Object expected, Object actual) (uses == operator) assertNotSame(String message, Object expected, Object actual)

More stuff in test classes Suppose you want to test a class Counter public class CounterTest extends junit.framework.TestCase { This is the unit test for the Counter class public CounterTest() { } //Default constructor protected void setUp() Test fixture creates and initializes instance variables, etc. protected void tearDown() Releases any system resources used by the test fixture public void testIncrement(), public void testDecrement() These methods contain tests for the Counter methods increment(), decrement(), etc. Note capitalization convention

JUnit tests for Counter public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } 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

TestSuites TestSuites collect a selection of tests to run them as a unit Collections automatically use TestSuites, however to specify the order in which tests are run, write your own: public static Test suite() { suite.addTest(new TestBowl(“testBowl”)); suite.addTest(new TestBowl(“testAdding”)); return suite; } Should seldom have to write your own TestSuites as each method in your TestCase should be independent of all others Can create TestSuites that test a whole package: TestSuite suite = new TestSuite(); suite.addTestSuite(TestBowl.class); suite.addTestSuite(TestFruit.class);

This will be filled in automatically JUnit in Eclipse To create a test class, select File New Other...  Java, JUnit, TestCase and enter the name of the class you will test Fill this in This will be filled in automatically

Running JUnit Second, use this pulldown menu First, select a Test class Third, Run As  JUnit Test

Results Your results are here

Viewing results in Eclipse Ran 10 of the 10 tests No tests failed, but... Something unexpected happened in two tests Bar is green if all tests pass, red otherwise This is how long the test took This test passed Something is wrong Depending on your preferences, this window might show only failed tests

More Information http://www.junit.org Download of JUnit Lots of information on using JUnit http://sourceforge.net/projects/cppunit C++ port of Junit http://www.thecoadletter.com Information on Test-Driven Development