Unit Testing.

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

Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Testing and Debugging CS221 – 2/13/09. Airline Program.
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.
TDD Test-Driven Development. JUnit 4.0 To use annotations need to import org.junit.Test To use assertion need to import org.junit.Assert.* No need to.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
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.
CODING Research Data Management. Research Data Management Coding When writing software or analytical code it is important that others and your future.
#RefreshCache CI - Daily Builds w/Jenkins – an Open Source Continuous Integration Server Nick Airdo Community Developer Advocate Central Christian Church.
By John Boal  Continuous Integration [CI] ◦ Automating the build process ◦ Build the entire system each time any new.
Lesson 4 Computer Software
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Testing. Definition From the dictionary- the means by which the presence, quality, or genuineness of anything is determined; a means of trial. For software.
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
© 2012 IBM Corporation Rational Insight | Back to Basis Series Chao Zhang Unit Testing.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com.
Program Development Life Cycle (PDLC)
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Testing in NetBeans. SWC Testing The ideal test: When the test is passed, the product is ready for delivery! Ideal – but (almost) impossible –Number of.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Software Construction Lecture 18 Software Testing.
TEST-1 6. Testing & Refactoring. TEST-2 How we create classes? We think about what a class must do We focus on its implementation We write fields We write.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
1 Legacy Code From Feathers, Ch 2 Steve Chenoweth, RHIT Right – Your basic Legacy, from Subaru, starting at $ 20,295, 24 city, 32 highway.
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
Chapter 1 Introducing Ant. What is ant? Ant is a build tool  Automate the tasks of compiling code, running test, and packaging the results for redistribution.
How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
SPI NIGHTLIES Alex Hodgkins. SPI nightlies  Build and test various software projects each night  Provide a nightlies summary page that displays all.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
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.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Northwest Arkansas.Net User Group Jay Smith Tyson Foods, Inc. Unit Testing nUnit, nUnitAsp, nUnitForms.
Software engineering - 2 Section 8. QUIZ Show how it is possible to determine the height of a tall building with the aid of a barometer.
Software Engineering Lecture 11 Software Testing Presenter: Josef Hallberg 1.
CSE 143 Lecture 14: testing.
Topic 2: Hardware and Software
Software Construction Lab 10 Unit Testing with JUnit
Test Granularities Unit Testing and Automation
Introduction to JUnit CS 4501 / 6501 Software Testing
Designing For Testability
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Testing and Debugging.
LCGAA nightlies infrastructure
Deploying and Configuring SSIS Packages
Software engineering – 1
BTEC NCF Dip in Comp - Unit 15 Website Development Lesson 12 – Publish and Test Mr C Johnston.
Unit testing C# classes
TRANSLATORS AND IDEs Key Revision Points.
How to Run a Java Program
Test-driven development (TDD)
CS240: Advanced Programming Concepts
Introduction to JUnit IT323 – Software Engineering II
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Testing Acknowledgement: Original slides by Jory Denny.
CS 240 – Advanced Programming Concepts
Section 4: Graphs and Testing
CSE 143 Lecture 5 More ArrayIntList:
Junit Tests.
Plug-In Architecture Pattern
Presentation transcript:

Unit Testing

F-22 Raptor Fighter

F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?

F-22 Raptor Fighter What would happen if Lockheed assembled an F-22 with "untested" parts (i.e., parts that were built but never verified)? It wouldn't work, and in all likelihood you would never be able to make it work Cheaper and easier to just start over

Managing implementation complexity Individual parts should be verified before being integrated with other parts Integrated subsystems should also be verified If adding a new part breaks the system, the problem must be related to the recently added part Track down the problem and fix it This ultimately leads to a complete system that works

2 approaches to programming "I wrote ALL of the code, but when I tried to compile and run it, nothing seemed to work!“ Approach #2 Write a little code (e.g., a method or small class) Test it Write a little more code Integrate the two verified pieces of code …

