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

Slides:



Advertisements
Similar presentations
Unit Testing Australian Development Centre Brisbane, Australia.
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.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Testing Object Oriented Programs CSE 111 4/28/20151.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
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.
Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1.
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.
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.
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.
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:
SENG 403, Winter 2012 SENG 403 – Winter  Exploring unit-testing frameworks in.NET  Writing our first test with NUnit  Unit Testing in Visual.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Principles of Object Oriented Programming Practical session 2 – part A.
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.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
Program Development Life Cycle (PDLC)
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
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.
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.
Making Good Code AKA: So, You Wrote Some Code. Now What? Ray Haggerty July 23, 2015.
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.
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.
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
Software Development COMP220/COMP285 Seb Coope Introducing Ant These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
PRESENTER PRIYANKA GUPTA.  Testing the complete system with respect to requirements.  In System testing, the functionalities of the system are tested.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
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.
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.
2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Unit Testing & Code Coverage Please download and install EclEmma (link in resources section…I recommend Option 2) Also snarf the junit code for today’s.
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.
Windows App Studio Windows App Studio is the tool that makes it fast and easy to build Windows 10 apps. It’s accessible from any device with a browser.
CSE 143 Lecture 14: testing.
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.
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
Test Granularities Unit Testing and Automation
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
LCGAA nightlies infrastructure
Software engineering – 1
Unit testing C# classes
TRANSLATORS AND IDEs Key Revision Points.
How to Run a Java Program
Introduction to JUnit IT323 – Software Engineering II
EEC-492/693/793 iPhone Application Development
CS 240 – Advanced Programming Concepts
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Refactoring.
Junit Tests.
Presentation transcript:

Unit Testing

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 Approach #1 – "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 – Test it – Integrate the two verified pieces of code – Test it – …

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 subsystems) – 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 Tools like Ant or Make are often used to automate the building and running of the test driver (e.g., $ ant test )

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 – Instrumented Unit Tests These tests depend on Android-specific classes, and so must be run on an Android device – 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

Android instrumented unit tests Require a device or emulator to run App’s primary source code is located in the folder – app/src/main/java/ Instrumented unit test code is located in the folder – app/src/androidTest/java/

Android instrumented unit tests For each primary class, you can create a corresponding test class Put the test class in the same package as the primary class – app/src/main/java/ /SomeClass.java – app/src/androidTest/java/ /SomeClassTest.java Putting both classes in the same package (although different folders) gives the test class greater access to the primary class’s members

Android instrumented unit tests Examples SuperAsteroids (basic test) – app/src/main/java/edu/byu/cs/superasteroids /core/GraphicsUtils.java – app/src/androidTest/java/edu/byu/cs/superas teroids/core/GraphicsUtilsTests.java BookClub (database test, code under database lecture) – app/src/main/java/edu/byu/cs240/bookclub /database/BookDAO.java – app/src/androidTest/java/edu/byu/cs240/b ookclub/database/BookDAOTest.java

Android instrumented unit tests Test class is subclass of – android.test.AndroidTestCase android.test.AndroidTestCase Put test cases in test* methods Test methods follow this outline: – Initialize test objects/data – Invoke methods on test objects – Use assert* methods to compare expected and actual results – Cleanup test objects/data

Android instrumented unit tests Test methods often have redundant (i.e., duplicated) initialization and cleanup code You can override AndroidTestCase’s setup() and tearDown() methods to centralize redundant initialization/cleanup code – setUp() is run before each test* method – tearDown() is run after each test* method Test methods should not influence each other (i.e., they should not depend on each other or have “cross talk”) It should be possible to run the test methods in a random order without affecting the results

Running instrumented unit tests Make sure a device or emulator is available In Android Studio, open the “Build Variants” tool window in the bottom-left corner, and set the “Test Artifact” setting to “Android Instrumentation Tests” To run a single test class, in the “Project” tool window right-click on a test class name, and select “Run *Tests” To run all of your instrumented unit tests, right- click on the “androidTest/java” folder, and select “Run All Tests”

Android local unit tests Can run on the development computer without a device or emulator App’s primary source code is located in the folder – app/src/main/java/ Local unit test code is located in the folder – app/src/test/java/

Android local unit tests Example SuperAsteroids (local test) – app/src/test/java/edu/byu/cs/superas teroids/core/LocalTests.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 annotation on them Common initialization code can be placed in a method (any name) with annotation Common cleanup code can be placed in a method (any name) with annotation Use JUnit assert* methods to implement test cases JUnit 4 Assert Method Documentation

Running local unit tests No device or emulator is needed In Android Studio, open the “Build Variants” tool window in the bottom-left corner, and set the “Test Artifact” setting to “Unit Tests” To run a single test class, in the “Project” tool window right-click on a test class name, and select “Run *Tests” To run all of your local unit tests, right-click on the “test/java” folder, and select “Run All Tests”

JUnit 4 unit testing framework JUnit 4 Documentation Use JUnit 4 annotations to mark test methods public void method() The identifies that a method is a test public void method() Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the 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 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 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 (expected = Exception.class) Fails, if the method does not throw the named Fails, if the method takes longer than 100 milliseconds.