Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington

Slides:



Advertisements
Similar presentations
LECTURE 8: Software Testing
Advertisements

Unit Testing Australian Development Centre Brisbane, Australia.
Introduction to Maven 2.0 An open source build tool for Enterprise Java projects Mahen Goonewardene.
Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
J-Unit Framework.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
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.
ANT: Another Nice Tool Ali Beyad October 1, 2003.
Approach of Unit testing with the help of JUnit Satish Mishra
Approach for Unit testing with the help of JUnit... Satish Mishra
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
Testing Cactus and JUnit By Bill Dudney and Jonathan Lehr Neal O’Brien 10/27/04.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Java Development Tools project.
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.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Programmer Testing Testing all things Java using JUnit and extensions.
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:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing with Android Part I of II. Android Testing Framework Based on JUnit The Android JUnit extensions provide component-specific test case classes.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Testing in Extreme Programming
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
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.
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
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 At the forefront of Test Driven Development.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
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.
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 Unit testing and Java Zeger Hendrikse Cas Stigter.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Automated Testing in Sakai Testing applications and services in isolation and in context Josh Holtzman, UC Berkeley David Haines, University of Michigan.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Unit Testing with FlexUnit
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
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.
13-Mar-16 Scalatest. Scalatest variants Scalatest is a testing framework inspired by JUnit Scalatest comes in various styles: FunSuite, FlatSpec, FunSpec,
JUnit Testing Why we do this and how we can get better.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Testing PA165 Dec 9, 2014 Petr Adámek, Tomáš Pitner.
Automated Testing with PHPUnit. How do you know your code works?
Introduction to Unit Testing and JUnit David Rabinowitz.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Unit testing Java programs Using JUnit
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Software Development Tools
Computer Science 209 Testing With JUnit.
Software Engineering 1, CS 355 Unit Testing with JUnit
Test-driven development (TDD)
Introduction to JUnit IT323 – Software Engineering II
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Continuous Integration
Software Development Tools
Presentation transcript:

Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington

Introduction ● Testing ● JUnit, etc. ● Best practices from the testing community ● My best practices

Types of Testing ● Unit testing: Does a single object work? ● Integration testing: Do multiple objects work together? ● Functional testing: Does my application work? ● Performance testing: Does my application work well? ● Acceptance testing: Does the customer like my application?

Why Test? ● No-brainers: Functional, Acceptance, Performance testing ● Why Unit Test? – Coverage, especially of strange conditions – Facilitates teamwork, lessens 'ownership' – Confidence in your own work – Enable refactoring – Self-documentation of API – Improve design

How to use tests ● A common testing cycle: – Write some new code OR Fix a bug OR Refactor some code – Run tests to ensure the now-changed code works – Check the new code in

Basic testing principles ● For things that can be tested automatically, tests should be: – easy to write – easy to run – objective (easy to see if they pass or fail) – repeatable (deterministic)

JUnit ● Open source framework for testing Java code ● Attempts to make writing unit tests easy ● Structure: – TestSuites ● TestCase [Java class] – Test [Java method] – Fixtures: common setup/teardown code – Most often you write TestCases, Tests, and Fixtures

Example import junit.framework.TestCase; public class TestString extends TestCase { public void testEndsWith() { assertTrue(“abcabc”.endsWith(“abc”)); } public void testEndsWithEmptyString() { assertTrue(“Doesn't end with empty string!”, “abcabc”.endsWith(“”)); }

Some best practices ● TestCases (Java classes) – Usually test a single Java class – Name starts or ends with Test ● Tests (Java methods) – test a single method ● testXxx tests method xxx ● multiple tests of method xxx are named testXxxYyy, where Yyy describes a particular condition, e.g., testEndsWithEmpty ● Label your assertions ● One test per test method

More details ● fixtures: setUp, tearDown ● assertFalse, assertEquals, assertNotEquals, assertNull, etc., etc. ● JUnit 4/Java 5 – some incremental improvements

Running JUnit tests ● Many ways: – Command line – IDE (Eclipse plug-in, etc.) – Ant – Maven ● Various ways of viewing output (text, XML -> Web page,...)

More best practices ● Run your tests using a tool: IDE, Ant, Maven… ● Tests should be independent ● Same package, different directory src test org org jasig jasig SomeClass.java TestSomeClass.java OtherClass.java TestOtherClass.java

Related tools ● “xUnit” - e.g., CUnit for C ● WebTest, HttpUnit – web applications ● DBUnit – database-driven code ● Cactus – J2EE components ● Code coverage tools – Do my tests cover all possible lines of code? ● Continuous testing tools – Run tests on a schedule, or whenever code is checked in

Functional testing ● For web applications: WebTest, HttpUnit, Cactus, JMeter... – HttpUnit and Cactus tests are like JUnit – WebTest uses XML to specify tests – JMeter has a GUI (and writes XML) – All require a special environment to run tests ● Functional tests usually are slower than unit tests ● Functional tests usually are run separately from unit tests

Best practices from the testing community ● If it’s hard to write a test, the code your testing should probably be changed. – To test a private method, make it protected (or package-protected)

Best Practices from the testing community II ● Test anything that can reasonably break

Best practices from the testing community III ● Always verify that tests fail when they should – Particularly important if you’re fixing a bug ● Test-Driven Development – Always write a failing test before writing any new code – Refactor

Best Practices from the testing community IV ● Test one object at a time – Stubs, mock objects

Mock Objects ● Allow you to test a class that relies on other, non- trivial objects, particularly objects that are difficult to instantiate or slow ● Mock versions of many popular classes (e.g., HttpRequest) available, as well as packages that create Mocks automatically

Example Want to test DB.getUser(User, Callback), but it's hard to create a Callback object. public void testGetUser() { Callback mc = new MockCallback(); assertNotNull(DB.getUser(new User(), mc) }

More on Mocks ● Mocks can keep track of how they were used, so you can check in with them afterwards to make sure the class you're testing performed the operations you expected

Example Check that DB.getUser(User, Callback) actually does what it's supposed to do with the Callback object MockCallback mc = new MockCallback(); mc.expects([something]); mc.expects([something else]); assertNotNull(DB.getUser(new User(), mc); mc.verify();

My best practices ● Write tests that work and are easy to use. ● Don’t reject tests if they work and are easy to use. ● Create a test whenever fixing a bug. Make sure it fails before fixing the code. ● Create a test when adding a new feature, or ‘tricky’ code. ● Write clean test code.

Best practices you can break ● If it helps you write and run tests more often: – Ignore test-driven design – Don’t obsess about testing `anything that could possibly fail’ – Test more than one object at once ● But… – Learn about mock objects – Learn about other tools

Conclusions ● Testing is good ● Don't put barriers in the way of writing and running tests ● Learn about the testing toolbox, and be prepared to use the tools as appropriate

References ● ● ● JUnit in Action – Vincent Massol ● Java Development with Ant – Erik Hatcher, Steve Loughran

Sample project rules relating to tests ● If a change you make creates a bug, and the tests don’t pass, it’s your fault. – Don’t check in code unless the tests pass. ● If a change you make creates a bug (in other code), and the tests pass, it’s the fault of whoever wrote the other code. – Write more tests.