Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.

Slides:



Advertisements
Similar presentations
Approach of Unit testing with the help of JUnit Unit testing with JUnit2 Unit Testing Testing concepts Unit testing Testing tools JUnit Practical use.
Advertisements

Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
J-Unit Framework.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
1-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.
Approach of Unit testing with the help of JUnit Satish Mishra
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
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.
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.
26-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.
Testowanie kodu Bartosz Baliś, Na podstawie prezentacji Satisha Mishra Iana Sommerville Erica Braude.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
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.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Programmer Testing Testing all things Java using JUnit and extensions.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing in Extreme Programming
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
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.
Test automation / JUnit Building automatically repeatable test suites.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
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.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
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.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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.
Cairo University Faculty of Computers and Information CS251 – Software Engineering Lecture 20: SW Testing Presented by: Mohammad El-Ramly, PhD
Unit Testing CSSE 514 Programming Methods 4/19/01.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
Unit Testing with FlexUnit
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
Test automation / JUnit Building automatically repeatable test suites.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
Software Construction Lab 10 Unit Testing with JUnit
Computer Science 209 Testing With JUnit.
Software Engineering 1, CS 355 Unit Testing with JUnit
JUnit 28-Nov-18.
JUnit 28-Nov-18.
Test-driven development (TDD)
JUnit 7-Dec-18.
JUnit 11-Jan-19.
Test Driven Development
Test Driven Development
JUnit 18-Apr-19.
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
JUnit 31-May-19.
Presentation transcript:

Unit testing Unit testing TDD with JUnit

Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools Examples How to create JUnit TestCase in Eclipse

Why? Unit testing with JUnit 3 Why testing? Improve software design Make software easier to understand Reduce debugging time Catch integration errors In short, to Produce Better Code Preconditions Working code Good set of unit tests

What should be tested ? Unit testing with JUnit 4 Test for boundary conditions Test for both success and failure Test for general functionality Etc..

When to start testing Unit testing with JUnit 5 Software quality and testing is a life-cycle process

When to start testing... Unit testing with JUnit 6 At the time of starting the projects How we start the projects ?? Do we have any formal way ??

The V-model of development Unit testing with JUnit 7

Fact of testing Unit testing with JUnit 8 Testing does not guarantee the absence of defects

What is test case Unit testing with JUnit 9 A test case is a document that describes an input, action, or event and an expected response, to determine if a feature of an application is working correctly

Good test case design Unit testing with JUnit 10 An good test case satisfies the following criteria: Reasonable probability of catching an error Does interesting things Doesn’t do unnecessary things Neither too simple nor too complex Not redundant with other tests Makes failures obvious Mutually Exclusive, Collectively Exhaustive

Test case design technique Unit testing with JUnit 11 Test case design techniques can be broadly split into two main categories Black box (functional) White box (structural)

Black Box tests Unit testing with JUnit 12 Targeted at the apparent simplicity of the software Makes assumptions about implementation Good for testing component interactions Tests the interfaces and behavior InputOutput

White Box tests Unit testing with JUnit 13 Targeted at the underlying complexity of the software Intimate knowledge of implementation Good for testing individual functions Tests the implementation and design InputOutput

Test case writing example Unit testing with JUnit 14 Suppose we have two parameters we want to cover in a set of tests. Parameters are as follows.. Operating system Win98 Win2k Winxp Printers HP 4100 HP 4200 How We should write test case for this ??

Types of Tests Unit testing with JUnit 15 Unit Individual classes or types Component Group of related classes or types Integration Interaction between classes

What is a testing framework? Unit testing with JUnit 16 A test framework provides reusable test functionality which: Is easier to use (e.g. don’t have to write the same code for each class) Is standardized and reusable Provides a base for regression tests

Why use a testing framework? Unit testing with JUnit 17 Each class must be tested when it is developed Each class needs a regression test Regression tests need to have standard interfaces Thus, we can build the regression test when building the class and have a better, more stable product for less work

Regression testing Unit testing with JUnit 18 New code and changes to old code can affect the rest of the code base ‘Affect’ sometimes means ‘break’ We need to run tests on the old code, to verify it works – these are regression tests Regression testing is required for a stable, maintainable code base

Testing tools Unit testing with JUnit 19 Tools are part of the quality equation, but not the entire equation

JUnit Unit testing with JUnit 20 JUnit is a framework for writing unit tests A unit test is a test of a single class A test case is a single test of a single method A test suite is a collection of test cases Unit testing is particularly important when software requirements change frequently Code often has to be refactored to incorporate the changes Unit testing helps ensure that the refactored code continues to work

JUnit.. Unit testing with JUnit 21 JUnit helps the programmer: Define and execute tests and test suites Formalize requirements and clarify architecture Write and debug code Integrate code and always be ready to release a working version

What JUnit does Unit testing with JUnit 22 JUnit runs a suite of tests and reports results For each test in the test suite: JUnit calls setUp() This method should create any objects you may need for testing

What JUnit does… Unit testing with JUnit 23 JUnit calls one test method The test method may comprise multiple test cases; that is, it may make multiple calls to the method you are testing In fact, since it’s your code, the test method can do anything you want The setUp() method ensures you entered the test method with a virgin set of objects; what you do with them is up to you JUnit calls tearDown() This method should remove any objects you created

