Automated Unit Testing. Test Automation Manual testing is laborious and time consuming. Computer automation has transformed many sectors of our economy.

Slides:



Advertisements
Similar presentations
Acceptance Testing vs. Unit Testing: A Developers Perspective Owen Rogers Rob Styles
Advertisements

Designing Reusable Frameworks for Test Automation
Test Automation Framework Ashesh Jain 2007EE50403 Manager Amit Maheshwari.
1 Software Engineering Lecture 11 Software Testing.
JUnit Syed Nabeel. Motivation Unit Testing Responsibility of  developer Rarely done properly Developers Excuse: “I am too much in a hurry”
Pragmatic Application Building: Step by Step Jay Sissom Principal Systems Analyst Indiana University
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.
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
An Overview of Computers and Programming
Matthew J Mattia CSC  Cumbersome Code  Consistent/Predictable design (GUEPs #5, CD’s #10)  Display “proper” amount of information  Including.
Test-Driven Development “Test first, develop later!” –OCUnit.
Io package as Java’s basic I/O system continue’d.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
TDD,BDD and Unit Testing in Ruby
SENG 403, Winter 2012 SENG 403 – Winter  Exploring unit-testing frameworks in.NET  Writing our first test with NUnit  Unit Testing in Visual.
LAYING OUT THE FOUNDATIONS. OUTLINE Analyze the project from a technical point of view Analyze and choose the architecture for your application Decide.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
CSC444F'06Lecture 81 Testing. CSC444F'06Lecture 82 Quality Assurance for Software Humans are fallible. –Infeasible to completely fix the humans –Need.
© 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,
Testing with Android Part I of II. Android Testing Framework Based on JUnit The Android JUnit extensions provide component-specific test case classes.
CS 360 Lecture 3.  The software process is a structured set of activities required to develop a software system.  Fundamental Assumption:  Good software.
Chapter 8 – Software Testing Lecture 1 1Chapter 8 Software testing The bearing of a child takes nine months, no matter how many women are assigned. Many.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Automated GUI testing How to test an interactive application automatically?
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Test Driven Development Arrange, Act, Assert… Awesome Jason Offutt Software Engineer Central Christian Church
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
CSE 219 Computer Science III Program Design Principles.
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
Implement Unit Test Framework for Application running on a Pocket PC 2003 device Durga Kulkarni Cyberonics Inc August 28, 2009.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
Automated GUI testing How to test an interactive application automatically?
LCG-SPI: SW-Testing LCG AppArea internal review (20/10/03)
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.
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.
1 CEN 4072 Software Testing PPT3: Making the program fail.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Testing in Android. Methods Unit Testing Integration Testing System Testing Regression Testing Compatibility Testing Black Box (Functional) White Box.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Unit Testing by Jon Edgar. Structure of Presentation Structure What is Unit Testing? Worked Example Extreme Programming (XP) Implementation Limitation.
Software Development Process CS 360 Lecture 3. Software Process The software process is a structured set of activities required to develop a software.
Testing JUnit Testing. Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes.
Northwest Arkansas.Net User Group Jay Smith Tyson Foods, Inc. Unit Testing nUnit, nUnitAsp, nUnitForms.
Tools for Automated Testing Presented by: Žygimantas Mockus.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
Unit testing with NUnit Anne Lam & Chris James CMPS 4113 – Software Engineering April 15, 2015.
Automated Testing with PHPUnit. How do you know your code works?
Exception testing Aistis Karkauskas, IFM-2/2. Introduction  Exceptions – anomalous or exceptional events requiring special processing – often changing.
Software Engineering Lecture 11 Software Testing Presenter: Josef Hallberg 1.
CPSC 372 John D. McGregor Module 8 Session 1 Testing.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Unit Testing - solid fundamentals
Testing Tutorial 7.
Introduction to JUnit CS 4501 / 6501 Software Testing
Chapter 8 – Software Testing
Testing and Test-Driven Development CSC 4700 Software Engineering
Introduction to JUnit CS 4501 / 6501 Software Testing
CS 240 – Advanced Programming Concepts
Presentation transcript:

Automated Unit Testing

Test Automation Manual testing is laborious and time consuming. Computer automation has transformed many sectors of our economy. Why not direct the same technology at the laborious aspects of software testing? Test automation is the process of using software to automate the manual aspects of software testing. Automated testing reduces labor required to run tests and speeds up the testing process. Perhaps the best opportunity for automated testing is during regression testing. Developers generally feel more confident making a change when there is a large comprehensive suite of regression tests that can be ran against the system under development.

Pitfalls of Test Automation As the number of automated test cases grows, so does the cost of maintaining them. A failed test doesn’t always mean a problem with the software. It can mean an assumption made when the test was written is no longer valid. A large comprehensive suite of automated tests acts as a safety net when making changes but it also increases the amount of software that has to be maintained as the system evolves.

Two General Approaches to Test Automation Automated GUI testing – using specialized (and expen$ive) software tools that generates keyboard and mouse input and validates the observable behavior of the program is correct. Mercury Interactive (now part of HP) offers automated UI testing software. Automated unit testing – writing routines to test procedures and the public interfaces of classes.

Testing Metrics Total number of automated tests. Number of automated test cases that fail during each build. % of failures caused by product errors vs. test case errors (or invalid assumptions). Of the test cases that failed because of an error or now invalid assumption, how many were rewritten vs. thrown away? Time spent creating new automated test cases. Time spent maintaining existing tests.

Unit Testing Frameworks Automated unit testing is often done using a framework such as NUnit (.net) or JUnit (Java). To use one of these xUnit frameworks, you write classes that contain methods which exercise the objects and routines in the code you want to test. By following certain conventions as you write these automated unit tests, you can easily manage these unit tests using the NUnit/JUnit/XUnit framework.

Example Unit Test Frameworks NUnit for.NET programs JUnit for Java Test::Unit for Ruby PyUnit for Python CppUnit for C++ OCUnit for Objective-C

Example Code to test: namespace MyCode { class Arithmetic { public int Add(int i, int j) { return i + j; }

NUnit Tests [TestFixture] public class ArithmeticTests { [Test] public void Add_SimpleTest(){ MyCode.Arithmetic a = new MyCode.Arithmetic(); int answer = a.Add(3,7); Assert.AreEqual(10, answer); } [Test] public void Add_BigNumbers() { MyCode.Arithmetic a = new MyCode.Arithmetic(); int answer = a.Add(int.MaxValue, 0); Assert.AreEqual(int.MaxValue, answer); }

Output From NUnit

Dependency Injection Objects are more flexible and easier to test when they allow their dependencies to be set by clients. The following class is not easy to test. It has a dependency on the file “AboutData.txt”. Testing the class involves making sure a file with the proper format exists on the file system. It can be made easier to test by redesigning it to use Dependency Injection.

public class AppDescription { private String name; private String version; private String author; public AppDescription() throws IOException { BufferedReader br = new BufferedReader( new FileReader("AboutData.txt")); name = br.readLine(); version = br.readLine(); author = br.readLine(); br.close(); } public String getAppDescription() { return name + " " + version + " was written by " + author; }

public class AppDescription { private String name; private String version; private String author; public AppDescription() throws IOException { this(new FileReader("AboutData.txt")); } public AppDescription(Reader r) throws IOException { BufferedReader br = new BufferedReader(r); name = br.readLine(); version = br.readLine(); author = br.readLine(); br.close(); } public String getAppDescription() { return name + " " + version + " was written by " + author; }

Dependency Injection via setter methods The previous example showed DI via a class’s constructor. You can also inject dependencies via setter methods. public class Dependent { private AService s1; private AnotherService s2; public Dependent() { } public setAService(AService s) { s1 = s; } public setAnotherService(AnotherService s) { s2 = s; }

Dependency Injection Example See class CourseGateway in SQLLiteExample. There is also a JUnit test for CourseGateway showing how dependency injection can make unit testing easier/possible. URL: de/tree/master/SQLLiteExample de/tree/master/SQLLiteExample

References

Creating and running unit tests with Android Studio There are JUnit extensions for Android to test Android components. Unit tests can be hard to write because application code is often tied to the application’s runtime environment. For example, to create a SQLite DB in android you need the application’s context. This makes it more difficult to write unit tests independent of the application. JUnit extensions for Android solve the problem. – Class android.test.AndroidTestCase provides access to an application’s context. – Class ActivityInstrumentationTestCase2 gives access to the application’s Activity (testing through the GUI)

Examples LuckyCount – demonstrates basic unit testing LuckyCount SQLLiteExample - demonstrates dependency injection with unit tests SQLLiteExample TestingThroughTheGUI – demonstrates automated tests that send input through the GUI. TestingThroughTheGUI For more detailed instruction on how to create and run automated unit tests in Android Studio, see: roidStudioUnitTesting.docx roidStudioUnitTesting.docx

Creating and running tests of Android applications in Eclipse with ADT There are JUnit extensions for Android to test Android components. Unit tests can be hard to write because application code is often tied to the application’s runtime environment. For example, to create a SQLite DB in android you need the application’s context. This makes it more difficult to write unit tests independent of the application. JUnit extensions for Android solve the problem. The JUnit extension class android.test.AndroidTestCase provides access to an application’s context.

Automated Android Activity Testing A well-designed program will separate program logic from the user interface. Among other benefits, it makes testing easier. Most program logic can be tested without going through the UI (something that requires physical input), but it does mean the portion code in the view and event handling routines for the view aren’t covered by automated unit tests. Until now! The Android instrumentation framework provides support for writing automated unit tests that send events to the user interface. It’s called Activity testing.