JUnit Reading: various web pages

Slides:



Advertisements
Similar presentations
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Advertisements

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.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
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.
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.
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.
CSE 403 Lecture 13 Black/White-Box Testing Reading: Software Testing: Principles and Practices, Ch. 3-4 (Desikan, Ramesh) slides created by Marty Stepp.
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:
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Object-Oriented Software Engineering, Ch. 9
CSE 403 Lecture 12 Effective Unit Testing Reading: The Art of Unit Testing, Ch. 7 (Osherove) slides created by Marty Stepp
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
Test automation / JUnit Building automatically repeatable test suites.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
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.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
1 CSE 331 Unit Testing with JUnit slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Testing code COMP204. How to? “manual” –Tedious, error-prone, not repeatable “automated” by writing code: –Assertions –Junit.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
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.
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University
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.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Test automation / JUnit Building automatically repeatable test suites.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Testing Data Structures
Lecture 5: Test-Driven Development Basics
Black/White-Box Testing Reading:
CompSci 280 S Introduction to Software Development
Software Construction Lab 10 Unit Testing with JUnit
Dept of Computer Science University of Maryland College Park
Unit Testing and Debugging
Introduction to JUnit CS 4501 / 6501 Software Testing
Executing Runtime Checks (For Comp401 and Comp410)
Unit Testing and Debugging
Midterm Review Problems
Defining New Types of Objects, part 3
JUnit 28-Nov-18.
JUnit 28-Nov-18.
Introduction to JUnit CS 4501 / 6501 Software Testing
JUnit 7-Dec-18.
JUnit 11-Jan-19.
CSE 403 Lecture 13 Black/White-Box Testing Reading:
Introduction to JUnit IT323 – Software Engineering II
Test Driven Development
Section 3 Graphs & Testing
CSE 403 JUnit Reading: These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed permission.
Building Java Programs
CSE403 Software Engineering Autumn 2000 More Testing
CMPE212 – Reminders Assignment 2 due this Friday.
Section 4: Graphs and Testing
Test Driven Development
Suggested self-checks: Section 7.11 #1-11
CSE 143 Lecture 5 More ArrayIntList:
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
JUnit 18-Apr-19.
CSE 1020:Software Development
TCSS 360, Spring 2005 Lecture Notes
JUnit 31-May-19.
Junit Tests.
Presentation transcript:

JUnit Reading: various web pages CSE 403 JUnit Reading: various web pages These lecture slides are copyright (C) Marty Stepp, 2007. They may not be rehosted, sold, or modified without expressed permission from the author. All rights reserved.

JUnit and Eclipse Adding JUnit to an Eclipse project: click Project -> Properties -> Add External JARs... -> eclipse folder/plugins/org.junit_x.x.x/junit.jar Create a test case click File -> New -> JUnit Test Case or right-click a file and choose New Test

Creating tests in Eclipse File -> New -> JUnit Test Case Specify class you're testing Eclipse can create stubs of tests for each method for you

A test class (JUnit) import org.junit.*; import static org.junit.Assert.*; public class <name> { ... @Test public void <name>() { .... } Methods with an @Test annotation are flagged as JUnit test cases, run when JUnit runs your test class

JUnit assertions testing methods (name of method MUST start with 'test') Formatting key: optional can be any type (primitive or Object) public void assertTrue(String message, boolean condition) public void assertFalse(String message, boolean condition) public void assertEquals(String message, Object expected, Object actual) public void assertNotEquals(String message, Object expected, Object actual) public void assertSame(String message, Object expected, Object actual) public void assertNotSame(String message, Object expected, Object actual) these methods compare using ==, not .equals public void assertNull(String message, Object obj) public void assertNotNull(String message, Object obj) assert <condition>; public void fail(String message) forcibly causes the test to fail

Running a test Right click its file in the Package Explorer, choose: Run As -> JUnit Test JUnit bar will show green if all tests pass, red if any fail Failure Trace shows which tests failed, if any, and why

A test class (Ruby) require 'test/unit' class <name> < Test::Unit::TestCase def <name> ... assert <condition> end

Ruby assertions assert(boolean, [msg]) - ensures the object/expression is true assert_equal(obj1, obj2, [msg]) - ensures obj1 obj2 is true assert_not_equal(obj1, obj2, [msg]) - ensures obj1 obj2 is false assert_same(obj1, obj2, [msg]) - ensures obj1.equal?(obj2) is true assert_not_same(obj1, obj2, [msg]) - ensures obj1.equal?(obj2) is false assert_nil(obj, [msg]) - ensures obj.nil? is true assert_not_nil(obj, [msg]) - ensures obj.nil? is false assert_match(regexp, string, [msg]) - ensures a string matches the regular expression assert_no_match(regexp, string, [msg]) - ensures a string doesn't match the regex assert_in_delta(expecting, actual, delta, [msg]) - ensures numbers are within delta assert_throws(symbol, [msg]){ block } - ensures a block throws the symbol assert_raises(exceptions){ block } - ensures block raises one of the exceptions assert_nothing_raised(exceptions){ block } - a block doesn’t raise one of the exceptions assert_instance_of(class, obj, [msg]) - ensures obj is the class type assert_kind_of(class, obj, [msg]) - ensures obj is or descends from class assert_respond_to(obj, symbol, [msg]) - ensures obj has a method called symbol assert_operator(obj1, operator, obj2, [msg]) - ensures obj1.operator(obj2) is true assert_send(array, [msg]) - ensures that executing the method listed in array[1] on the object in array[0] with the parameters of array[2 and up] is true flunk([msg]) - Forcibly fails this test

Tests with a timeout import org.junit.*; import static org.junit.Assert.*; public class <name> { ... @Test(timeout = 5000) public void <name>() { .... } The above method will be considered a failure if it doesn't finish running within 5000ms

Testing for exceptions import org.junit.*; import static org.junit.Assert.*; public class <name> { ... @Test(expected = IndexOutOfBoundsException.class) public void <name>() { .... } The above method will PASS if it does throw the exception, and FAIL if it doesn't use this for cases meant to test for expected errors

Setup and teardown methods to be run before or after all test cases import org.junit.*; import static org.junit.Assert.*; public class <name> { ... @Before public void <name>() { .... } @After methods to be run before or after all test cases

Test suites Suites are groups of tests that can be run together import org.junit.runner.*; import org.junit.runners.*; @RunWith(Suite.class) @Suite.SuiteClasses({ <test class name>.class, <test class name>.class }) public class <name> {} Suites are groups of tests that can be run together

What tests should I make? Metrics for whether we have enough tests: code coverage path coverage We can't cover all possible inputs, so: need to pick tests for which we can know/verify correct output equivalence partitioning boundary cases (e.g. min, middle, max) check common expected error cases randomized data? count statement branching/complexity to count test cases needed to test it

JUnit exercise Given a Date class with the following methods: public Date(int year, int month, int day) public Date() // constructs today public void addDays(int days) // advances by days public boolean equals(Object o) public int getDay(), getMonth(), getYear() public int getDaysInMonth() public String getDayOfWeek() public boolean isLeapYear() public void nextDay() // advances by 1 day public String toString() Come up with unit tests to check the following: That no Date object can ever get into an invalid state. That the addDays method works properly. It should also be efficient enough to add 1,000,000 days in a call. a good equals method has some initial checks like checking for null, checking whether this == that, check instanceof, etc... then it calls compareTo

Test-driven development Imagine that we'd like to add a method subtractWeeks to our Date class, that shifts this Date backward in time by the given number of weeks. Write JUnit test code to test this method before it has been written. This way, once we do implement the method, we'll know whether it works.