CompSci 280 S Introduction to Software Development

Slides:



Advertisements
Similar presentations
Defect testing Objectives
Advertisements

J-Unit Framework.
Testing and Quality Assurance
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
CMSC 345, Version 11/07 SD Vick from S. Mitchell Software Testing.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
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.
Software Testing Sudipto Ghosh CS 406 Fall 99 November 9, 1999.
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:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Introduction to Unit Testing Jun-Ru Chang 2012/05/03.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 23 Slide 1 Software testing Slightly adapted by Anders Børjesson.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
CMSC 345 Fall 2000 Unit Testing. The testing process.
Chapter 8 – Software Testing Lecture 1 1Chapter 8 Software testing The bearing of a child takes nine months, no matter how many women are assigned. Many.
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.
Software Testing. 2 CMSC 345, Version 4/12 Topics The testing process  unit testing  integration and system testing  acceptance testing Test case planning.
Introduction to Software Testing. Types of Software Testing Unit Testing Strategies – Equivalence Class Testing – Boundary Value Testing – Output Testing.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Software Testing Reference: Software Engineering, Ian Sommerville, 6 th edition, Chapter 20.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
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.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 23 Slide 1 Software testing.
Software Testing Reference: Software Engineering, Ian Sommerville, 6 th edition, Chapter 20.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Testing Data Structures
CompSci 280 S Introduction to Software Development
CompSci 230 S Programming Techniques
Lecture 5: Test-Driven Development Basics
Unit Testing with JUnit
Testing Tutorial 7.
Software Testing.
Chapter 13 Solver .out File and CCL
The Joy of Breaking Code Testing Logic and Case Selection
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
IS301 – Software Engineering V:
Input Space Partition Testing CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Chapter 8 – Software Testing
Chapter 7 Software Testing.
Software Engineering 1, CS 355 Unit Testing with JUnit
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
UNIT-4 BLACKBOX AND WHITEBOX TESTING
History, Characteristics and Frameworks
Testing and Test-Driven Development CSC 4700 Software Engineering
Introduction to JUnit CS 4501 / 6501 Software Testing
Coding Concepts (Basics)
Introduction to JUnit IT323 – Software Engineering II
Automation of Testing in the Distributed Common Ground System (Army)
Automation of Testing in the Distributed Common Ground System (Army)
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Section 3 Graphs & Testing
Section 4: Graphs and Testing
Go to pollev.com/cse143.
CSE 143 Lecture 5 More ArrayIntList:
JUnit Reading: various web pages
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Chapter 7 Software Testing.
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Testing Slides adopted from John Jannotti, Brown University
Presentation transcript:

CompSci 280 S2 2107 Introduction to Software Development JUnit

Today’s Agenda Topics: Reading: Introduction JUnit Testing Strategies Equivalence Partition Boundary Analysis Testing Reading: Software Engineering 10th Edition, Ian Somerville Chapter 8 Lecture20

1.Introduction Difficulties of testing Perception by some developers and managers: Testing is seen as a novice's job. Assigned to the least experienced team members. Done as an afterthought (if at all). "My code is good; it won't have bugs. I don't need to test it." "I'll just find the bugs by running the client program." Limitations of what testing can show you: It is impossible to completely test a system. Testing does not always directly reveal the actual bugs in the code. Testing does not prove the absence of errors in software. Lecture20

1.Introduction Unit Testing unit testing: Looking for errors in a subsystem in isolation. Generally a "subsystem" means a particular class or object. The Java library JUnit helps us to easily perform unit testing. The basic idea: For a given class Foo, create another class FooTest to test it, containing various "test case" methods to run. Each method looks for particular results and passes / fails. JUnit provides "assert" commands to help us write tests. The idea: Put assertion calls in your test methods to check things you expect to be true. If they aren't, the test will fail. Lecture20

