Test automation / JUnit. Building automatically repeatable test suites

Similar documents
Test automation / JUnit. Building automatically repeatable test suites

Test automation Test automation / JUnit

EECS 4313 Software Engineering Testing

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

Tools for Unit Test - JUnit

Testing on Steriods EECS /30

Introduction to JUnit

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

Tools for Unit Test JUnit

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

Agile Software Development. Lecture 7: Software Testing

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

JUnit Testing Framework Architecture

COMP 354 TDD and Refactoring

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

linaye/gl.html

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

4. A Testing Framework

Functional Testing (Testování funkčnosti)

Junit Overview. By Ana I. Duncan

Practical Objects: Test Driven Software Development using JUnit

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

Advanced Software Engineering

Chapter 15. Software Testing The assert Statement

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

Credit where Credit is Due. Lecture 29: Test-Driven Development. Test-Driven Development. Goals for this lecture

Test-Driven Development

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

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

SWE 434 Software Testing and Validation

4. A Testing Framework. Oscar Nierstrasz

An introduction to formal specifications and JML. Invariant properties

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

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

TEST DRIVEN DEVELOPMENT

Testing Stragegies. Black Box Testing. Test case

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

More JUnit CS 4501 / 6501 Software Testing

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

INTRODUCTION TO JAVA PROGRAMMING JAVA FUNDAMENTALS PART 2

Test-Driven Development

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

Binghamton University. CS-140 Fall Unit Testing

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

Unit Testing with JUnit and CppUnit

Binghamton University. CS-140 Fall Unit Testing

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

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

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

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

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

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

CS18000: Programming I

Unit Testing : Software Testing

There are three basic elements in object oriented programming: encapsulation, inheritance and polymorphism.

Problem other classes messing with my stuff!

Chapter 14 Software Testing Techniques

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

Unit testing. unit testing: Looking for errors in a subsystem in isolation. The basic idea: JUnit provides "assert" commands to help us write tests.

Testing & Debugging TB-1

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

Analysis of the Test Driven Development by Example

Simple TDD Case Study. in Java

3. Design by Contract

Assertions, pre/postconditions

White-box testing. Software Reliability and Testing - Barbara Russo SERG - Laboratory of Empirical Software Engineering.

CS159. Nathan Sprague. September 30, 2015

An Introduction to Systematic Software Testing. Robert France CSU

Java Review via Test Driven Development

Lecture 15 Software Testing

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

Automating Regression Testing of Java Programs the JSnoopy Way

CSE 331. Unit Testing with JUnit

Conduite de Projet Cours 9 Test-Driven Development

Topics. Software Testing Test Driven Development Black Box Testing Unit Testing White Box Testing Coverage Testing Software Debugging

JUnit tes)ng. Elisa Turrini

JUnit 3.8 Documented Using Collaborations

CSCE 747 Unit Testing Laboratory Name(s):

Tutorials for Struts, EJB, xdoclet and eclipse.

Unit Testing. CS 240 Advanced Programming Concepts

Static program checking and verification

Test-Driven Development

Testing with JUnit 1

JUnit 4 and Java EE 5 Better Testing by Design

17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

11 Using JUnit with jgrasp

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

F. Tip and M. Weintraub FUNCTIONAL TESTING

Trusted Components. Bertrand Meyer, Manuel Oriol. Lecture 7: Testing Object-Oriented Software. Ilinca Ciupa, Andreas Leitner, Bertrand Meyer

Chapter 8 Software Testing. Chapter 8 Software testing

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 3

An Eclipse Plug-in for Model Checking

Efficient Regression Test Model for Object Oriented Software

Testing and Debugging

Transcription:

Test automation / JUnit Building automatically repeatable test suites

