Test automation / JUnit. Building automatically repeatable test suites

Similar documents
Test automation Test automation / JUnit

EECS 4313 Software Engineering Testing

Test automation / JUnit. Building automatically repeatable test suites

Test-Driven Development JUnit

Test-Driven Development JUnit

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 to you at

Introduction to JUnit

Tools for Unit Test - JUnit

JUnit Framework. Terminology: assertions, annotations, fixtures. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Video 2.1. Arvind Bhusnurmath. Property of Penn Engineering, Arvind Bhusnurmath. SD1x-2 1

Tools for Unit Test JUnit

Testing on Steriods EECS /30

Object Oriented Software Design - I

Agenda. JUnit JaCoCo Project. CSE 4321, Jeff Lei, UTA

Tuesday, November 15. Testing

Verifying and Documenting ADTs: Javadoc, Java Assertions and JUnits

Advanced Software Engineering

linaye/gl.html

Approach for Unit testing with the help of JUnit...

Test-Driven Development (a.k.a. Design to Test) CSE260, Computer Science B: Honors Stony Brook University

Junit Overview. By Ana I. Duncan

Chapter 3. Unit Testing with JUnit and Debugging. Testing with JUnit getting started. Note

COMP 354 TDD and Refactoring

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Agile Software Development. Lecture 7: Software Testing

Functional Testing (Testování funkčnosti)

CS18000: Programming I

SWE 434 Software Testing and Validation

print statements, debugger expressions, test scripts. Writing expressions in a debugger only that t a program works now. An application typically

Software Test. Levels of test. Types of test. Regression test The JUnit tool for unit testing Java programs. System test Integration test Unit test

Basic Keywords Practice Session

Practical Objects: Test Driven Software Development using JUnit

Testing Stragegies. Black Box Testing. Test case

UNIT TESTING. Krysta Yousoufian CSE 331 Section April 19, With material from Marty Stepp, David Notkin, and The Pragmatic Programmer

3. Design by Contract

JUnit in EDA Introduction. 2 JUnit 4.3

Software Development Tools. COMP220/COMP285 Sebastian Coope Eclipse and JUnit: Creating and running a JUnit test case

Announcements. Lab tomorrow. Quiz Thursday P3 due Friday. Exceptions and unit testing

xtreme Programming (summary of Kent Beck s XP book) Stefan Resmerita, WS2015

TEST DRIVEN DEVELOPMENT

Chapter 15. Software Testing The assert Statement

More JUnit CS 4501 / 6501 Software Testing

JUnit on Eclipse. Chien-Tsun Chen Sep. 23, 2003

INTRODUCTION TO JAVA PROGRAMMING JAVA FUNDAMENTALS PART 2

Testing. Technion Institute of Technology Author: Assaf Israel. Author: Assaf Israel - Technion

Java Review via Test Driven Development

JUnit Testing Framework Architecture

4. A Testing Framework

Programmieren II. Unit Testing & Test-Driven Development. Alexander Fraser.

Assertions, pre/postconditions

Section 4: Graphs and Testing. Slides by Erin Peach and Nick Carney

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

Testing. CMSC 433 Programming Language Technologies and Paradigms Spring A Real Testing Example. Example (Black Box)?

Software Testing Lecture 1. Justin Pearson

White box testing. White-box testing. Types of WBT 24/03/15. Advanced Programming

CMSC 132: OBJECT-ORIENTED PROGRAMMING II

Introduc)on to tes)ng with JUnit. Workshop 3

Unit Testing with JUnit and CppUnit

4. A Testing Framework. Oscar Nierstrasz

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

An introduction to formal specifications and JML. Invariant properties

11 Using JUnit with jgrasp

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Unit Testing : Software Testing

Ingegneria del Software Corso di Laurea in Informatica per il Management

No Source Code. EEC 521: Software Engineering. Specification-Based Testing. Advantages

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/

Jtest Tutorial. Tutorial

JUnit tes)ng. Elisa Turrini

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

Analysis of the Test Driven Development by Example

Efficient Regression Test Model for Object Oriented Software

Testing with JUnit 1

Black Box Testing. EEC 521: Software Engineering. Specification-Based Testing. No Source Code. Software Testing

