Code Contracts Parameterized Unit Tests Tao Xie. Example Unit Test Case = ? Outputs Expected Outputs Program + Test inputs Test Oracles 2 void addTest()

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Automated Testing and Response Analysis of Web Services
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Automated Software Verification with a Permission-Based Logic 20 th June 2014, Zürich Malte Schwerhoff, ETH Zürich.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
1 of 24 Automatic Extraction of Object-Oriented Observer Abstractions from Unit-Test Executions Dept. of Computer Science & Engineering University of Washington,
Dept. of Computer Science A Runtime Assertion Checker for the Java Modeling Language (JML) Yoonsik Cheon and Gary T. Leavens SERP 2002, June 24-27, 2002.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
Software Testing and Quality Assurance
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Program Exploration with Pex Nikolai Tillmann, Peli de Halleux Pex
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering Automatic Verification of Computer Programs.
K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond Caltech Pasadena, CA 12 November 2009.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
CS527: (Advanced) Topics in Software Engineering Overview of Software Quality Assurance Tao Xie ©D. Marinov, T. Xie.
Ranga Rodrigo. Class is central to object oriented programming.
Separation of Concerns Tao Xie Peking University, China North Carolina State University, USA In collaboration with Nikolai Tillmann, Peli de Halleux, Wolfram.
Testing. Definition From the dictionary- the means by which the presence, quality, or genuineness of anything is determined; a means of trial. For software.
Tao Xie North Carolina State University Supported by CACC/NSA Related projects supported in part by ARO, NSF, SOSI.
Tao Xie (North Carolina State University) Nikolai Tillmann, Jonathan de Halleux, Wolfram Schulte (Microsoft Research, Redmond WA, USA)
Automated Developer Testing: Achievements and Challenges Tao Xie North Carolina State University contact:
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
Tao Xie Automated Software Engineering Group Department of Computer Science North Carolina State University
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
1 Automatic Identification of Common and Special Object-Oriented Unit Tests Dept. of Computer Science & Engineering University of Washington, Seattle Oct.
Design by Contract in Java Concept and Comparison.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
FEN UCN T&B - PBA/CodeContract- Intro 1 Code Contract Introduction Specification of a Person Class.
Tao Xie North Carolina State University Nikolai Tillmann, Peli de Halleux, Wolfram Schulte Microsoft Research.
Parameterized Unit Tests By Nikolai Tillmann and Wolfram Schulte Proc. of ESEC/FSE 2005 Presented by Yunho Kim Provable Software Lab, KAIST TexPoint fonts.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Tao Xie (North Carolina State University) Peli de Halleux, Nikolai Tillmann, Wolfram Schulte (Microsoft Research)
Day Class Definitions and Methods Local & global variables, parameters & arguments,
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
1 Test Selection for Result Inspection via Mining Predicate Rules Wujie Zheng
Cooperative Developer Testing: Tao Xie North Carolina State University In collaboration with Xusheng ASE and Nikolai Tillmann, Peli de
Spec# Andreas Vida. Motivation Correct and maintainable software Correct and maintainable software Cost effective software production Cost effective software.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
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.
Tao Xie (North Carolina State University) Nikolai Tillmann, Peli de Halleux, Wolfram Schulte (Microsoft Research)
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Parameterized Unit Testing in the Open Source Wild Wing Lam (U. Illinois) In collaboration with Siwakorn Srisakaokul, Blake Bassett, Peyman Mahdian and.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Random Test Generation of Unit Tests: Randoop Experience
Google C++ Testing Framework Part 2: Assertion. Concepts A test case contains one or many tests. ◦ You should group your tests into test cases that reflect.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
Study 1 Purpose of the tool. Test architecture.. Testing Target system Test system Testing results results affecting.
Cs498dm Software Testing Darko Marinov January 24, 2012.
1 © Agitar Software, 2007 Automated Unit Testing with AgitarOne Presented by Eamon McCormick Senior Solutions Consultant, Agitar Software Inc. Presented.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Unit Testing.
Input Space Partition Testing CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Code Contracts and Pex Peli de Halleux, Nikolai Tillmann
White-Box Testing Using Pex
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
CS 240 – Advanced Programming Concepts
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
CSE 1020:Software Development
Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”
Presentation transcript:

Code Contracts Parameterized Unit Tests Tao Xie

Example Unit Test Case = ? Outputs Expected Outputs Program + Test inputs Test Oracles 2 void addTest() { ArrayList a = new ArrayList(1); Object o = new Object(); a.add(o); AssertTrue(a.get(0) == o); } Appropriate method sequence Appropriate primitive argument values Appropriate assertions Test Case = Test Input + Test Oracle

