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.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

CIT 590 Unit testing.
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
Testowanie kodu Bartosz Baliś, Na podstawie prezentacji Satisha Mishra Iana Sommerville Erica Braude.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
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.
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.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
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.
Testing in NetBeans. SWC Testing The ideal test: When the test is passed, the product is ready for delivery! Ideal – but (almost) impossible –Number of.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
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.
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.
A tool for test-driven development
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
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.
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.
2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
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.
Test automation / JUnit Building automatically repeatable test suites.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Lecture 5: Test-Driven Development Basics
Some Eclipse shortcuts
Introduction to JUnit CS 4501 / 6501 Software Testing
Software Engineering 1, CS 355 Unit Testing with JUnit
Unit testing C# classes
History, Characteristics and Frameworks
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.
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.
Testing 24-Feb-19.
Test Driven Development
JUnit 18-Apr-19.
JUnit Reading: various web pages
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:

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 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 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 Makes code much more maintainable and refactorable This is a huge win for programs that get actual use!

3 XP approach to testing In the Extreme Programming approach, Tests are written before the code itself If code has no automated test case, it is assumed not to work A test framework is used so that automated testing can be done after every small change to the code This may be as often as every 5 or 10 minutes If a bug is found after development, a test is created to keep the bug from coming back Consequences Fewer bugs More maintainable code Continuous integration—During development, the program always works—it may not do everything required, but what it does, it does right

4 JUnit JUnit is a framework for writing tests JUnit was written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology) JUnit uses Java’s reflection capabilities (Java programs can examine their own code) JUnit helps the programmer: define and execute tests and test suites formalize requirements and clarify architecture write and debug code integrate code and always be ready to release a working version JUnit is not yet (as far as I know) included in Sun’s SDK, but an increasing number of IDEs include it BlueJ, JBuilder, and Eclipse now provide JUnit tools

5 Terminology A test fixture sets up the data (both objects and primitives) that are needed to run tests Example: If you are testing code that updates an employee record, you need an employee record to test it on A unit test is a test of a single class A test case tests the response of a single method to a particular set of inputs A test suite is a collection of test cases A test runner is software that runs tests and reports results An integration test is a test of how well classes work together JUnit provides some limited support for integration tests

6 test suite Once more, in pictures A unit test tests the methods in a single class A test case tests (insofar as possible) a single method You can have multiple test cases for a single method A test suite combines unit tests The test fixture provides software support for all this The test runner runs unit tests or an entire test suite Integration testing (testing that it all works together) is not well supported by JUnit unit test (for one class) another unit test test case (for one method) another test case test case (for one method) another unit test another test case test fixture test runner another test case

7 How to write a JUnit test class A JUnit test class is a class you write that extends junit.framework.TestCase As usual, you can use the default constructor or write your own constructors Your test class will inherit the following methods: protected void setUp() This a method that will be called before each of your test methods Typically, you will override this method and use it to assign values to some instance variables you need in testing protected void tearDown() This a method that will be called after each of your test methods Typically you will just ignore this method, unless you need to close files You will also write any number of test methods, all of which have the form public void test Something () Something is usually, but not necessarily, the name of the method you want to test.

8 How to write a JUnit test method Your test method should start with public void test Something (), where Something is any name you like (typically the name of the method you are testing) This is a normal Java method; you can put any Java in it that you like Your method will typically use one of the “assert methods”, such as assertEquals( expected_value, computed_value ) or assertTrue( boolean_condition ) You do not need to call these methods; as long as they are defined as above, they will be called automatically You do not need to do anything if the tests fail; the framework will take care of this for you

9 A simple example Suppose you have a class Arithmetic with methods int add(int x, int y) and boolean isPositive(int x) public class ArithmeticTest extends junit.framework.TestCase { }  public void testMultiply() { assertEquals(4, Arithmetic.add(2, 2)); assertEquals(-15, Arithmetic.multiply(3, -5)); }  public void testIsPositive() { assertTrue(Arithmetic.isPositive(5)); assertFalse(Arithmetic.isPositive(-5)); assertFalse(Arithmetic.isPositive(0)); }

