CS427: Software Engineering I

Slides:



Advertisements
Similar presentations
Introduction to Software Testing (2nd edition) Chapter 3 Test Automation Paul Ammann & Jeff Offutt aretest/ Second.
Advertisements

J-Unit Framework.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
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.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
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.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
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.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Test automation / JUnit Building automatically repeatable test suites.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
A tool for test-driven development
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
JUnit Automated Software Testing Framework Advanced Material Paul Ammann & Jeff Offutt
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
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.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Introduction to Software Testing (2nd edition) Chapter 3 Test Automation Paul Ammann & Jeff Offutt Updated.
SE 433/333 Software Testing & Quality Assurance Dennis Mumaugh, Instructor Office: CDM, Room 428 Office Hours: Tuesday, 4:00 –
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Test automation / JUnit Building automatically repeatable test suites.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Advanced programming Practices
Lecture 5: Test-Driven Development Basics
CompSci 280 S Introduction to Software Development
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
CS427: Software Engineering I
Dept of Computer Science University of Maryland College Park
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
Input Space Partition Testing CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Software Engineering 1, CS 355 Unit Testing with JUnit
Classes and Objects 2nd Lecture
Design by Contract Fall 2016 Version.
JUnit Automated Software Testing Framework
Introduction to Data Structures
Introduction to Software Testing Chapter 3 Test Automation
Introduction to JUnit CS 4501 / 6501 Software Testing
Introduction to Software Testing (2nd edition) Chapter 4 TDD Example
Java Programming Arrays
Test Automation CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Data Structures and Algorithms for Information Processing
Advanced programming Practices
JUnit Reading: various web pages
Java & Testing.
JUnit SWE 619 Spring 2008.
Unit Testing.
Presentation transcript:

CS427: Software Engineering I Grigore Rosu

Today’s goals Quick Logistics Extreme Programming (XP) vs. Scrum More Testing

Course Logistics Wiki: http://wiki.cites.illinois.edu/wiki/display/cs427fa17 Piazza: https://piazza.com/class#fall2017/cs427 Compass: https://compass2g.illinois.edu All class-wide announcements through compass or my.cs MP3 due today, 23:59; MP4 out today Follow the rules and the format specified on Wiki! Office hours by request (Piazza) Qing Ye offers regular office hours on Tu/Th 3-4pm, SC4105 Used also for makeup quizzes: Mon quiz ~> Tu; Wed quiz ~> Th Reminder MPs (30%), final exam (25%), project (40%), quizzes (5%) Bonus up to 3% for active participation (lectures, Piazza, …)

Xp vs. Scrum

Class Question (Last Wednesday) What is the difference between XP and Scrum? Quite similar: both agile, regular meetings/iterations, require planning, customer interaction, etc. Here is a good link, and another good link; and another “if you walked in on a team doing one of these processes you might have hard time quickly deciding whether you had walked in on a Scrum team or an XP team.” Bottom line: XP’s iterations (1-2 w) shorter than Scrum’s sprints (2-4 w) XP more flexible wrt change in plans; Scrum more rigid XP – customer chooses; Scrum – team chooses Scrum does not prescribe any processes/activities, while XP does: pair programming, test-driven development XP does not mandate planning ceremonies and artifacts, while Scrum does Teams using Scrum often adopt XP practices, and vice-versa “start with Scrum and then invent your own version of XP.”

Testing, Testing, Testing

Recall: Why test? Improve quality – find faults (or defects, or bugs) Measure quality Prove there are no faults? (Is it possible?) Determine if software is ready to be released Determine what to work on See if you made a mistake Learn the software

Recall: What is a test? Run program with known inputs (test inputs/data), check results (with test oracles) Tests pass (green) or fail (red) Tests can document faults Tests can document code Important terminology to remember: Mistake, fault (or defect, or bug), failure, error Oracle

Recall Terminology: Mistake, Fault/Bug, Failure, Error Running the test inputs … Programmer makes a mistake Fault (defect, bug) appears in the program Fault remains undetected during testing Program failure occurs during execution (program behaves unexpectedly) Error: difference between computed, observed, or measured value or condition and true, specified, or theoretically correct value or condition

Recall Terminology: Mistake, Fault/Bug, Failure, Error Based on test oracles … Programmer makes a mistake Fault (defect, bug) appears in the program Fault remains undetected during testing Program failure occurs during execution (program behaves unexpectedly) Error: difference between computed, observed, or measured value or condition and true, specified, or theoretically correct value or condition

Recall: Test Input vs. Test Oracle Objective: double the balance and then add 10 int calAmount () { int ret = balance * 3; ret = ret + 10; return ret; } test input test oracle void testCalAmount() { Account account = new Account(); account.setBalance(1); int amount = account.calAmount(); assertTrue(amount == 12); }