Topics covered. Introduction to JUnit JUnit: Hands-on session Introduction to Mockito Mockito: Hands-on session. JUnit & Mockito 2

EECS 4313 Software Engineering Testing. Topic 01: Limits and objectives of software testing Zhen Ming (Jack) Jiang

Unit Testing. CS 240 Advanced Programming Concepts

Problem other classes messing with my stuff!

JUnit 4 and Java EE 5 Better Testing by Design

CSE 326: Data Structures. Section notes, 4/9/2009

CSE 331 Final Exam 3/16/15 Sample Solution

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

Motivating Example: Two Types of Errors (2) Test-Driven Development (TDD) with JUnit. Motivating Example: Two Types of Errors (1)

Test-Driven Development (TDD) with JUnit

Section 4: Graphs and Testing

Ex 1. public float temperature(float powsupply, float clock, char cooler)

Automated testing in Agile SW development

Tutorial 3: Unit tests and JUnit

Chapter 14 Software Testing Techniques

Testing and Debugging

Stephen Edwards. What is unit testing? Manuel Pérez- Quiñones. Virginia Tech. You betcha! For you: Repeatedly check behaviors of many student programs

Unit testing. JUnit. JUnit and Eclipse. A JUnit test class 3/6/17. A method is flagged as a JUnit test case.

CSC207 Week 3. Larry Zhang

Automating Regression Testing of Java Programs the JSnoopy Way

Java JUnit laboratory

Section 4: Graphs and Testing

Lecture 15 Software Testing

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Testing & Debugging TB-1

Transcription:

Test automation / JUnit Building automatically repeatable test suites

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

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

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

Automated testing advantages n Unlike manual testing, it is not errorprone and tedious n Only feasible way to do regression testing n Necessary to run long and complex tests n Easily evaluates large quantities of output 5

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

XP approach to testing n In the Extreme Programming approach n n n n Tests are written before the code itself If the code has no automated test cases, it is assumed not to work A testing framework is used so that automated testing can be done after every small change to the code n 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 7

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

JUnit n JUnit is a framework for writing tests n n n Written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology) Uses Java 5 features such as annotations and static imports JUnit helps the programmer: n n n n define and execute tests and test suites formalize requirements write and debug code integrate code and always be ready to release a working version 9

Terminology n A test fixture sets up the data (both objects and primitives) that are needed for every test n Example: If you are testing code that updates an employee record, you need an employee record to test it on n A unit test is a test of a single class n A test case tests the response of a single method to a particular set of inputs n A test suite is a collection of test cases n A test runner is software that runs tests and reports results 10

Structure of a JUnit test class n To test a class named Fraction n Create a test class FractionTest import org.junit.*;! import static org.junit.assert.*;! public class FractionTest! {!!!! }! 11

Test fixtures n Methods annotated with @Before will execute before every test case n Methods annotated with @After will execute after every test case @Before! public void setup() { }! @After! public void teardown() { }! 12

Class Test fixtures n Methods annotated with @BeforeClass will execute once before all test cases n Methods annotated with @AfterClass will execute once after all test cases n These are useful if you need to allocate and release expensive resources once 13

Test cases n Methods annotated with @Test are considered to be test cases @Test! public void testadd() { }! @Test! public void testtostring() { }! 14

What JUnit does n For each test case t: n JUnit executes all @Before methods n Their order of execution is not specified n JUnit executes t n Any exceptions during its execution are logged n JUnit executes all @After methods n Their order of execution is not specified n A report for all test cases is presented 15

Within a test case n Call the methods of the class being tested n Assert what the correct result should be with one of the provided assert methods n These steps can be repeated as many times as necessary n An assert method is a JUnit method that performs a test, and throws an AssertionError if the test fails n JUnit catches these exceptions and shows you the results 16

List of assert methods 1 n asserttrue(boolean b) asserttrue(string s, boolean b) n Throws an AssertionError if b is False n The optional message s is included in the Error n assertfalse(boolean b) assertfalse(string s, boolean b) n Throws an AssertionError if b is True n All assert methods have an optional message 17

