Test automation / JUnit Building automatically repeatable test suites.

Slides:



Advertisements
Similar presentations
J-Unit Framework.
Advertisements

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.
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. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
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.
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.
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:
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.
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
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.
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
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
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 SWE 619 Summer July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable.
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.
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.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
Software Engineering 1, CS 355 Unit Testing with JUnit
Test-first development
JUnit 28-Nov-18.
JUnit 28-Nov-18.
Introduction to JUnit CS 4501 / 6501 Software Testing
JUnit 7-Dec-18.
Test-Driven Development
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.
Test Driven Development
JUnit 18-Apr-19.
JUnit Reading: various web pages
Test-Driven Development
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.
Presentation transcript:

Test automation / JUnit Building automatically repeatable test suites

2 Test automation Test automation is software that automates any aspect of testing Generating test inputs and expected results Running test suites without manual intervention Evaluating pass/no pass Testing must be automated to be effective and repeatable

3 Automated testing steps Exercise the implementation with the automated test suite Repair faults revealed by failures Rerun the test suite on the revised implementation Evaluate test suite coverage Enhance the test suite to achieve coverage goals Rerun the automated test suite to support regression testing

4 Automated testing advantages Permits quick and efficient verification of bug fixes Speeds debugging and reduces “bad fixes” Allows consistent capture and analysis of test results Its cost is recovered through increased productivity and better system quality More time to design better tests, rather than entering and reentering tests

5 Automated testing advantages Unlike manual testing, it is not error-prone and tedious Only feasible way to do regression testing, compatibility testing, portability testing, performance testing, and configuration testing Necessary to run long and complex tests Easily evaluates large quantities of output

6 Limitations and caveats A skilled tester can use his experience to react to manual testing results by improvising effective tests Automated tests are expensive to create and maintain If the implementation is changing frequently, maintaining the test suite might be hard

7 XP approach to testing In the Extreme Programming approach Tests are written before the code itself If the code has no automated test cases, 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

8 XP consequences Fewer bugs More maintainable code The code can be refactored without fear Continuous integration During development, the program always works It may not do everything required, but what it does, it does right

9 JUnit JUnit is a framework for writing tests Written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology) 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 write and debug code integrate code and always be ready to release a working version BlueJ, JBuilder, and Eclipse provide JUnit tools

10 Terminology A test fixture sets up the data (both objects and primitives) that are needed for every test 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

11 Structure of a JUnit test class Suppose you want to test a class named Fraction Create a corresponding test class FractionTest public class FractionTest extends junit.framework.TestCase { … }

12 Structure of a JUnit test class protected void setUp() Creates a test fixture by creating and initializing objects and values protected void tearDown() Releases any system resources used by the test fixture public void testAdd() public void testToString() These methods contain tests for the Fraction methods add(), toString(), etc. It is important that their name starts with test

13 What JUnit does For each test method t in the test suite: JUnit calls setUp() This creates all the necessary objects JUnit calls t Any exceptions during its execution are logged JUnit calls tearDown() Clean up, e.g. close files A report for all test cases is presented textually or graphically

14 Assert methods 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 An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails JUnit catches these exceptions and shows you the results

15 List of assert methods 1 static void assertTrue(boolean test ) static void assertTrue(String message, boolean test ) Throws an AssertionFailedError if test is False The optional message is included in the Error static void assertFalse(boolean test ) static void assertFalse(String message, boolean test ) Throws an AssertionFailedError if test is True

16 Example: Counter class Consider a trivial “counter” class The constructor creates a counter and sets it to zero The increment method adds one to the counter and returns the new value The decrement method subtracts one from the counter and returns the new value The corresponding Junit test class…

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

18 List of assert methods 2 assertEquals( expected, actual ) assertEquals(String message, expected, actual ) This method is heavily overloaded: arg1 and arg2 must be both objects or both of the same primitive type For objects, it uses the equals method, if it has been defined properly, as public boolean equals(Object o) Otherwise it uses ==

19 List of assert methods 3 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

20 List of assert methods 4 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, or when testing for exceptions (see next slide)

public void testAnIOExceptionIsThrown { try { // Code that should raise an IO exception fail("Expected an IO exception"); } catch (IOException e) { // This is the expected result, so // leave it empty so that the test // will pass. If you care about // particulars of the exception, you // can test various assertions about // the exception object }

22 The assert statement Earlier versions of JUnit had an assert method instead of an assertTrue method The name had to be changed when Java 1.4 introduced the assert statement A statement such as assert boolean_condition ; will also throw an AssertionFailedError if the boolean_condition is false

23 Problems with unit testing JUnit is designed to call methods and compare the results they return against expected results This works great for methods that just return results, but many methods have side effects To test methods that do output, you have to capture the output It’s possible to capture output, but it’s an unpleasant coding chore To test methods that change the state of the object, you have to have code that checks the state It’s a good idea to have methods that test state invariants It isn’t easy to see how to unit test GUI code

24 First steps toward solutions You can redefine System.out to use a different PrintStream with System.setOut(PrintStream) You can “automate” GUI use by “faking” events public void fakeAction(Component c) { getToolkit().getSystemEventQueue().postEvent( new ActionEvent(c, ActionEvent.ACTION_PERFORMED, "")); }

25 JUnit in Eclipse JUnit can be downloaded from For this course, we will use it as part of Eclipse Eclipse contains wizards to help with the development of test suites with JUnit JUnit results are presented in an Eclipse window

26 Hello World demo Run Eclipse File -> New -> Project, choose Java Project, and click Next. Type in a project name, e.g. ProjectWithJUnit. Click Finish Project -> Properties, select Java Build Path, Libraries, click Add External JARs.Browse to directory where JUnit is stored. Pick junit.jar and click Open. JUnit will appear in the list of libraries. Click OK

27 Create a class File -> New -> Class, enter class name, e.g. HelloWorld. Click Finish Add a dummy method such as public String say() { return null; } Right-click in the editor window and select Save

28 Create a test class Right-click on the ProjectWithJUnit title, select New -> Junit Test Case Provide names for the class to test, e.g. HelloWorld, and the test class, e.g. HelloWorldTest

29 Create a test class Check to create a setup method Click Next Check the checkbox for the say method This will create a stub for a test case for this method Click Finish The HelloWorldTest class is created The first version of the test suite is ready

30 Run the test class Run -> Run as -> JUnit Test The results appear in the left window (you may have to click the JUnit tab) All tests passed! But we didn’t really test anything The setup method should create a HelloWorld object hi = new HelloWorld(); Modify the testSay method body to assertEquals("Hello World!", hi.say());

31 Re-run the test class Save the new version of the test class and re-run This time the test fails The body of the method say has to be modified to return(“Hello World!”); for the test to pass

32 Create a test suite 1 Right-click on ProjectWithJUnit, select New -> Other, select JUnit, click on JUnit Test Suite, and then Next On the next window, click Finish. This creates a test suite You can easily add more test classes Run with Run -> Run As -> JUnit Test

33 Create a test suite 2 Right-click on ProjectWithJUnit, select New -> Other, select JUnit, click on JUnit Test Suite, and then Next Check the checkboxes to create a main method and a test runner statement Run -> Run As -> Java application