Creating a test class in JUnit Unit testing with JUnit 24 Define a subclass of TestCase Override the setUp() method to initialize object(s) under test. Override the tearDown() method to release object(s) under test. Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results. Define a static suite() factory method that creates a TestSuite containing all the testXXX() methods of the TestCase. Optionally define a main() method that runs the TestCase in batch mode.

Fixtures Unit testing with JUnit 25 A fixture is just a some code you want run before every test You get a fixture by overriding the method protected void setUp() { …} The general rule for running a test is: protected void runTest() { setUp(); tearDown(); } so we can override setUp and/or tearDown, and that code will be run prior to or after every test case

Implementing setUp() method Unit testing with JUnit 26 Override setUp() to initialize the variables, and objectssetUp Since setUp() is your code, you can modify it any way you like (such as creating new objects in it) Reduces the duplication of code

Implementing the tearDown() method Unit testing with JUnit 27 In most cases, the tearDown() method doesn’t need to do anything The next time you run setUp(), your objects will be replaced, and the old objects will be available for garbage collection Like the finally clause in a try-catch-finally statement, tearDown() is where you would release system resources (such as streams)

The structure of a test method Unit testing with JUnit 28 A test method doesn’t return a result If the tests run correctly, a test method does nothing If a test fails, it throws an AssertionFailedError The JUnit framework catches the error and deals with it; you don’t have to do anything

Test suites Unit testing with JUnit 29 In practice, you want to run a group of related tests (e.g. all the tests for a class) To do so, group your test methods in a class which extends TestCase Running suites we will see in examples

assert X methods Unit testing with JUnit 30 static void assertTrue(boolean test ) static void assertFalse(boolean test ) assertEquals( expected, actual ) This method is heavily overloaded: arg1 and arg2 must be both objects or both of the same primitive type For objects, uses your equals method, if you have defined it properly, as public boolean equals(Object o) --otherwise it uses ==. assertSame(Object expected, Object actual ) Asserts that two objects refer to the same object (using == ) assertNotSame(Object expected, Object actual ) assertNull(Object object )

assert X methods Unit testing with JUnit 31 assertNotNull(Object object ) fail() Causes the test to fail and throw an AssertionFailedError Useful as a result of a complex test, when the other assert methods aren’t quite what you want. All the above may take an optional String message as the first argument, for example, static void assertTrue(String message, boolean test )

Organize The Tests Unit testing with JUnit 32 Create test cases in the same package as the code under test For each Java package in your application, define a TestSuite class that contains all the tests for validating the code in the package Define similar TestSuite classes that create higher-level and lower-level test suites in the other packages (and sub- packages) of the application Make sure your build process includes the compilation of all tests

JUnit framework Unit testing with JUnit 33

Example: Counter class Unit testing with JUnit 34 For the sake of example, we will create and test a trivial “counter” class The constructor will create a counter and set it to zero The increment method will add one to the counter and return the new value The decrement method will subtract one from the counter and return the new value

Example: Counter class Unit testing with JUnit 35 We write the test methods before we write the code This has the advantages described earlier Depending on the JUnit tool we use, we may have to create the class first, and we may have to populate it with stubs (methods with empty bodies) Don’t be alarmed if, in this simple example, the JUnit tests are more code than the class itself

JUnit tests for Counter Unit testing with JUnit 36 public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } protected void tearDown() { } // no resources to release

JUnit tests for Counter… Unit testing with JUnit 37 public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } public void testDecrement() { assertTrue(counter1.decrement() == -1); } }// End from last slide

The Counter class itself Unit testing with JUnit 38  public class Counter { int count = 0; public int increment() { return ++count; } public int decrement() { return --count; }  public int getCount() { return count; } }

TestCase lifecycle Unit testing with JUnit setUp 2. testXXX() 3. tearDown() 4. Repeats 1 through 3 for each testXXX method…

Test Suites Unit testing with JUnit 40 import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import example.SimpleTest; import example.HtmlDocumentTest; public class AllTests { static public Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(SimpleTest.class); suite.addTestSuite(HtmlDocumentTest.class); return suite; } Demo

JUnit Best Practices Unit testing with JUnit 41 Separate production and test code But typically in the same packages Compile into separate trees, allowing deployment without tests Don’t forget OO techniques, base classing Test-driven development 1. Write failing test first 2. Write enough code to pass 3. Refactor 4. Run tests again 5. Repeat until software meets goal 6. Write new code only when test is failing

Why JUnit Unit testing with JUnit 42 Allow you to write code faster while increasing quality Elegantly simple Check their own results and provide immediate feedback Tests is inexpensive Increase the stability of software Developer tests Written in Java Free Gives proper uniderstanding of unit testing

Problems with unit testing Unit testing with JUnit 43 JUnit is designed to call methods and compare the results they return against expected results This ignores: Programs that do work in response to GUI commands Methods that are used primary to produce output

Problems with unit testing… Unit testing with JUnit 44 Heavy use of JUnit encourages a “functional” style, where most methods are called to compute a value, rather than to have side effects This can actually be a good thing Methods that just return results, without side effects (such as printing), are simpler, more general, and easier to reuse