Example: Counter class n Consider a trivial counter class n The constructor creates a counter and sets it to zero n The increment method adds one to the counter and returns the new value n The decrement method subtracts one from the counter and returns the new value n The corresponding JUnit test class 18

public class CounterTest { Counter counter1; @Before public void setup() { // creates a (simple) test fixture counter1 = new Counter(); } @Test public void testincrement() { asserttrue(counter1.increment() == 1); asserttrue(counter1.increment() == 2); } } @Test 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

List of assert methods 2 n assertequals(object expected, Object actual) n Uses the equals method to compare the two objects n Primitives can be passed as arguments thanks to autoboxing n Casting may be required for primitives n There is also a version to compare arrays 20

List of assert methods 3 n assertsame(object expected, Object actual) n Asserts that two references are attached to the same object (using ==) n assertnotsame(object expected, Object actual) n Asserts that two references are not attached to the same object 21

List of assert methods 4 n assertnull(object object) Asserts that a reference is null n assertnotnull(object object) Asserts that a reference is not null n fail() Causes the test to fail and throw an AssertionError n Useful as a result of a complex test, or when testing for exceptions 22

Testing for exceptions n If a test case is expected to raise an exception, it can be noted as follows @Test(expected = Exception.class)! public void testexception() {! //Code that should raise an exception! fail("should raise an exception");! } 23

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! }! }!

The assert statement n A statement such as assert boolean_condition; will also throw an AssertionError if the boolean_condition is false n Can be used instead of the Junit asserttrue method 25

Ignoring test cases n Test cases that are not finished yet can be annotated with @Ignore! n JUnit will not execute the test case but will report how many test cases are being ignored 26

Automated testing issues n It isn t easy to see how to unit test GUI code n JUnit is designed to call methods and compare the results they return against expected results n This works great for methods that just return results, but many methods have side effects 27

Automated testing issues n To test methods that do output, you have to capture the output n It s possible to capture output, but it s an unpleasant coding chore n To test methods that change the state of the object, you have to have code that checks the state n It s a good idea to have methods that test state invariants 28

First steps toward solutions n You can redefine System.out to use a different PrintStream with System.setOut(PrintStream)! n You can automate GUI use by faking events n We will see this in more detail later 29

JUnit in Eclipse n JUnit can be downloaded from http://junit.sourceforge.net/ n If you use Eclipse, as in this course, you do not need to download anything n Eclipse contains wizards to help with the development of test suites with JUnit n JUnit results are presented in an Eclipse window

Hello World demo n Run Eclipse n File -> New -> Project, choose Java Project, and click Next. Type in a project name, e.g. ProjectWithJUnit. n Click Next n Click Create New Source Folder, name it test n Click Finish n Click Finish

Create a class n Right-click on ProjectWithJUnit Select New -> Package Enter package name, e.g. cse4313 Click Finish n Right-click on cse4313 Select New -> Class Enter class name, e.g. HelloWorld Click Finish

Create a class - 2 n Add a dummy method such as public String say() { return null; } n Right-click in the editor window and select Save

Create a test class n Right-click on the HelloWorld class Select New -> Junit Test Case n Change the source folder to test as opposed to src n Check to create a setup method n Click Next

Create a test class n Check the checkbox for the say method n This will create a stub for a test case for this method n Click Finish n Click OK to Add JUnit 4 library to the build path n The HelloWorldTest class is created n The first version of the test suite is ready

Run the test class - 1st try n Right click on the HelloWorldTest class n Select Run as -> JUnit Test n The results appear in the left n The automatically created test case fails

Create a better test case n Import the class under test import cse4313.helloworld; n Declare an attribute of type HelloWorld HelloWorld hi; n The setup method should create a HelloWorld object hi = new HelloWorld(); n Modify the testsay method body to assertequals("hello World!", hi.say());

Run the test class - 2nd try n Save the new version of the test class and re-run n This time the test fails due to expected and actual not being equal n The body of the method say has to be modified to return Hello World! ; for the test to pass

Create a test suite n Right-click on the cse4313 package in the test source folder n Select New -> Class. Name the class AllTests. n Modify the class text so it looks like class AllTests on the course website n Run with Run -> Run As -> JUnit Test n You can easily add more test classes