10 Assert methods I Within a test, Call the method being tested and get the actual result Assert what the correct result should be with one of the assert methods These steps can be repeated as many times as necessary An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails JUnit catches these Errors and shows you the result 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

11 Example: Counter class For the sake of example, 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 This has the advantages described earlier Depending on the JUnit tool we use, we may have to create the class first, and we may have to populate it with stubs (methods with empty bodies) Don’t be alarmed if, in this simple example, the JUnit tests are more code than the class itself

12 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(); } 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

13 The Counter class itself  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, 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

14 Warning: equals You can compare primitives with == Java has a method x.equals( y ), for comparing objects This method works great for String s and a few other Java classes For objects of classes that you create, you have to define equals assertEquals( expected, actual ) uses == or equals To define equals for your own objects, define exactly this method: public boolean equals(Object obj ) {... } The argument must be of type Object, which isn’t what you want, so you must cast it to the correct type (say, Person ): public boolean equals(Object something) { Person p = (Person)something; return this.name == p.name; // test whatever you like here } We’ll talk much more about equals later

15 Assert methods II assertEquals( expected, actual ) assertEquals(String message, expected, actual ) This method is heavily overloaded: expected and actual must be both objects or both of the same primitive type For objects, uses your equals method, if you have defined it properly, as public boolean equals(Object o) —otherwise it uses == 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

16 Assert methods III 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 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

17 BlueJ BlueJ provides support with commands such as Create Test Class and Create Test Method You can create objects on the test bench and move them to the fixture (and back again) BlueJ also has a “recording” mode where you create and manipulate objects, and BlueJ turns your actions into test code This is a first implementation and is still quite buggy It’s worth experimenting with, but you have to check the code produced to see if it makes sense BlueJ also makes it easy to run a single test, a suite of tests, or all tests BlueJ’s display of JUnit results is virtually identical to the way results are displayed by commercial IDEs, such as JBuilder

18 JUnit in BlueJ To use JUnit in BlueJ, go to Tools  Preferences...  Miscellaneous and check Show Unit Testing Tools To create a test class for an existing class, right- click on the class and choose Create test class, then add your test methods JUnit has its own menus in BlueJ

19 Viewing test results If you run a single test, and it is successful, you just get a message in the status line Failed tests Unexpected errors and exceptions

20 Test-Driven Development (TDD) It is difficult to add JUnit tests to an existing program The program probably wasn’t written with testing in mind It’s actually better to write the tests before writing the code you want to test This seems backward, but it really does work better: When tests are written first, you have a clearer idea what to do when you write the methods Because the tests are written first, the methods are necessarily written to be testable Writing tests first encourages you to write simpler, single-purpose methods Because the methods will be called from more than one environment (the “real” one, plus your test class), they tend to be more independent of the environment

21 Stubs In order to run our tests, the methods we are testing have to exist, but they don’t have to be right Instead of starting with “real” code, we start with stubs—minimal methods that always return the same values A stub that returns void can be written with an empty body A stub that returns a number can return 0 or -1, or whatever number makes least sense A stub that returns a boolean value should usually return false A stub that returns an object of any kind (including a String or an array) should return null When we run our test methods with these stubs, we want the test methods to fail—to report all kinds of errors This helps “test the tests”

22 Recommended approach 1.Write a test for some method you intend to write If the method is fairly complex, test only the simplest case 2.Write a stub for the method 3.Run the test and make sure it fails 4.Replace the stub with code Write just enough code to pass the tests 5.Run the test If it fails, debug the method (or maybe debug the test); repeat until the test passes 6.If the method needs to do more, or handle more complex situations, add the tests for these first, and go back to step 3

23 The End If you don't unit test then you aren't a software engineer, you are a typist who understands a programming language. --Moses Jones