Recall: Junit Basics Open source (junit.org) Java testing framework used to write and run repeatable automated tests A structure for writing test drivers JUnit features include: Assertions for testing expected results Sharing common test data among tests Test suites for easily organizing and running tests Test runners, both graphical and textual JUnit is widely used in industry Can be used as stand alone Java programs (from command line) or from an IDE such as IntelliJ or Eclipse Introduction to Software Testing (Ch 1) © Ammann & Offutt

Recall: JUnit Tests JUnit can be used to test … … an entire object … part of an object – method or interacting methods … interaction between several objects Primarily unit & integration testing, not system testing Each test is embedded into one test method A test class contains one or more test methods Test classes include: A test runner to run the tests - main() A collection of test methods Methods to set up the state before and update the state after each test and before and after all tests Introduction to Software Testing (Ch 1) © Ammann & Offutt

Recall: Writing Tests for JUnit Need to use methods of junit.framework.assert class javadoc gives a complete description of its capabilities Each test method checks a condition (assertion) and reports to the test runner whether the test succeeded or failed The test runner uses the result to report to the user (in command line mode) or update the display (in an IDE) All of the methods return void A few representative methods (of junit.framework.assert): assertTrue(boolean) assertTrue(String, boolean) assertEquals(Object, Object) assertNull(Object) Fail(String) Hm, did I do that in MP2? Introduction to Software Testing (Ch 1) © Ammann & Offutt

Recall: Example JUnit Test public class Calc { public long add (int a, int b) return a + b; } Expected result import org.junit.Test; import static org.junit.Assert.*; public class calcTest { private Calc calc; @Test public void testAdd() calc = new Calc (); assertEquals((long) 5, calc.add(2,3)); } The test No assertion = no junit test … Introduction to Software Testing (Ch 1) © Ammann & Offutt

JUnit Test Fixtures A test fixture is the state of the test Objects and variables used by more than one test Initializations (prefix values) Reset values (postfix values) Different tests can use objects without sharing state Objects in fixtures declared as instance variables They should be initialized in a @Before method JUnit runs them before every @Test method Can be deallocated or reset in an @After method JUnit runs them after every @Test method Introduction to Software Testing (Ch 1) © Ammann & Offutt