Unit testing Large programs consist of many smaller pieces Classes, methods, packages, etc. "Unit" is a generic term for these smaller pieces Three important types of software testing are: Unit Testing (test units in isolation) Integration Testing (test integrated units) System Testing (test entire system that is fully integrated) Unit Testing is done to test the smaller pieces in isolation before they are combined with other pieces Usually done by the developers who write the code

What unit tests do Unit tests create objects, call methods, and verify that the returned results are correct Actual results vs. Expected results Unit tests should be automated so that they can be run frequently (many times a day) to ensure that changes, additions, bug fixes, etc. have not broken the code Regression testing Notifies you when changes have introduced bugs, and helps to avoid destabilizing the system

Test driver program The tests are run by a "test driver", which is a program that just runs all of the unit test cases It must be easy to add new tests to the test driver After running the test cases, the test driver either tells you that everything worked, or gives you a list of tests that failed Little or no manual labor required to run tests and check the results

Android testing framework Android provides a framework for writing automated unit tests Based on the popular JUnit unit testing framework There are two types of Android unit tests Local Unit Tests These tests depend only on standard Java classes, and so can be run on the development computer instead of on an Android device You will create local unit tests for the Family Map Server project Instrumented Unit Tests These tests depend on Android-specific classes, and so must be run on an Android device You will create instrumented unit tests for the Family Map Client project

Android local unit tests Official Documentation Can run on the development computer without a device or emulator Module’s primary source code is located in the folder <module>/src/main/java/<package> Local unit test code is located in the folder <module>/src/test/java/<package>

Android local unit tests Example: junit-example (on web site) “spellcheck” module contains code for web-based spelling checker “Real” classes are in: src/main/java/spellcheck/*.java src/main/java/dataaccess/*.java “Test” classes are in: src/test/java/spellcheck/*.java src/test/java/dataaccess/*.java

Android local unit tests Local test classes are written using the JUnit 4 unit test framework Include the following in app/build.gradle dependencies { … testCompile 'junit:junit:4.12' } Import JUnit 4 classes import org.junit.*; import static org.junit.Assert.*;

Android local unit tests Test classes are just regular classes (no special superclass) Test methods may have any name (need not be test*), but must have the @Test annotation on them Common initialization code can be placed in a method (any name) with the @Before annotation Common cleanup code can be placed in a method (any name) with the @After annotation Use JUnit assert* methods to implement test cases JUnit 4 Assert Method Documentation

Running local unit tests (from Android Studio) No device or emulator is needed To run a single test class, in the “Project” tool window right-click on a test class name, and select “Run Tests” or “Debug Tests” To run all of your local unit tests, right-click on the “test/java” folder, and select “Run All Tests” or “Debug All Tests”

Running local unit tests (from command-line) Write a test driver class whose “main” method invokes the org.junit.runner.JUnitCore class to run your unit tests Run your test driver program from the command-line: java –cp build\classes\main;build\classes\test;libs\junit-4.12.jar;libs\hamcrest-all-1.3.jar;libs\sqlite-jdbc-3.16.1.jar TestDriver For the Family Map Server project, you will create a bash shell script that will compile and run your unit tests from the command-line

JUnit 4 unit testing framework JUnit 4 Documentation Use JUnit 4 annotations to mark test methods Annotation Description @Test public void method() The annotation @Test identifies that a method is a test method. @Before public void method() Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class). @After public void method() Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults).

JUnit 4 unit testing framework Use JUnit 4 annotations to mark test methods Annotation Description @BeforeClass public void method() Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database. @AfterClass public void method() Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database. @Test (expected = Exception.class) Fails, if the method does not throw the named exception. @Test(timeout=100) Fails, if the method takes longer than 100 milliseconds.

Database Unit Tests When writing unit tests for your database code, there are additional things to think about Put database driver JAR file on the class path Each unit test should start with a pristine database so prior tests have no effect Can re-create tables before each test Or, you can “rollback” the effects of each test so they are undone and don’t affect later tests