Introduction to Eclipse Unit Testing and JUnit David

  • Slides: 14
Download presentation
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz Object Oriented Design Course

Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz Object Oriented Design Course

Eclipse l l Eclipse is a kind of universal tool platform an open extensible

Eclipse l l Eclipse is a kind of universal tool platform an open extensible IDE for anything and nothing in particular. plugins • • JDT CDT UML Many more 10/3/2020 Object Oriented Design Course 2

Working with Eclipse – Live Demonstration l l l Project Views Code Assist Errors

Working with Eclipse – Live Demonstration l l l Project Views Code Assist Errors Debugging 10/3/2020 Object Oriented Design Course 3

Unit Tests First level of testing l Done by the programmer l Part of

Unit Tests First level of testing l Done by the programmer l Part of the coding process l Delivered with the code l Part of the build process l 10/3/2020 Object Oriented Design Course 4

What’s a Unit? l Unit tests should be written in test classes, that replace

What’s a Unit? l Unit tests should be written in test classes, that replace ‘drivers’ For example, class Stack l Tests are per-functionality l Unit testing can have several levels l • Class Stack Has push, pop, count, … • Class Test. Stack Has test. Push, test. Pop • Not per method (test. Push. Null) • Not per class (test. Iterators) 10/3/2020 Object Oriented Design Course 5

What’s a Unit Test? l Call methods, and assert conditions void test. Push() {

What’s a Unit Test? l Call methods, and assert conditions void test. Push() { s = new Stack(); s. push(new Integer(10)); assert. Equals(s. count() == 1); } l l l Tests check themselves • Only output if a test fails Write tests after writing interface Run all tests after each change 10/3/2020 Object Oriented Design Course 6

Running Unit Tests l A main() is required to run tests There are better

Running Unit Tests l A main() is required to run tests There are better options l Unit Testing Frameworks l • JUnit for Java • Cpp. Unit for C++ • Graphical user interface • Easily choose which tests to run • Elegantly support for test suites 10/3/2020 Object Oriented Design Course 7

Coding with Unit Tests l Part of the design process • Design for testability

Coding with Unit Tests l Part of the design process • Design for testability --> Modularity l Part of the coding process l Part of the build process • Test-first coding • Run tests after each build • Build = compile + link + pass unit tests • A. k. a. “smoke tests” 10/3/2020 Object Oriented Design Course 8

Benefits of Unit Tests l Regression Testing l Part of the usual work •

Benefits of Unit Tests l Regression Testing l Part of the usual work • For your own code • Daily build: for others’ work • Replaces work done anyway • Causes tests to be written • Validates the design before impl 10/3/2020 Object Oriented Design Course 9

Top 12 Reasons to Write Unit Tests Part I l l Tests Reduce Bugs

Top 12 Reasons to Write Unit Tests Part I l l Tests Reduce Bugs in New Features Tests Reduce Bugs in Existing Features Tests Are Good Documentation Tests Reduce the Cost of Change 10/3/2020 Object Oriented Design Course 10

Top 12 Reasons to Write Unit Tests Part II l l Tests Improve Design

Top 12 Reasons to Write Unit Tests Part II l l Tests Improve Design Tests Allow Refactoring Tests Constrain Features Tests Defend Against Other Programmers 10/3/2020 Object Oriented Design Course 11

Top 12 Reasons to Write Unit Tests Part III l l Testing Is Fun

Top 12 Reasons to Write Unit Tests Part III l l Testing Is Fun Testing Forces You to Slow Down and Think Testing Makes Development Faster Tests Reduce Fear 10/3/2020 Object Oriented Design Course 12

JUnit l l l Unit testing framework for Java Open Source Integrated into Eclipse

JUnit l l l Unit testing framework for Java Open Source Integrated into Eclipse Test. Case • set. Up(), tear. Down() • Test<Test. Name>() Test. Suite Test. Runner 10/3/2020 Object Oriented Design Course 13

Resources l Eclipse l Top 12 Reasons to Write Unit Tests • http: //www.

Resources l Eclipse l Top 12 Reasons to Write Unit Tests • http: //www. eclipse. org/ • http: //www. eclipse-plugins. info/ • http: //www. onjava. com/pub/a/onjava/2003 /04/02/javaxpckbk. html l JUnit • http: //www. junit. org/ 03 October 2020 Object Oriented Design Course 14