Testing the Immutable Stack Class public class Stack { … public String toString() { // EFFECTS: Returns String representation // of this Stack from top to bottom. StringBuffer buf = new StringBuffer("{"); for (int i = size-1; i >= 0; i--) if (i < (size-1)) buf.append (", "); buf.append(elements[ i ].toString()); } buf.append("}"); return buf.toString(); … public boolean repOk() { if (elements == null) return false; if (size != elements.length) return false; for (int i = 0; i < size; i++) { if (elements[i] == null) return false; } return true; Introduction to Software Testing (Ch 1) © Ammann & Offutt

Stack Test Class Classes to import: Pre-test setup method (prefix) : import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; import junit.framework.JUnit4TestAdapter; Classes to import: private Stack stack; // setUp method using @Before syntax // @Before methods are run before each test @Before public void runBeforeEachTest() { stack = new Stack(); } Pre-test setup method (prefix) : // tear-down method using @After // @After methods are run after each test @After public void runAfterEachTest() { stack = null; } Post-test teardown method (postfix) : Introduction to Software Testing (Ch 1) © Ammann & Offutt

Stack Test Cases @Test public void testToString() { stack = stack.push (new Integer (1)); stack = stack.push (new Integer (2)); assertEquals ("{2, 1}", stack.toString()); } @Test public void testRepOk() { boolean result = stack.repOk(); assertEquals (true, result); stack = stack.push (new Integer (1)); result = stack.repOk(); stack = stack.pop(); stack = stack.push (new Integer (1)); stack.top(); } A problem with this “test” is that it actually combines four separate tests in one method 1 Without automation, large tests have the advantage of reducing costs of running many tests 2 3 With automation, small tests allow us to more easily identify failures … 4 Introduction to Software Testing (Ch 1) © Ammann & Offutt

Stack Test Cases (2) @Test public void testRepOkC() { } @Test public void testRepOkA() { boolean result = stack.repOk(); assertEquals (true, result); } @Test public void testRepOkB() stack = stack.push (new Integer(1)); @Test public void testRepOkC() { stack = stack.push (new Integer (1)); stack = stack.pop(); boolean result = stack.repOk(); assertEquals (true, result); } @Test public void testRepOkD() stack.top(); Introduction to Software Testing (Ch 1) © Ammann & Offutt

Running from Command Line This is all we need to run JUnit in an IDE We could use a main() for command line execution … but even better is to run from the Runner Introduction to Software Testing (Ch 1) © Ammann & Offutt

Running All Tests The name of your test class import org.junit.runner.RunWith; import org.junit.runners.Suite; import junit.framework.JUnit4TestAdapter; // This section declares all of the test classes in the program. @RunWith (Suite.class) @Suite.SuiteClasses ({ StackTest.class }) // Add test classes here. public class AllTests { // Execution begins at main(). In this test class, we will execute // a text test runner that will tell you if any of your tests fail. public static void main (String[] args) junit.textui.TestRunner.run (suite()); } // The suite() method is helpful when using JUnit 3 Test Runners or Ant. public static junit.framework.Test suite() return new JUnit4TestAdapter (AllTests.class); The name of your test class Introduction to Software Testing (Ch 1) © Ammann & Offutt

How to Run Tests JUnit provides test drivers Character-based test driver runs from the command line GUI-based test driver: junit.swingui.TestRunner Allows programmer to specify the test class to run Creates a “Run” button If a test fails, JUnit gives the location of the failure and any exceptions that were thrown Introduction to Software Testing (Ch 1) © Ammann & Offutt

Advanced Topics in JUnit Assertion patterns How to decide if your test passes State testing vs. interaction testing patterns Parameterized JUnit tests How to describe and run very similar tests JUnit theories Applying the contract model to testing AAA model: Assume, Act, Assert Very powerful approach But also still a work in progress

Assertion Patterns State Testing Patterns Interaction Assertions Final State Assertion Most Common Pattern: Arrange. Act. Assert. Guard Assertion Assert Both Before and After The Action (Precondition Testing) Delta Assertion Verify a Relative Change to the State Custom Assertion Encodes Complex Verification Rules Interaction Assertions Verify Expected Interactions Heavily used in Mocking tools Very Different Analysis Compared to State Testing Resource: http://martinfowler.com/articles/mocksArentStubs.html Introduction to Software Testing (Ch 1) © Ammann & Offutt

Parameterized Tests Problem: Testing a function with similar values How to avoid test code bloat? Simple example: Adding two numbers Adding given pair of numbers is just like adding any other pair You really only want to write one test Parameterized unit tests call constructor for each logical set of data values Same tests are then run on each set of data values List of data values identified with @Parameters annotation Introduction to Software Testing (Ch 1) © Ammann & Offutt

Parameterized Unit Tests import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import static org.junit.Assert.*; import java.util.*; @RunWith(Parameterized.class) public class ParamTest { public int sum, a, b; public ParamTest (int sum, int a, int b) { this.sum = sum; this.a = a; this.b = b; } @Parameters public static Collection<Object[]> parameters() { return Arrays.asList (new Object [][] {{0, 0, 0}, {2, 1, 1}}); @Test public void additionTest() { assertEquals(sum, a+b); } Introduction to Software Testing (Ch 1) © Ammann & Offutt

JUnit Theories These Are Unit Tests With Actual Parameters So Far, We’ve Only Seen Parameterless Test Methods Contract Model: Assume, Act, Assert Assumptions (Preconditions) Limit Values Appropriately Action Performs Activity Under Scrutiny Assertions (Postconditions) Check Result @Theory public void removeThenAddDoesNotChangeSet( Set<String> set, String string) { // Parameters! assumeTrue(set.contains(string)) ; // Assume Set<String> copy = new HashSet<String>(set); // Act copy.remove(string); copy.add(string); assertTrue (set.equals(copy)); // Assert // System.out.println(“Instantiated test: “ + set + “, “ + string); } Introduction to Software Testing (Ch 1) © Ammann & Offutt

Question: Where Does Data Come From? Answer: All combinations of values from @DataPoint annotations where assume clause is true Four (of nine) combinations in this particular case Note: @DataPoint format is an array. @DataPoints public static String[] string = {"ant", "bat", "cat"}; public static Set[] sets = { new HashSet(Arrays.asList("ant", "bat")), new HashSet(Arrays.asList(“bat", “cat", “dog“, “elk”)), new HashSet(Arrays.asList(“Snap”, “Crackle”, “Pop")) }; Introduction to Software Testing (Ch 1) © Ammann & Offutt

JUnit Theories Need BoilerPlate import org.junit.*; import org.junit.runner.RunWith; import static org.junit.Assert.*; import static org.junit.Assume.*; import org.junit.experimental.theories.DataPoint; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import java.util.*; @RunWith(Theories.class) public class SetTheoryTest { … // See Earlier Slides } Introduction to Software Testing (Ch 1) © Ammann & Offutt

JUnit Resources Some JUnit tutorials JUnit: Download, Documentation http://open.ncsu.edu/se/tutorials/junit/ (Laurie Williams, Dright Ho, and Sarah Smith ) http://www.laliluna.de/eclipse-junit-testing-tutorial.html (Sascha Wolski and Sebastian Hennebrueder) http://www.diasparsoftware.com/template.php?content= jUnitStarterGuide (Diaspar software) http://www.clarkware.com/articles/JUnitPrimer.html (Clarkware consulting) JUnit: Download, Documentation http://www.junit.org/ Introduction to Software Testing (Ch 1) © Ammann & Offutt

Summary What test values to use ? The only way to make testing efficient as well as effective is to automate as much as possible JUnit provides a very simple way to automate our unit tests It is no “silver bullet” however … it does not solve the hard problem of testing : What test values to use ? This is test design … the purpose of test criteria Introduction to Software Testing (Ch 1) © Ammann & Offutt