Levels of Test Oracles Expected output for an individual test input –In the form of assertions in test code Properties applicable for multiple test inputs –Crash (uncaught exceptions) or not, related to robustness issues, supported by most tools –Properties in production code: Design by Contract (precondition, postcondition, class invariants) supported by Parasoft Jtest, Google CodePro AnalytiX –Properties in test code: Parameterized unit tests supported by MSR Pex, AgitarOne X. Xiao, S. Thummalapenta, and T. Xie. Advances on Improving Automation in Developer Testing. In Advances in Computers, devtesthttp://people.engr.ncsu.edu/txie/publications.htm#ac12- devtest

Economics of Test Oracles 4 Expected output for an individual test input –Easy to manually verify for one test input –Expensive/infeasible to verify for many test inputs –Limited benefits: only for one test input Properties applicable for multiple test inputs –Not easy to write (need abstraction skills) –But once written, broad benefits for multiple test inputs

Assert behavior of multiple test inputs Design by Contract Example tools: Parasoft Jtest, Google CodePro AnalytiX, MSR Code Contracts, MSR Pex Class invariant: properties being satisfied by an object (in a consistent state) [AgitarOne allows a class invariant helper method used as test oracles] Precondition: conditions to be satisfied (on receiver object and arguments) before a method can be invoked Postcondition: properties being satisfied (on receiver object and return) after the method has returned Other types of specs also exist

Microsoft Research Code Contracts [ContractInvariantMethod] void ObjectInvariant() { Contract.Invariant( items != null ); } Features  Language expression syntax  Type checking / IDE  Declarative  Special Encodings  Result and Old public virtual int Add(object value) { Contract.Requires( value != null ); Contract.Ensures( Count == Contract.OldValue(Count) + 1 ); Contract.Ensures( Contract.Result () == Contract.OldValue(Count) ); if (count == items.Length) EnsureCapacity(count + 1); items[count] = value; return count++; } - Slide adapted from MSR RiSE

Parameterized Unit Testing void TestAdd(List list, int item) { Assume.IsTrue(list != null); var count = list.Count; list.Add(item); Assert.AreEqual(count + 1, list.Count); } void TestAdd(List list, int item) { Assume.IsTrue(list != null); var count = list.Count; list.Add(item); Assert.AreEqual(count + 1, list.Count); } Parameterized Unit Test = Unit Test with Parameters Separation of concerns – Data is generated by a tool – Developer can focus on functional specification [Tillmann&Schulte ESEC/FSE 05]

Parameterized Unit Tests are Formal Specifications Algebraic Specifications A Parameterized Unit Test can be read as a universally quantified, conditional axiom. void TestReadWrite(Res r, string name, string data) { Assume.IsTrue(r!=null & name!=null && data!=null); r.WriteResource(name, data); Assert.AreEqual(r.ReadResource(name), data); } void TestReadWrite(Res r, string name, string data) { Assume.IsTrue(r!=null & name!=null && data!=null); r.WriteResource(name, data); Assert.AreEqual(r.ReadResource(name), data); }  string name, string data, Res r: r ≠ null ⋀ name ≠ null ⋀ data ≠ null ⇒ equals( ReadResource(WriteResource(r, name, data).state, name), data)  string name, string data, Res r: r ≠ null ⋀ name ≠ null ⋀ data ≠ null ⇒ equals( ReadResource(WriteResource(r, name, data).state, name), data)

Parameterized Unit Tests in Pex

Parameterized Unit Testing Getting Popular Parameterized Unit Tests (PUTs) commonly supported by various test frameworks.NET: Supported by.NET test frameworks – – – … Java: Supported by JUnit 4.X – Generating test inputs for PUTs supported by tools.NET: Supported by Microsoft Research Pex – Java: Supported by Agitar AgitarOne –

Parameterized Test-Driven Development Write/refine Contract as PUT Write/refine Code of Implementation Fix-it (with Pex ), Debug with generated tests Fix-it (with Pex ), Debug with generated tests Use Generated Tests for Regression Run Pex Bug in PUT Bug in Code failures no failures

Assert behavior of multiple test inputs Software Agitation in AgitarOne Code Software Agitation Observations on code behavior, plus Test Coverage data If an Observation reveals a bug, fix it If it describes desired behavior, click to create a Test Assertion Code Compile Review Agitate - Slide adapted from Agitar Software Inc.

Software Agitation in AgitarOne 13 Image from