JUnit in Eclipse For this course, we will use JUnit in Eclipse It is automatically a part of Eclipse One documentation site (all one line www.ibm.com/developerworks/java/tutorials/ j-junit4/section5.html JUnit can be downloaded from www.junit.org Eclipse contains wizards to help with the development of test suites with JUnit JUnit results are presented in an Eclipse window JU 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 JU 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 JU 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 JU 5

Automated testing advantages Unlike manual testing, it is not error-prone and tedious Only feasible way to do regression testing Necessary to run long and complex tests Easily evaluates large quantities of output JU 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 difficult JU 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 testing 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 JU 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 JU 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 5 features such as annotations and static imports 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 JU 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 unit tests A test runner is software that runs tests and reports results JU 11

Example Currency program package currency; public class Currency { protected int amount; protected String type; Currency(int amt, String typ) { amount = amt; type = typ; } public boolean equals(object obj) { return amount == ((Currency) obj).amount && type == ((Currency) obj).type; } protected Currency times(int multiplier) { return new Currency(amount * multiplier, type); } static Currency dollar(int amt) { return new Currency(amt, "Dollar"); } static Currency franc(int amt){ return new Currency(amt, "Franc"); } } JU 12

Example Currency test program 1 of 2 package currency; import org.junit.*; import static org.junit.assert.asserttrue; public class Currency_Test { @BeforeClass public static void setupbeforeclass() throws Exception { } @AfterClass public static void teardownafterclass() throws Exception { } @Before public static void setup() throws Exception { } @After public static void teardown() throws Exception { } JU 13

Example Currency test program 2 of 2 public void testequality() { asserttrue(new Currency(5, "Franc").equals(new Currency(5, "Franc"))); assertfalse(new Currency(5, "Franc").equals(new Currency(6, "Franc"))); assertfalse(new Currency(5, "Franc").equals(new Currency(5, "Currency"))); } public void testmultiplication() { Currency five = new Currency(5, "Dollar"); assertequals(new Currency(15, "Dollar"), five.times(3)); } public void testcurrencytype( ) assertequals("dollar", Currency.dollar(1).type); assertequals("franc", Currency.franc(1).type); } } JU 14

Example running multiple test classes package currency; import org.junit.runner.runwith; import org.junit.runners.suite; @RunWith(Suite.class) @Suite.SuiteClasses( { Currency_BoundaryTest.class, Currency_EquivalenceTest.class, Currency_DecisionTest.class, } ) public class AllTests { } } JU 15

Test fixtures Methods annotated with @Before will execute before every test case Methods annotated with @After will execute after every test case The routine names are your choice @Before public static void setup() { } @After public static void teardown() { } JU 16

Class Test fixtures Methods annotated with @BeforeClass will execute once before all test cases Methods annotated with @AfterClass will execute once after all test cases These are useful if you need to allocate and release expensive resources once @BeforeClass public static void setupbeforeclass() { } @AfterClass public static void teardownafterclass() { } JU 17

Test cases Methods annotated with @Test are considered to be test cases Need before every test that you want to execute @Test public void test_add() { } @Test public void test_tostring() { } JU 18

Ignoring test cases Test cases that are not to be executed are annotated with @Ignore While making corrections due to other test failures Can avoid executing expensive tests Can avoid executing incompletely written tests @Ignore public void test_add() { } @Ignore public void test_tostring() { } JU 19

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

Within a test case Call the methods of the class being tested 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 AssertionError if the test fails JUnit catches these exceptions and shows you the results Only the first failed assert JU 21

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

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 is on the next slide JU 23

Example JUnit test class for counter program public class CounterTest { Counter counter1; @Before public void setup() { // create a test fixture counter1 = new Counter(); } Each test begins with a brand new counter. No need consider the @Test order in which the tests are run. public void testincrement() { asserttrue(counter1.increment() == 1); asserttrue(counter1.increment() == 2); } @Test public void testdecrement() { asserttrue(counter1.decrement() == -1); } } JU 24

List of assert methods 2 assertequals(object expected, Object actual) Uses the equals method to compare the two objects Casting may be required when passing primitives, although autoboxing may be done There is also a version to compare arrays JU 25

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

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

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

Testing for exceptions example public void testanioexceptionisthrown { try { // Code that should raise an IO exception fail("expected an IO exception"); } catch (IOException e) { // This is the expected result, // 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 } } JU 29

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

Automated testing issues It isnʼt easy to see how to unit test GUI code 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 JU 31

Automated testing issues 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 JU 32

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 JU 33

No tool? What do you do if there is no equivalent to JUnit for the language or system in which you have to write test cases? JU 34

Minimal output testing 1 What to do if no tool exists? Use minimal output testing Works for any programming language Works for any system Successful test outputs only the briefest of messages test started test ended JU 35

Minimal output testing 2 Basic structure Test program is a sequence of if-statements with the following structure Note use of msg_id to identify which test failed Rest of test program consists of set up and support routines to simplify programming the condition and the then-phrase if expected_output actual output then print_message(msg_id, ) fi JU 36