1.Introduction JUnit - History Kent Beck developed the first xUnit automated test tool for Smalltalk in mid-90’s Beck and Gamma (of design patterns Gang of Four) developed JUnit on a flight from Zurich to Washington, D.C. Martin Fowler: “Never in the field of software development was so much owed by so many to so few lines of code.” Junit has become the standard tool for Test-Driven Development in Java (see Junit.org) Junit test generators now part of many Java IDEs (Eclipse, BlueJ, Jbuilder, DrJava) Xunit tools have since been developed for many other languages (Perl, C++, Python, Visual Basic, C#, …) Lecture20

2.JUnit & Eclipse To add JUnit to an Eclipse project, click: Project → Properties → Build Path → Libraries → Add Library... → Junit → JUnit 4 → Finish To create a test class Select File → New → JUnit Test Case Enter Name (test) Enter Class Under test (source class) Select method(s) Note: Name of class is important – should be of the form TestMyClass or MyClassTest JUnit can create stubs of method tests for you. JUnit runs tests and reports results Lecture20

2.JUnit & Eclipse Example 1: Calculator First, create a class in JUnit Calculator:- perform arithmetic operations add, subtract, multiply and divide public class Calculator { public int add( int number1, int number2 ) { return number1 + number2; } public int multiply( int number1, int number2 ) { return number1 * number2; ... Lecture20

2.JUnit & Eclipse Example 1: CalculatorTest Create a test class Enter name and class under Test, Select methods Note: A method with @Test is flagged as a JUnit test case. All @Test methods run when JUnit runs your test class. Lecture20

2.JUnit & Eclipse JUnit Assertion Methods Each method can also be passed a string to display if it fails: e.g. assertEquals("message", expected, actual) Flags discrepancies between the “expected” value and the result returned by the “class under test” method( ) assertTrue(test) fails if the boolean test is false assertFalse(test) fails if the boolean test is true assertEquals(expected, actual) fails if the values are not equal assertSame(expected, actual) fails if the values are not the same (by ==) assertNotSame(expected, actual) fails if the values are the same (by == assertNull(value) fails if the given value is not null assertNotNull(value) fails if the given value is null fail() causes current test to immediately fail Lecture20

2.JUnit & Eclipse Running a Test Right click it in the Eclipse Package Explorer at left; choose: Run As → JUnit Test The JUnit bar will show green if all tests pass, red if any fail. The Failure Trace shows which tests failed, if any, and why. @Test public void testAdd() { Calculator calc = new Calculator(); assertEquals(13, calc.add(3,10)); } Lecture20

2.JUnit & Eclipse Exercise 1 Complete all test cases: Multiple Subtract Divide Lecture20

2.Junit & Eclipse Fixture Tests may require common resources to be set up A test fixture is a fixed state of a set of objects used as a baseline for running tests. Examples of fixtures: Preparation of input data and setup/creation of fake or mock objects Loading a database with a specific, known set of data Copying a specific known set of files creating a test fixture will create a set of objects initialized to certain states. A fixture can be created by overriding the setUp and tearDown methods Note: setUp is invoked before each test, tearDown after Lecture20

2.JUnit & Eclipse Using Test fixtures Pattern follows programming by contract paradigm: Set up pre-conditions Exercise functionality being tested Check post-conditions public class CalculatorTest { private Calculator calc; /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { calc = new Calculator(); } @After public void tearDown() throws Exception { calc = null; @Test public void testAdd() { assertEquals(13, calc.add(3,10)); passed Lecture20

2.JUnit & Eclipse Things to Notice: Use the specific method signature – public void testWhatever() Allows them to be found and collected automatically by JUnit You cannot test every possible input, parameter value, etc. So you must think of a limited set of tests likely to expose bugs. Think about boundary cases positive; zero; negative numbers right at the edge of an array or collection's size Think about empty cases and error cases 0, -1, null; an empty list or array test behavior in combination maybe add usually works, but fails after you call remove make multiple calls; maybe size fails the second time only Lecture20

2.JUnit & Eclipse Example 2 A simple case: import java.util.*; public class SampleTest { private List<Integer> emptyList; @Before public void setUp() throws Exception { emptyList = new ArrayList<Integer>(); } @After public void tearDown() throws Exception { emptyList = null; @Test public void test() { assertEquals("Size of an empty list should be zero.", 0, emptyList.size()); assertTrue("An empty bowl should report empty.",emptyList.isEmpty()); Called before every test case method Called after every test case method Lecture20

2.JUnit & Eclipse Example 3: IntList Revisited: IntList public class IntListTest { IntList list ; @Before public void setUp() throws Exception { int[] input = { 1, 93, 2, 5, -17 }; list = new IntList(input); } @After public void tearDown() throws Exception { list = null; @Test public void testCountNumZeros() { assertEquals(0, list.countNumZeros()); Lecture20

2.Testing Strategies Automation Issues Some artifact testing cannot easily be automated (e.g., user interfaces) Some potential failure situations cannot be easily exercised (e.g., timing problems in real-time or concurrent software) Still a lot of work needed to support traceability: relationship between tests executed and client's requirements Lecture20

2.Testing Strategies Developing Good Tests Purpose of testing: Detect the presence of faults Detect faults by executing the code so that a failure is caused Assuming a fault exists: the higher the probability of causing a failure, the better the quality of the test Look at where faults are likely to be Consider quality of the test suite (individual tests of limited value) Consider the example… Lecture20

2.Testing Strategies Example Tab stops are specific locations on a line that are used for alignment. A character that appears after a tab character will be aligned to the nearest tab stop after the location of the tab character. One means of indicating tab stops is the number of characters that appear between tab stops. The example below shows, on the left, three lines of text with tab characters indicated by “\t”. On the right, the same lines of text are shown how they would appear after aligning to the tab stops, assuming the tab stops are every 4 characters. Consider implementing a method that provides the following functionality: Lecture20

2.Testing Strategies Example Inputs: string where the only whitespace characters are space and tab, integer indicating what tab stop to use Outputs: the number of space characters between the beginning of the string and the first non-whitespace character, assuming any tab characters are replaced by enough spaces corresponding to the tabstop Tab stop width = 4 spaces Examples: System.out.println(leadingSpacesCount("Testing \tis fun", 4)); System.out.println(leadingSpacesCount("\t\tTesting is fun", 4)); System.out.println(leadingSpacesCount("\tTesting", 4)); System.out.println(leadingSpacesCount(" \tTesting", 4)); + + + Testing 8 4 Lecture20

2.Testing Strategies Test Suite Quality Consider this test suite: Input = (“Fun", 4), Expected Output: 0 Is it improved by adding this test case? Input = (“Fun ", 4), Expected Output: 0 More test cases does not mean a better quality test suite Lecture20

2.Testing Strategies Choosing unit test cases The test cases should show that, when used as expected, the component that you are testing does what it is supposed to do. If there are defects in the component, these should be revealed by test cases. This leads to 2 types of unit test case: The first of these should reflect normal operation of a program and should show that the component works as expected. The other kind of test case should be based on testing experience of where common problems arise. It should use abnormal inputs to check that these are properly processed and do not crash the component. Lecture20

3.Equivalence Partition Partition testing, where you identify groups of inputs that have common characteristics and should be processed in the same way. You should choose tests from within each of these groups. Each of these classes is an equivalence partition or domain where the program behaves in an equivalent way for each class member. Lecture20

3.Equivalence Partition Examples Enter an integer between 4 and 10 Username (6-10 characters) Age (18-80 years, except 60-65 years) Invalid partition Valid partition Less than 4 e.g. 3, 2, 1, 0, -1… 4, 5, 6, 7, 8, 9 10 More than 10 e.g. 11, 12 … Invalid partition Valid partition Less than 6 e.g. 5, 4 .. 0 6, 7, 8, 9, 10 More than 10 e.g. 11, 12 etc Invalid partition Valid partition Less than 18 e.g. 17, 16…0 18, 19, … 59 60, 61 .. 65 66, 67.. 80 81, 82 … Lecture20

4.Boundary Analysis Testing Boundary testing is the process of testing between extreme ends or boundaries between partitions of the input values. Errors more likely to occur at extreme values of a variable So these extreme ends like Start- End, Lower- Upper, Maximum-Minimum are called boundary values and the testing is called "boundary testing". The basic idea in boundary value testing is to select input variable values at their: Minimum Just above the minimum A nominal value Just below the maximum Maximum Lecture20

4.Boundary Analysis Testing Examples Enter an integer between 4 and 10 Username (6-10 characters) Invalid partition – valid partition LOWER boundary Invalid partition – valid partition UPPER boundary Just below the boundary Just above the boundary 3 4 10 11 Invalid partition – valid partition LOWER boundary Invalid partition – valid partition UPPER boundary Just below the boundary Just above the boundary 5 characters 6 characters 10 characters 11 characters Lecture20