SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Next Generation Developer Testing:
Parameterized Testing
Tao Xie
Department of Computer Science
University of Illinois at Urbana-Champaign
Email: taoxie@illinois.edu
http://taoxie.cs.illinois.edu/
In Collaboration with Microsoft Research
2
Introduction
 Developer (aka unit) testing – a widely adopted practice for
ensuring high quality software
 A conventional unit test (CUT): small program with test inputs
and test assertions
void AddTest() {
HashSet set = new HashSet();
set.Add(7);
set.Add(3);
Assert.IsTrue(set.Count == 2);
}
Test Scenario
Test Assertions
Test Data
3
Parameterized Unit Tests (PUTs)
 Recent advances in unit testing introduced PUTs
void AddSpec(int x, int y)
{
HashSet set = new HashSet();
set.Add(x);
set.Add(y);
Assert.AreEqual(x == y, set.Count == 1);
Assert.AreEqual(x != y, set.Count == 2);
}
 PUTs separate two concerns:
• Specification of externally visible behavior (assertions)
• Selection of internally relevant test inputs (coverage)
4
Parameterized Unit Tests (PUTs)
 More beneficial than CUTs
• Help describe behaviors for all test arguments
 Address two main issues with CUTs
• Missing test data required for exercising important
behaviors
 Low fault-detection capability
• Including test data that exercises the same behaviour
 Redundant unit tests
5
An Example using IntStack
public void CUT1() {
int elem = 1;
IntStack stk = new IntStack();
stk.Push(elem);
Assert.AreEqual(1, stk.Count());
}
Three CUTs
public void CUT2() {
int elem = 30;
IntStack stk = new IntStack();
stk.Push(elem);
Assert.AreEqual(1, stk.Count());
}
public void CUT3() {
int elem1 = 1, elem2 = 30;
IntStack stk = new IntStack();
stk.Push(elem1);
stk.Push(elem2);
Assert.AreEqual(2, stk.Count());
}
 CUT1 and CUT2 exercise push with
different test data
 CUT3 exercises push when stack is not
empty
Two main issues with CUTs:
 Fault-detection capability issue:
undetected defect where things go wrong
when passing a negative value to push
 Redundant test issue: CUT2 is
redundant with respect to CUT1
6
An Example using IntStack
public void CUT1() {
int elem = 1;
IntStack stk = new IntStack();
stk.Push(elem);
Assert.AreEqual(1, stk.Count());
}
Three CUTs
public void CUT2() {
int elem = 30;
IntStack stk = new IntStack();
stk.Push(elem);
Assert.AreEqual(1, stk.Count());
}
public void CUT3() {
int elem1 = 1, elem2 = 30;
IntStack stk = new IntStack();
stk.Push(elem1);
stk.Push(elem2);
Assert.AreEqual(2, stk.Count());
}
 No need to describe test data
• Generated automatically
 Single PUT replaces multiple CUTs
• With reduced size of test code
public void PUT(int[] elem) {
Assume.IsTrue(elem != null);
IntStack stk = new IntStack();
for(int i in elem)
stk.push(elem);
Assert.AreEqual(elem.Length, stk.count());
}
An equivalent PUT
Parameterized Unit Tests are
Algebraic Specifications
• A Parameterized Unit Test can be read as a
universally quantified, conditional axiom.
void ReadWrite(Storage s, string name, string data) {
Assume.IsTrue(s!=null && name!=null && data!=null);
s.WriteResource(name, data);
var readData = s.ReadResource(name);
Assert.AreEqual(data, readData);
}
 Storage s, string name, string data:
s ≠ null ⋀ name ≠ null ⋀ data ≠ null ⇒
equals(
ReadResource(WriteResource(s,name,data).state, name).retval,
data)
Parameterized Unit Testing
is going mainstream
Parameterized Unit Tests (PUTs) commonly supported by various
test frameworks
• .NET: Supported by .NET test frameworks
– http://www.mbunit.com/
– http://www.nunit.org/ …
• Java: Supported by JUnit 4.X
– http://www.junit.org/
Generating test inputs for PUTs supported by tools
• .NET: Supported by Microsoft Visual Studio 2015 IntelliTest
– Formerly Microsoft Research Pex: http://research.microsoft.com/pex/
• Java: Supported by Agitar AgitarOne
– http://www.agitar.com/
Parameterized Tests in JUnit
9
https://github.com/junit-team/junit/wiki/Parameterized-tests
JUnit Theories
10
https://github.com/junit-team/junit/wiki/Theories
Assumptions and Assertions
void PexAssume.IsTrue(bool c) {
if (!c)
throw new AssumptionViolationException();
}
void PexAssert.IsTrue(bool c) {
if (!c)
throw new AssertionViolationException();
}
• Assumptions and assertions induce branches
• Executions which cause assumption violations are
ignored, not reported as errors or test cases
Test Data Generation
• Human
– Expensive, incomplete, …
• Brute Force
– Pairwise, predefined data, etc…
• Semi - Random
– Cheap, Fast
– “It passed a thousand tests” feeling
• Dynamic Symbolic Execution:
IntelliTest/Pex, SAGE, CUTE, …
– Automated white-box
– Not random – Constraint Solving
13
void CoverMe(int[] a)
{
if (a == null) return;
if (a.Length > 0)
if (a[0] == 1234567890)
throw new Exception("bug");
}
a.Length>0
a[0]==123…
TF
T
F
F
a==null
T
Constraints to solve
a!=null
a!=null &&
a.Length>0
a!=null &&
a.Length>0 &&
a[0]==123456890
Input
null
{}
{0}
{123…}
Execute&MonitorSolve
Choose next path
Observed constraints
a==null
a!=null &&
!(a.Length>0)
a==null &&
a.Length>0 &&
a[0]!=1234567890
a==null &&
a.Length>0 &&
a[0]==1234567890
 Generates test data systematically
Done: There is no path left.
Background: DSE
Pex4Fun
http://pex4fun.com/
Nikolai Tillmann, Jonathan De Halleux, Tao Xie, Sumit Gulwani and Judith Bishop. Teaching and Learning
Programming and Software Engineering via Interactive Gaming. In ICSE 2013, SEE.
http://taoxie.cs.illinois.edu/publications/icse13see-pex4fun.pdf
1,703,247 clicked 'Ask Pex!'
Pex4Fun
• Click http://pex4fun.com/default.aspx?language=CSharp&sa
mple=_Template
• Copy and modify the following code snippet in the code
editing box (or simply click here)
using System;
using Microsoft.Pex.Framework;
using Microsoft.Pex.Framework.Settings;
[PexClass]
public class Program
{
[PexMethod]//[PexMethod(TestEmissionFilter=PexTestEmissionFilter.All)]
public static string testMethod(int x, int y)
{
PexAssume.IsTrue(y >= 0);//replace here with your assumption
//... enter your code under test here
//if (x == 10000) throw new Exception();
PexAssert.IsTrue(y >= 0);//replace here with your assertion
return PexSymbolicValue.GetPathConditionString();
}
}
https://sites.google.com/site/teachpex/Home/pex-usage-tips
Microsoft Visual Studio 2015
IntelliTest
16
https://msdn.microsoft.com/en-us/library/dn823749.aspx
Microsoft Visual Studio 2015
IntelliTest
17https://msdn.microsoft.com/en-us/library/dn823749.aspx
18
Recall: An Example using IntStack
public void CUT1() {
int elem = 1;
IntStack stk = new IntStack();
stk.Push(elem);
Assert.AreEqual(1, stk.Count());
}
Three CUTs
public void CUT2() {
int elem = 30;
IntStack stk = new IntStack();
stk.Push(elem);
Assert.AreEqual(1, stk.Count());
}
public void CUT3() {
int elem1 = 1, elem2 = 30;
IntStack stk = new IntStack();
stk.Push(elem1);
stk.Push(elem2);
Assert.AreEqual(2, stk.Count());
}
 No need to describe test data
• Generated automatically
 Single PUT replaces multiple CUTs
• With reduced size of test code
public void PUT(int[] elem) {
Assume.IsTrue(elem != null);
IntStack stk = new IntStack();
for(int i in elem)
stk.push(elem);
Assert.AreEqual(elem.Length, stk.count());
}
An equivalent PUT
19
Test Generalization: CUTs  PUT
Major Steps
• S1: Parameterize
• S2: Generalize Test Oracle
• S3: Add Assumptions
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
Example
00: public class SettingsGroup{
01: MSS storage; ...
02: public SettingsGroup(MSS storage) {
03: this.storage = storage;
04: }
05: public void SaveSetting(string sn, object sv) {
06: object ov = storage.GetSetting( sn );
07: //Avoid change if there is no real change
08: if (ov != null ) {
09: if(ov is string && sv is string && (string)ov==(string)sv ||
10: ov is int && sv is int && (int)ov==(int)sv ||
11: ov is bool&& sv is bool&& (bool)ov==(bool)sv ||
12: ov is Enum&& sv is Enum&& ov.Equals(sv))
13: return;
14: }
15: storage.SaveSetting(sn, sv);
16: if (Changed != null)
17: Changed(this, new SettingsEventArgs(sn));
18: }}
20
SettingsGroup class of NUnit with
the SaveSetting method under test
An Existing CUT
00: public class SettingsGroup{
01: MSS storage; ...
02: public SettingsGroup(MSS storage) {
03: this.storage = storage;
04: }
05: public void SaveSetting(string sn, object sv) {
06: object ov = storage.GetSetting( sn );
07: //Avoid change if there is no real change
08: if (ov != null ) {
09: if(ov is string && sv is string && (string)ov==(string)sv ||
10: ov is int && sv is int && (int)ov==(int)sv ||
11: ov is bool&& sv is bool&& (bool)ov==(bool)sv ||
12: ov is Enum&& sv is Enum&& ov.Equals(sv))
13: return;
14: }
15: storage.SaveSetting(sn, sv);
16: if (Changed != null)
17: Changed(this, new SettingsEventArgs(sn));
18: }}
21
00: //tg is of type SettingsGroup
01: [Test]
02: public void TestSettingsGroup() {
03: tg.SaveSetting("X",5);
04: tg.SaveSetting("NAME","Tom");
05: Assert.AreEqual(5,tg.GetSetting("X"));
06: Assert.AreEqual("Tom",tg.GetSetting("NAME"));
07: }
Existing CUT
SettingsGroup class of NUnit with
the SaveSetting method under test
?
Issues with Existing CUT
22
Only CUT for verifying SaveSetting method
• Does not verify the behavior for the types bool and enum
• Does not cover the true branch in Line 8
Does test generalization addresses these two
issues
…
06: object ov = storage.GetSetting( sn );
07: //Avoid change if there is no real change
08: if (ov != null ) { …
23
S1 - Parameterize
 Promote all primitive values as arguments
• name of setting as a parameter of type string
• string “TOM” and int 5 as a parameter of type object
helping IntelliTest/Pex generate concrete values based on the
constraints encountered in different paths
 Promote non-primitive objects such as receiver objects
as arguments
helping IntelliTest/Pex generate object states for the receiver
objects that can cover additional paths
//Original CUT
02: public void TestSettingsGroup() {
03: tg.SaveSetting("X",5);
04: tg.SaveSetting("NAME","Tom");….
//New PUT
02: public void TestSave(
SettingsGroup st,
string sn, object sv){
….
24
S2 – Generalize Test Oracle
 Replace the constant value, “TOM” and 5, with
the relevant parameter of the PUT
//Original CUT
02: public void TestSettingsGroup() {
03: tg.SaveSetting("X",5);
04: tg.SaveSetting("NAME","Tom");
05: Assert.AreEqual(5,tg.GetSetting("X"));
06: Assert.AreEqual("Tom",tg.GetSetting("NAME"));
….
//New PUT
02: public void TestSave(SettingsGroup st,
string sn, object sv){
03: st.SaveSetting(sn, sv);
04: PexAssert.AreEqual(sv,st.GetSetting(sn));
//New PUT
02: public void TestSave(SettingsGroup st,
string sn, object sv){
03: st.SaveSetting(sn, sv);
04: PexAssert.AreEqual(sv,st.GetSetting(sn));
25
S3 – Add Assumptions
IntelliTest/Pex requires guidance in generating
legal values for the parameters
E.g., Add the tag PexAssumeUnderTest (PAUT) with the parameter, i.e.,
generated value should not be null
//New PUT
02: public void TestSave([PAUT]SettingsGroup
st, [PAUT]string sn, [PAUT] object sv){
03: st.SaveSetting(sn, sv);
04: PexAssert.AreEqual(sv,st.GetSetting(sn));
26
Example Summary
 Resulting PUT
Result of S1  Parameters “sn” and “sv”
 Result of S2  Line 4: “st.GetSetting(sn)”
 Result of S3  Parameter “st”
00: //PAUT: PexAssumeUnderTest
01: [PexMethod]
02: public void TestSave([PAUT]SettingsGroup st,
[PAUT] string sn, [PAUT] object sv) {
03: st.SaveSetting(sn, sv);
04: PexAssert.AreEqual(sv,st.GetSetting(sn));
05: }
27
Example - Test Generalization Results
 Achieved 10% branch cov
 Required 2 method calls
with values of type string and
int
2: public void
TestSave([PAUT]SettingsGroup st,
[PAUT]string sn, [PAUT] object sv){
3: st.SaveSetting(sn, sv);
4: PexAssert.AreEqual
(sv,st.GetSetting(sn));
2: public void TestSettingsGroup() {
3: tg.SaveSetting("X",5);
4: tg.SaveSetting("NAME","Tom");….
 Achieved 90% branch cov
 Required only 1 method call,
sufficient to test for all types
(parameter of type object)
Original CUT New PUT
EXAMPLE PATTERNS
FOR PARAMETERIZED UNIT TESTS
http://research.microsoft.com/pex/patterns.pdf
Pattern
4A
• Assume, Arrange, Act, Assert
[PexMethod]
void Add(List target, T value) {
PexAssume.IsNotNull(target); // assume
var count = target.Count; // arrange
target.Add(value); // act
Assert(target.Count == count + 1)//assert
}
Pattern
Roundtrip
• For an API f(x), f-1(f(x)) = x for all x
void ToStringParseRoundtrip(int value) {
string s = value.ToString();
int parsed = int.Parse(s);
Assert.AreEqual(value, parsed);
}
Pattern
State Relation
• Observe a state change
void ContainedAfterAdd(string value) {
var list = new List<string>();
list.Add(value);
Assert.IsTrue(list.Contains(value));
}
Pattern
Commutative Diagram
• Given two implementations f and g of the same
function, each possible requiring a different number
of steps, i.e. f(x)=f1(f2(…(fn(x)…)), and g(x)=g1(g2(…
(gm(x)…)), then it should hold that
f1(f2(…(fn(x))…) = g1(g2(…(gm(x)…)) for all x.
string Multiply(string x, string y);
int Multiply(int x, int y);
void CommutativeDiagram1(int x, int y) {
string z1 = Multiply(x, y).ToString();
string z2 = Multiply(x.ToString(), y.ToString());
PexAssert.AreEqual(z1, z2);
}
Code Hunt: Turning Pex/PUT into
a Coding Game
https://www.codehunt.com/
Behind the Scene of Code Hunt
Secret Implementation
class Secret {
public static int Puzzle(int x) {
if (x <= 0) return 1;
return x * Puzzle(x-1);
}
}
Player Implementation
class Player {
public static int Puzzle(int x) {
return x;
}
}
class Test {
public static void Driver(int x) {
if (Secret.Puzzle(x) != Player.Puzzle(x))
throw new Exception(“Mismatch”);
}
}
behavior
Secret Impl == Player Impl
34
It’s a game!
• iterative gameplay
• adaptive
• personalized
• no cheating
• clear winning criterion
code
test cases
Bishop et al. Code Hunt: Experience with Coding Contests at Scale. ICSE 2015, JSEET
http://taoxie.cs.illinois.edu/publications/icse15jseet-codehunt.pdf
Next Generation Developer
Testing: Parameterized Testing
Microsoft
Visual Studio 2015
- IntelliTest
JUnit TheoriesPexMethod
Test Generalization
CUTs  PUTs
PUT Patterns
 PUTs
…
Thank You
37
https://sites.google.com/site/teachpex/
http://research.microsoft.com/pex
http://taoxie.cs.illinois.edu/
Collaboration is Welcome!
Email: taoxie@illinois.edu
Next Generation Developer
Testing: Parameterized Testing
Microsoft
Visual Studio 2015
- IntelliTest
JUnit TheoriesPexMethod
Test Generalization
CUTs  PUTs
PUT Patterns
 PUTs
…
http://taoxie.cs.illinois.edu/taoxie@illinois.edu
https://sites.google.com/site/teachpex/
Pattern
Roundtrip
• For an API f(x), f-1(f(x)) = x for all x
void PropertyRoundtrip(string value) {
var target = new Foo();
target.Name = value;
var roundtripped = target.Name;
Assert.AreEqual(value, roundtripped);
}
http://research.microsoft.com/pex/patterns.pdf
Pattern
Sanitized Roundtrip
• For an API f(x), f-1(f(f-1(x)) = f-1(x) for all x
void ParseToString(string x) {
var normalized = int.Parse(x);
var intermediate = normalized.ToString();
var roundtripped = int.Parse(intermediate);
Assert(normalized == roundtripped);
}
http://research.microsoft.com/pex/patterns.pdf
Pattern
Reachability
• Indicate which portions of a PUT should be
reachable.
[PexExpectedGoals]
public void InvariantAfterParsing(string input)
{
ComplexDataStructure x;
bool success = ComplexDataStructure.TryParse(
input, out x);
PexAssume.IsTrue(success);
PexGoal.Reached();
x.AssertInvariant();
}
http://research.microsoft.com/pex/patterns.pdf
Pattern
Regression Tests
• Generated test asserts any observed value
– Return value, out parameters, PexGoal
• When code evolves, breaking changes in
observable will be discovered
int AddTest(int a, int b) {
return a + b; }
void AddTest01() {
var result = AddTest(0, 0);
Assert.AreEqual(0, result);
} http://research.microsoft.com/pex/patterns.pdf
Pattern
Same Observable Behavior
• Given two methods f(x) and g(x), and a method b(y)
that observes the result or the exception behavior of
a method, assert that f(x) and g(x) have same
observable behavior under b, i.e. b(f(x)) = b(g(x))
for all x.
public void ConcatsBehaveTheSame(
string left, string right)
{
PexAssert.AreBehaviorsEqual(
() => StringFormatter.ConcatV1(left, right),
() => StringFormatter.ConcatV2(left, right));
}
http://research.microsoft.com/pex/patterns.pdf
Pattern
Allowed Exception
• Allowed exception -> negative test case
[PexAllowedException(typeof(ArgumentException))]
void Test(object item) {
var foo = new Foo(item) // validates item
// generated test (C#)
[ExpectedException(typeof(ArgumentException))]
void Test01() {
Test(null); // argument check
}
http://research.microsoft.com/pex/patterns.pdf
45
S4 – Add Factory Method
 IntelliTest/Pex requires guidance handling non-
primitive objects
e.g., Add factory methods that generate instances of non-primitive objects
00: //PAUT: PexAssumeUnderTest
01: //MSS: MemorySettingsStorage (class)
01: [PexFactoryMethod(typeof(MSS))]
02: public static MSS Create([PAUT] string[] sn, [PAUT]object[] sv) {
03: PexAssume.IsTrue(sn.Length == sv.Length);
04: PexAssume.IsTrue(sn.Length > 0);
05: MSS mss = new MSS();
06: for(int count = 0; count < sn.Length; count++) {
07: mss.SaveSetting(sn[count], sv[count]);
08: }
09: return mss;
10: }
• Help create different object states for MSS
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
46
S5 – Add Mock Object
 Pex faces challenges in handling code that
interacts with external environment, e.g., file system
• Write mock objects to assist Pex and to test features in isolation
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
47
Empirical Study
 RQ1: Branch coverage
• How much higher percentage of branch coverage is achieved
by retrofitted PUTs compared to existing CUTs?
 RQ2: Defect detection
• How many new defects (that are not detected by existing
CUTs) are detected by PUTs and vice-versa?
 RQ3: Generalization effort
• How much effort is required for generalizing CUTs to PUTs?
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
Study Setup
 Three Subject Applications
48
Subjects Downloads Code Under Test
# Classes # Methods KLOC
Test Code
# Classes # CUTs KLOC
NUnit 193,563 9 87 1.4 9 49 0.9
DSA 3,239 27 259 2.4 20 337 2.5
QuickGraph 7,969 56 463 6.2 9 21 1.2
TOTAL 92 809 10 38 407 4.6
 High downloads count
 Lines of code under test: 10 KLOC
 Number of CUTs: 407
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
Study Setup – cont.
49
 Conducted by the first and second authors
• No prior knowledge of subjects
• Two years of experience with PUTs and Pex
• Retrofitted 407 CUTs (4.6 KLOC) as 224 PUTs (4.0 KLOC)
Generated three categories of CUTs
• C1: Existing CUTs
• C2: CUTs generated from PUTs
• C3: Existing CUTs + RTs (tests generated using Randoop)
 Help show that benefits of generalization cannot be achieved by
generating tests randomly
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
RQ1: Branch Coverage
50
Subjects # RTs Branch Coverage
CUTs (%) CUTs + RTs (%) PUTs (%)
Overall
Increase
(%)
Maximum
Increase
(%)
NUnit 144 78 78 88 10 52
DSA 615 91 91 92 1 1
QuickGrap
h
3628 87 88 89 2 11
CUTs + RTs:
 Added 144, 615, and 3628 tests using Randoop
 Branch coverage increased by 0%, 0%, and 1%
CUTs generated from PUTs:
 Branch coverage increased by 10%, 1%, and 2%
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
RQ2: Defect Detection
51
 RTs: CUTs generated using Randoop
 Pex without generalized PUTs: CUTs generated by applying Pex on public
methods
 CUTs generated from PUTs: CUTs generated by applying Pex on generalized PUTs
• Detected all defects detected by the first two categories
CUTs category #Failing Tests
DSA NUnit QuickGraph
# Real Defects
Basic CUTs 0 0 0 0
RTs 90 25 738 4
Pex without
generalized PUTs
23 170 17 2
CUTs generated
from PUTs
15 4 0 19
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
RQ3: Generalization Effort
52
 Both authors conducted comparable amount of
generalization
 Effort spent in hours
• NUnit: 2.8 hrs
• DSA: 13.8 hrs
• QuickGraph: 1.5 hrs
 Effort is worthwhile compared to benefits of test
generalization
Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011
http://taoxie.cs.illinois.edu/publications/fase11-put.pdf

Weitere ähnliche Inhalte

Was ist angesagt?

Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
Alok Kumar
 

Was ist angesagt? (20)

DCN Practical
DCN PracticalDCN Practical
DCN Practical
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
STAMP Descartes Presentation
STAMP Descartes PresentationSTAMP Descartes Presentation
STAMP Descartes Presentation
 
Java programs
Java programsJava programs
Java programs
 
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
[Quality Meetup] Piotr Wittchen - Fixing a billion dollar mistake
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
Java practical
Java practicalJava practical
Java practical
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in Java
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
 
Java Generics
Java GenericsJava Generics
Java Generics
 
JVM Mechanics
JVM MechanicsJVM Mechanics
JVM Mechanics
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbook
 
Java programs
Java programsJava programs
Java programs
 

Ähnlich wie Next Generation Developer Testing: Parameterized Testing

33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
fantasiatheoutofthef
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 

Ähnlich wie Next Generation Developer Testing: Parameterized Testing (20)

JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
Advances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and PracticeAdvances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and Practice
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
JUnit PowerUp
JUnit PowerUpJUnit PowerUp
JUnit PowerUp
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Unit testing [4] - Software Testing Techniques (CIS640)
Unit testing [4] - Software Testing Techniques (CIS640)Unit testing [4] - Software Testing Techniques (CIS640)
Unit testing [4] - Software Testing Techniques (CIS640)
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
 
Be smart when testing your Akka code
Be smart when testing your Akka codeBe smart when testing your Akka code
Be smart when testing your Akka code
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 

Mehr von Tao Xie

Mehr von Tao Xie (20)

MSR 2022 Foundational Contribution Award Talk: Software Analytics: Reflection...
MSR 2022 Foundational Contribution Award Talk: Software Analytics: Reflection...MSR 2022 Foundational Contribution Award Talk: Software Analytics: Reflection...
MSR 2022 Foundational Contribution Award Talk: Software Analytics: Reflection...
 
DSML 2021 Keynote: Intelligent Software Engineering: Working at the Intersect...
DSML 2021 Keynote: Intelligent Software Engineering: Working at the Intersect...DSML 2021 Keynote: Intelligent Software Engineering: Working at the Intersect...
DSML 2021 Keynote: Intelligent Software Engineering: Working at the Intersect...
 
Intelligent Software Engineering: Synergy between AI and Software Engineering
Intelligent Software Engineering: Synergy between AI and Software EngineeringIntelligent Software Engineering: Synergy between AI and Software Engineering
Intelligent Software Engineering: Synergy between AI and Software Engineering
 
Diversity and Computing/Engineering: Perspectives from Allies
Diversity and Computing/Engineering: Perspectives from AlliesDiversity and Computing/Engineering: Perspectives from Allies
Diversity and Computing/Engineering: Perspectives from Allies
 
Intelligent Software Engineering: Synergy between AI and Software Engineering...
Intelligent Software Engineering: Synergy between AI and Software Engineering...Intelligent Software Engineering: Synergy between AI and Software Engineering...
Intelligent Software Engineering: Synergy between AI and Software Engineering...
 
MSRA 2018: Intelligent Software Engineering: Synergy between AI and Software ...
MSRA 2018: Intelligent Software Engineering: Synergy between AI and Software ...MSRA 2018: Intelligent Software Engineering: Synergy between AI and Software ...
MSRA 2018: Intelligent Software Engineering: Synergy between AI and Software ...
 
SETTA'18 Keynote: Intelligent Software Engineering: Synergy between AI and So...
SETTA'18 Keynote: Intelligent Software Engineering: Synergy between AI and So...SETTA'18 Keynote: Intelligent Software Engineering: Synergy between AI and So...
SETTA'18 Keynote: Intelligent Software Engineering: Synergy between AI and So...
 
ISEC'18 Tutorial: Research Methodology on Pursuing Impact-Driven Research
ISEC'18 Tutorial: Research Methodology on Pursuing Impact-Driven ResearchISEC'18 Tutorial: Research Methodology on Pursuing Impact-Driven Research
ISEC'18 Tutorial: Research Methodology on Pursuing Impact-Driven Research
 
ISEC'18 Keynote: Intelligent Software Engineering: Synergy between AI and Sof...
ISEC'18 Keynote: Intelligent Software Engineering: Synergy between AI and Sof...ISEC'18 Keynote: Intelligent Software Engineering: Synergy between AI and Sof...
ISEC'18 Keynote: Intelligent Software Engineering: Synergy between AI and Sof...
 
Intelligent Software Engineering: Synergy between AI and Software Engineering
Intelligent Software Engineering: Synergy between AI and Software EngineeringIntelligent Software Engineering: Synergy between AI and Software Engineering
Intelligent Software Engineering: Synergy between AI and Software Engineering
 
Software Analytics: Data Analytics for Software Engineering and Security
Software Analytics: Data Analytics for Software Engineering and SecuritySoftware Analytics: Data Analytics for Software Engineering and Security
Software Analytics: Data Analytics for Software Engineering and Security
 
Planning and Executing Practice-Impactful Research
Planning and Executing Practice-Impactful ResearchPlanning and Executing Practice-Impactful Research
Planning and Executing Practice-Impactful Research
 
Software Analytics: Data Analytics for Software Engineering
Software Analytics: Data Analytics for Software EngineeringSoftware Analytics: Data Analytics for Software Engineering
Software Analytics: Data Analytics for Software Engineering
 
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
 
Transferring Software Testing Tools to Practice
Transferring Software Testing Tools to PracticeTransferring Software Testing Tools to Practice
Transferring Software Testing Tools to Practice
 
Common Technical Writing Issues
Common Technical Writing IssuesCommon Technical Writing Issues
Common Technical Writing Issues
 
HotSoS16 Tutorial "Text Analytics for Security" by Tao Xie and William Enck
HotSoS16 Tutorial "Text Analytics for Security" by Tao Xie and William EnckHotSoS16 Tutorial "Text Analytics for Security" by Tao Xie and William Enck
HotSoS16 Tutorial "Text Analytics for Security" by Tao Xie and William Enck
 
User Expectations in Mobile App Security
User Expectations in Mobile App SecurityUser Expectations in Mobile App Security
User Expectations in Mobile App Security
 
Impact-Driven Research on Software Engineering Tooling
Impact-Driven Research on Software Engineering ToolingImpact-Driven Research on Software Engineering Tooling
Impact-Driven Research on Software Engineering Tooling
 
Software Analytics - Achievements and Challenges
Software Analytics - Achievements and ChallengesSoftware Analytics - Achievements and Challenges
Software Analytics - Achievements and Challenges
 

Kürzlich hochgeladen

Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
makhmalhalaaay
 
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
baharayali
 
Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...
Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...
Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...
baharayali
 
Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...
baharayali
 
Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...
Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...
Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...
baharayali
 
Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...
Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...
Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...
Amil Baba Naveed Bangali
 

Kürzlich hochgeladen (20)

English - The Forgotten Books of Eden.pdf
English - The Forgotten Books of Eden.pdfEnglish - The Forgotten Books of Eden.pdf
English - The Forgotten Books of Eden.pdf
 
Legends of the Light v2.pdf xxxxxxxxxxxxx
Legends of the Light v2.pdf xxxxxxxxxxxxxLegends of the Light v2.pdf xxxxxxxxxxxxx
Legends of the Light v2.pdf xxxxxxxxxxxxx
 
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
Professional Amil baba, Kala jadu specialist in Multan and Kala ilam speciali...
 
Genesis 1:8 || Meditate the Scripture daily verse by verse
Genesis 1:8  ||  Meditate the Scripture daily verse by verseGenesis 1:8  ||  Meditate the Scripture daily verse by verse
Genesis 1:8 || Meditate the Scripture daily verse by verse
 
Jude: The Acts of the Apostates (Jude vv.1-4).pptx
Jude: The Acts of the Apostates (Jude vv.1-4).pptxJude: The Acts of the Apostates (Jude vv.1-4).pptx
Jude: The Acts of the Apostates (Jude vv.1-4).pptx
 
St. Louise de Marillac and Poor Children
St. Louise de Marillac and Poor ChildrenSt. Louise de Marillac and Poor Children
St. Louise de Marillac and Poor Children
 
St John's Church Parish Diary for May 2024
St John's Church Parish Diary for May 2024St John's Church Parish Diary for May 2024
St John's Church Parish Diary for May 2024
 
MEIDUNIDADE COM JESUS PALESTRA ESPIRITA1.pptx
MEIDUNIDADE COM JESUS  PALESTRA ESPIRITA1.pptxMEIDUNIDADE COM JESUS  PALESTRA ESPIRITA1.pptx
MEIDUNIDADE COM JESUS PALESTRA ESPIRITA1.pptx
 
"The Magnificent Surah Rahman: PDF Version"
"The Magnificent Surah Rahman: PDF Version""The Magnificent Surah Rahman: PDF Version"
"The Magnificent Surah Rahman: PDF Version"
 
Study of the Psalms Chapter 1 verse 3 - wanderean
Study of the Psalms Chapter 1 verse 3 - wandereanStudy of the Psalms Chapter 1 verse 3 - wanderean
Study of the Psalms Chapter 1 verse 3 - wanderean
 
Genesis 1:5 - Meditate the Scripture Daily bit by bit
Genesis 1:5 - Meditate the Scripture Daily bit by bitGenesis 1:5 - Meditate the Scripture Daily bit by bit
Genesis 1:5 - Meditate the Scripture Daily bit by bit
 
Genesis 1:2 - Meditate the Scripture Daily bit by bit
Genesis 1:2 - Meditate the Scripture Daily bit by bitGenesis 1:2 - Meditate the Scripture Daily bit by bit
Genesis 1:2 - Meditate the Scripture Daily bit by bit
 
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
Real Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in kara...
 
Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...
Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...
Popular Kala Jadu, Black magic expert in Karachi and Kala jadu expert in Laho...
 
A Spiritual Guide To Truth v10.pdf xxxxxxx
A Spiritual Guide To Truth v10.pdf xxxxxxxA Spiritual Guide To Truth v10.pdf xxxxxxx
A Spiritual Guide To Truth v10.pdf xxxxxxx
 
Amil baba in Lahore /Amil baba in Karachi /Amil baba in Pakistan
Amil baba in Lahore /Amil baba in Karachi /Amil baba in PakistanAmil baba in Lahore /Amil baba in Karachi /Amil baba in Pakistan
Amil baba in Lahore /Amil baba in Karachi /Amil baba in Pakistan
 
Genesis 1:10 || Meditate the Scripture daily verse by verse
Genesis 1:10  ||  Meditate the Scripture daily verse by verseGenesis 1:10  ||  Meditate the Scripture daily verse by verse
Genesis 1:10 || Meditate the Scripture daily verse by verse
 
Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...Authentic Black magic, Kala ilam expert in UAE  and Kala ilam specialist in S...
Authentic Black magic, Kala ilam expert in UAE and Kala ilam specialist in S...
 
Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...
Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...
Famous Kala Jadu, Black magic specialist in Lahore and Kala ilam expert in ka...
 
Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...
Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...
Top 10 Amil baba list Famous Amil baba In Pakistan Amil baba Kala jadu in Raw...
 

Next Generation Developer Testing: Parameterized Testing

  • 1. Next Generation Developer Testing: Parameterized Testing Tao Xie Department of Computer Science University of Illinois at Urbana-Champaign Email: taoxie@illinois.edu http://taoxie.cs.illinois.edu/ In Collaboration with Microsoft Research
  • 2. 2 Introduction  Developer (aka unit) testing – a widely adopted practice for ensuring high quality software  A conventional unit test (CUT): small program with test inputs and test assertions void AddTest() { HashSet set = new HashSet(); set.Add(7); set.Add(3); Assert.IsTrue(set.Count == 2); } Test Scenario Test Assertions Test Data
  • 3. 3 Parameterized Unit Tests (PUTs)  Recent advances in unit testing introduced PUTs void AddSpec(int x, int y) { HashSet set = new HashSet(); set.Add(x); set.Add(y); Assert.AreEqual(x == y, set.Count == 1); Assert.AreEqual(x != y, set.Count == 2); }  PUTs separate two concerns: • Specification of externally visible behavior (assertions) • Selection of internally relevant test inputs (coverage)
  • 4. 4 Parameterized Unit Tests (PUTs)  More beneficial than CUTs • Help describe behaviors for all test arguments  Address two main issues with CUTs • Missing test data required for exercising important behaviors  Low fault-detection capability • Including test data that exercises the same behaviour  Redundant unit tests
  • 5. 5 An Example using IntStack public void CUT1() { int elem = 1; IntStack stk = new IntStack(); stk.Push(elem); Assert.AreEqual(1, stk.Count()); } Three CUTs public void CUT2() { int elem = 30; IntStack stk = new IntStack(); stk.Push(elem); Assert.AreEqual(1, stk.Count()); } public void CUT3() { int elem1 = 1, elem2 = 30; IntStack stk = new IntStack(); stk.Push(elem1); stk.Push(elem2); Assert.AreEqual(2, stk.Count()); }  CUT1 and CUT2 exercise push with different test data  CUT3 exercises push when stack is not empty Two main issues with CUTs:  Fault-detection capability issue: undetected defect where things go wrong when passing a negative value to push  Redundant test issue: CUT2 is redundant with respect to CUT1
  • 6. 6 An Example using IntStack public void CUT1() { int elem = 1; IntStack stk = new IntStack(); stk.Push(elem); Assert.AreEqual(1, stk.Count()); } Three CUTs public void CUT2() { int elem = 30; IntStack stk = new IntStack(); stk.Push(elem); Assert.AreEqual(1, stk.Count()); } public void CUT3() { int elem1 = 1, elem2 = 30; IntStack stk = new IntStack(); stk.Push(elem1); stk.Push(elem2); Assert.AreEqual(2, stk.Count()); }  No need to describe test data • Generated automatically  Single PUT replaces multiple CUTs • With reduced size of test code public void PUT(int[] elem) { Assume.IsTrue(elem != null); IntStack stk = new IntStack(); for(int i in elem) stk.push(elem); Assert.AreEqual(elem.Length, stk.count()); } An equivalent PUT
  • 7. Parameterized Unit Tests are Algebraic Specifications • A Parameterized Unit Test can be read as a universally quantified, conditional axiom. void ReadWrite(Storage s, string name, string data) { Assume.IsTrue(s!=null && name!=null && data!=null); s.WriteResource(name, data); var readData = s.ReadResource(name); Assert.AreEqual(data, readData); }  Storage s, string name, string data: s ≠ null ⋀ name ≠ null ⋀ data ≠ null ⇒ equals( ReadResource(WriteResource(s,name,data).state, name).retval, data)
  • 8. Parameterized Unit Testing is going mainstream Parameterized Unit Tests (PUTs) commonly supported by various test frameworks • .NET: Supported by .NET test frameworks – http://www.mbunit.com/ – http://www.nunit.org/ … • Java: Supported by JUnit 4.X – http://www.junit.org/ Generating test inputs for PUTs supported by tools • .NET: Supported by Microsoft Visual Studio 2015 IntelliTest – Formerly Microsoft Research Pex: http://research.microsoft.com/pex/ • Java: Supported by Agitar AgitarOne – http://www.agitar.com/
  • 9. Parameterized Tests in JUnit 9 https://github.com/junit-team/junit/wiki/Parameterized-tests
  • 11. Assumptions and Assertions void PexAssume.IsTrue(bool c) { if (!c) throw new AssumptionViolationException(); } void PexAssert.IsTrue(bool c) { if (!c) throw new AssertionViolationException(); } • Assumptions and assertions induce branches • Executions which cause assumption violations are ignored, not reported as errors or test cases
  • 12. Test Data Generation • Human – Expensive, incomplete, … • Brute Force – Pairwise, predefined data, etc… • Semi - Random – Cheap, Fast – “It passed a thousand tests” feeling • Dynamic Symbolic Execution: IntelliTest/Pex, SAGE, CUTE, … – Automated white-box – Not random – Constraint Solving
  • 13. 13 void CoverMe(int[] a) { if (a == null) return; if (a.Length > 0) if (a[0] == 1234567890) throw new Exception("bug"); } a.Length>0 a[0]==123… TF T F F a==null T Constraints to solve a!=null a!=null && a.Length>0 a!=null && a.Length>0 && a[0]==123456890 Input null {} {0} {123…} Execute&MonitorSolve Choose next path Observed constraints a==null a!=null && !(a.Length>0) a==null && a.Length>0 && a[0]!=1234567890 a==null && a.Length>0 && a[0]==1234567890  Generates test data systematically Done: There is no path left. Background: DSE
  • 14. Pex4Fun http://pex4fun.com/ Nikolai Tillmann, Jonathan De Halleux, Tao Xie, Sumit Gulwani and Judith Bishop. Teaching and Learning Programming and Software Engineering via Interactive Gaming. In ICSE 2013, SEE. http://taoxie.cs.illinois.edu/publications/icse13see-pex4fun.pdf 1,703,247 clicked 'Ask Pex!'
  • 15. Pex4Fun • Click http://pex4fun.com/default.aspx?language=CSharp&sa mple=_Template • Copy and modify the following code snippet in the code editing box (or simply click here) using System; using Microsoft.Pex.Framework; using Microsoft.Pex.Framework.Settings; [PexClass] public class Program { [PexMethod]//[PexMethod(TestEmissionFilter=PexTestEmissionFilter.All)] public static string testMethod(int x, int y) { PexAssume.IsTrue(y >= 0);//replace here with your assumption //... enter your code under test here //if (x == 10000) throw new Exception(); PexAssert.IsTrue(y >= 0);//replace here with your assertion return PexSymbolicValue.GetPathConditionString(); } } https://sites.google.com/site/teachpex/Home/pex-usage-tips
  • 16. Microsoft Visual Studio 2015 IntelliTest 16 https://msdn.microsoft.com/en-us/library/dn823749.aspx
  • 17. Microsoft Visual Studio 2015 IntelliTest 17https://msdn.microsoft.com/en-us/library/dn823749.aspx
  • 18. 18 Recall: An Example using IntStack public void CUT1() { int elem = 1; IntStack stk = new IntStack(); stk.Push(elem); Assert.AreEqual(1, stk.Count()); } Three CUTs public void CUT2() { int elem = 30; IntStack stk = new IntStack(); stk.Push(elem); Assert.AreEqual(1, stk.Count()); } public void CUT3() { int elem1 = 1, elem2 = 30; IntStack stk = new IntStack(); stk.Push(elem1); stk.Push(elem2); Assert.AreEqual(2, stk.Count()); }  No need to describe test data • Generated automatically  Single PUT replaces multiple CUTs • With reduced size of test code public void PUT(int[] elem) { Assume.IsTrue(elem != null); IntStack stk = new IntStack(); for(int i in elem) stk.push(elem); Assert.AreEqual(elem.Length, stk.count()); } An equivalent PUT
  • 19. 19 Test Generalization: CUTs  PUT Major Steps • S1: Parameterize • S2: Generalize Test Oracle • S3: Add Assumptions Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 20. Example 00: public class SettingsGroup{ 01: MSS storage; ... 02: public SettingsGroup(MSS storage) { 03: this.storage = storage; 04: } 05: public void SaveSetting(string sn, object sv) { 06: object ov = storage.GetSetting( sn ); 07: //Avoid change if there is no real change 08: if (ov != null ) { 09: if(ov is string && sv is string && (string)ov==(string)sv || 10: ov is int && sv is int && (int)ov==(int)sv || 11: ov is bool&& sv is bool&& (bool)ov==(bool)sv || 12: ov is Enum&& sv is Enum&& ov.Equals(sv)) 13: return; 14: } 15: storage.SaveSetting(sn, sv); 16: if (Changed != null) 17: Changed(this, new SettingsEventArgs(sn)); 18: }} 20 SettingsGroup class of NUnit with the SaveSetting method under test
  • 21. An Existing CUT 00: public class SettingsGroup{ 01: MSS storage; ... 02: public SettingsGroup(MSS storage) { 03: this.storage = storage; 04: } 05: public void SaveSetting(string sn, object sv) { 06: object ov = storage.GetSetting( sn ); 07: //Avoid change if there is no real change 08: if (ov != null ) { 09: if(ov is string && sv is string && (string)ov==(string)sv || 10: ov is int && sv is int && (int)ov==(int)sv || 11: ov is bool&& sv is bool&& (bool)ov==(bool)sv || 12: ov is Enum&& sv is Enum&& ov.Equals(sv)) 13: return; 14: } 15: storage.SaveSetting(sn, sv); 16: if (Changed != null) 17: Changed(this, new SettingsEventArgs(sn)); 18: }} 21 00: //tg is of type SettingsGroup 01: [Test] 02: public void TestSettingsGroup() { 03: tg.SaveSetting("X",5); 04: tg.SaveSetting("NAME","Tom"); 05: Assert.AreEqual(5,tg.GetSetting("X")); 06: Assert.AreEqual("Tom",tg.GetSetting("NAME")); 07: } Existing CUT SettingsGroup class of NUnit with the SaveSetting method under test
  • 22. ? Issues with Existing CUT 22 Only CUT for verifying SaveSetting method • Does not verify the behavior for the types bool and enum • Does not cover the true branch in Line 8 Does test generalization addresses these two issues … 06: object ov = storage.GetSetting( sn ); 07: //Avoid change if there is no real change 08: if (ov != null ) { …
  • 23. 23 S1 - Parameterize  Promote all primitive values as arguments • name of setting as a parameter of type string • string “TOM” and int 5 as a parameter of type object helping IntelliTest/Pex generate concrete values based on the constraints encountered in different paths  Promote non-primitive objects such as receiver objects as arguments helping IntelliTest/Pex generate object states for the receiver objects that can cover additional paths //Original CUT 02: public void TestSettingsGroup() { 03: tg.SaveSetting("X",5); 04: tg.SaveSetting("NAME","Tom");…. //New PUT 02: public void TestSave( SettingsGroup st, string sn, object sv){ ….
  • 24. 24 S2 – Generalize Test Oracle  Replace the constant value, “TOM” and 5, with the relevant parameter of the PUT //Original CUT 02: public void TestSettingsGroup() { 03: tg.SaveSetting("X",5); 04: tg.SaveSetting("NAME","Tom"); 05: Assert.AreEqual(5,tg.GetSetting("X")); 06: Assert.AreEqual("Tom",tg.GetSetting("NAME")); …. //New PUT 02: public void TestSave(SettingsGroup st, string sn, object sv){ 03: st.SaveSetting(sn, sv); 04: PexAssert.AreEqual(sv,st.GetSetting(sn));
  • 25. //New PUT 02: public void TestSave(SettingsGroup st, string sn, object sv){ 03: st.SaveSetting(sn, sv); 04: PexAssert.AreEqual(sv,st.GetSetting(sn)); 25 S3 – Add Assumptions IntelliTest/Pex requires guidance in generating legal values for the parameters E.g., Add the tag PexAssumeUnderTest (PAUT) with the parameter, i.e., generated value should not be null //New PUT 02: public void TestSave([PAUT]SettingsGroup st, [PAUT]string sn, [PAUT] object sv){ 03: st.SaveSetting(sn, sv); 04: PexAssert.AreEqual(sv,st.GetSetting(sn));
  • 26. 26 Example Summary  Resulting PUT Result of S1  Parameters “sn” and “sv”  Result of S2  Line 4: “st.GetSetting(sn)”  Result of S3  Parameter “st” 00: //PAUT: PexAssumeUnderTest 01: [PexMethod] 02: public void TestSave([PAUT]SettingsGroup st, [PAUT] string sn, [PAUT] object sv) { 03: st.SaveSetting(sn, sv); 04: PexAssert.AreEqual(sv,st.GetSetting(sn)); 05: }
  • 27. 27 Example - Test Generalization Results  Achieved 10% branch cov  Required 2 method calls with values of type string and int 2: public void TestSave([PAUT]SettingsGroup st, [PAUT]string sn, [PAUT] object sv){ 3: st.SaveSetting(sn, sv); 4: PexAssert.AreEqual (sv,st.GetSetting(sn)); 2: public void TestSettingsGroup() { 3: tg.SaveSetting("X",5); 4: tg.SaveSetting("NAME","Tom");….  Achieved 90% branch cov  Required only 1 method call, sufficient to test for all types (parameter of type object) Original CUT New PUT
  • 28. EXAMPLE PATTERNS FOR PARAMETERIZED UNIT TESTS http://research.microsoft.com/pex/patterns.pdf
  • 29. Pattern 4A • Assume, Arrange, Act, Assert [PexMethod] void Add(List target, T value) { PexAssume.IsNotNull(target); // assume var count = target.Count; // arrange target.Add(value); // act Assert(target.Count == count + 1)//assert }
  • 30. Pattern Roundtrip • For an API f(x), f-1(f(x)) = x for all x void ToStringParseRoundtrip(int value) { string s = value.ToString(); int parsed = int.Parse(s); Assert.AreEqual(value, parsed); }
  • 31. Pattern State Relation • Observe a state change void ContainedAfterAdd(string value) { var list = new List<string>(); list.Add(value); Assert.IsTrue(list.Contains(value)); }
  • 32. Pattern Commutative Diagram • Given two implementations f and g of the same function, each possible requiring a different number of steps, i.e. f(x)=f1(f2(…(fn(x)…)), and g(x)=g1(g2(… (gm(x)…)), then it should hold that f1(f2(…(fn(x))…) = g1(g2(…(gm(x)…)) for all x. string Multiply(string x, string y); int Multiply(int x, int y); void CommutativeDiagram1(int x, int y) { string z1 = Multiply(x, y).ToString(); string z2 = Multiply(x.ToString(), y.ToString()); PexAssert.AreEqual(z1, z2); }
  • 33. Code Hunt: Turning Pex/PUT into a Coding Game https://www.codehunt.com/
  • 34. Behind the Scene of Code Hunt Secret Implementation class Secret { public static int Puzzle(int x) { if (x <= 0) return 1; return x * Puzzle(x-1); } } Player Implementation class Player { public static int Puzzle(int x) { return x; } } class Test { public static void Driver(int x) { if (Secret.Puzzle(x) != Player.Puzzle(x)) throw new Exception(“Mismatch”); } } behavior Secret Impl == Player Impl 34
  • 35. It’s a game! • iterative gameplay • adaptive • personalized • no cheating • clear winning criterion code test cases Bishop et al. Code Hunt: Experience with Coding Contests at Scale. ICSE 2015, JSEET http://taoxie.cs.illinois.edu/publications/icse15jseet-codehunt.pdf
  • 36. Next Generation Developer Testing: Parameterized Testing Microsoft Visual Studio 2015 - IntelliTest JUnit TheoriesPexMethod Test Generalization CUTs  PUTs PUT Patterns  PUTs …
  • 38. Next Generation Developer Testing: Parameterized Testing Microsoft Visual Studio 2015 - IntelliTest JUnit TheoriesPexMethod Test Generalization CUTs  PUTs PUT Patterns  PUTs … http://taoxie.cs.illinois.edu/taoxie@illinois.edu https://sites.google.com/site/teachpex/
  • 39. Pattern Roundtrip • For an API f(x), f-1(f(x)) = x for all x void PropertyRoundtrip(string value) { var target = new Foo(); target.Name = value; var roundtripped = target.Name; Assert.AreEqual(value, roundtripped); } http://research.microsoft.com/pex/patterns.pdf
  • 40. Pattern Sanitized Roundtrip • For an API f(x), f-1(f(f-1(x)) = f-1(x) for all x void ParseToString(string x) { var normalized = int.Parse(x); var intermediate = normalized.ToString(); var roundtripped = int.Parse(intermediate); Assert(normalized == roundtripped); } http://research.microsoft.com/pex/patterns.pdf
  • 41. Pattern Reachability • Indicate which portions of a PUT should be reachable. [PexExpectedGoals] public void InvariantAfterParsing(string input) { ComplexDataStructure x; bool success = ComplexDataStructure.TryParse( input, out x); PexAssume.IsTrue(success); PexGoal.Reached(); x.AssertInvariant(); } http://research.microsoft.com/pex/patterns.pdf
  • 42. Pattern Regression Tests • Generated test asserts any observed value – Return value, out parameters, PexGoal • When code evolves, breaking changes in observable will be discovered int AddTest(int a, int b) { return a + b; } void AddTest01() { var result = AddTest(0, 0); Assert.AreEqual(0, result); } http://research.microsoft.com/pex/patterns.pdf
  • 43. Pattern Same Observable Behavior • Given two methods f(x) and g(x), and a method b(y) that observes the result or the exception behavior of a method, assert that f(x) and g(x) have same observable behavior under b, i.e. b(f(x)) = b(g(x)) for all x. public void ConcatsBehaveTheSame( string left, string right) { PexAssert.AreBehaviorsEqual( () => StringFormatter.ConcatV1(left, right), () => StringFormatter.ConcatV2(left, right)); } http://research.microsoft.com/pex/patterns.pdf
  • 44. Pattern Allowed Exception • Allowed exception -> negative test case [PexAllowedException(typeof(ArgumentException))] void Test(object item) { var foo = new Foo(item) // validates item // generated test (C#) [ExpectedException(typeof(ArgumentException))] void Test01() { Test(null); // argument check } http://research.microsoft.com/pex/patterns.pdf
  • 45. 45 S4 – Add Factory Method  IntelliTest/Pex requires guidance handling non- primitive objects e.g., Add factory methods that generate instances of non-primitive objects 00: //PAUT: PexAssumeUnderTest 01: //MSS: MemorySettingsStorage (class) 01: [PexFactoryMethod(typeof(MSS))] 02: public static MSS Create([PAUT] string[] sn, [PAUT]object[] sv) { 03: PexAssume.IsTrue(sn.Length == sv.Length); 04: PexAssume.IsTrue(sn.Length > 0); 05: MSS mss = new MSS(); 06: for(int count = 0; count < sn.Length; count++) { 07: mss.SaveSetting(sn[count], sv[count]); 08: } 09: return mss; 10: } • Help create different object states for MSS Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 46. 46 S5 – Add Mock Object  Pex faces challenges in handling code that interacts with external environment, e.g., file system • Write mock objects to assist Pex and to test features in isolation Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 47. 47 Empirical Study  RQ1: Branch coverage • How much higher percentage of branch coverage is achieved by retrofitted PUTs compared to existing CUTs?  RQ2: Defect detection • How many new defects (that are not detected by existing CUTs) are detected by PUTs and vice-versa?  RQ3: Generalization effort • How much effort is required for generalizing CUTs to PUTs? Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 48. Study Setup  Three Subject Applications 48 Subjects Downloads Code Under Test # Classes # Methods KLOC Test Code # Classes # CUTs KLOC NUnit 193,563 9 87 1.4 9 49 0.9 DSA 3,239 27 259 2.4 20 337 2.5 QuickGraph 7,969 56 463 6.2 9 21 1.2 TOTAL 92 809 10 38 407 4.6  High downloads count  Lines of code under test: 10 KLOC  Number of CUTs: 407 Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 49. Study Setup – cont. 49  Conducted by the first and second authors • No prior knowledge of subjects • Two years of experience with PUTs and Pex • Retrofitted 407 CUTs (4.6 KLOC) as 224 PUTs (4.0 KLOC) Generated three categories of CUTs • C1: Existing CUTs • C2: CUTs generated from PUTs • C3: Existing CUTs + RTs (tests generated using Randoop)  Help show that benefits of generalization cannot be achieved by generating tests randomly Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 50. RQ1: Branch Coverage 50 Subjects # RTs Branch Coverage CUTs (%) CUTs + RTs (%) PUTs (%) Overall Increase (%) Maximum Increase (%) NUnit 144 78 78 88 10 52 DSA 615 91 91 92 1 1 QuickGrap h 3628 87 88 89 2 11 CUTs + RTs:  Added 144, 615, and 3628 tests using Randoop  Branch coverage increased by 0%, 0%, and 1% CUTs generated from PUTs:  Branch coverage increased by 10%, 1%, and 2% Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 51. RQ2: Defect Detection 51  RTs: CUTs generated using Randoop  Pex without generalized PUTs: CUTs generated by applying Pex on public methods  CUTs generated from PUTs: CUTs generated by applying Pex on generalized PUTs • Detected all defects detected by the first two categories CUTs category #Failing Tests DSA NUnit QuickGraph # Real Defects Basic CUTs 0 0 0 0 RTs 90 25 738 4 Pex without generalized PUTs 23 170 17 2 CUTs generated from PUTs 15 4 0 19 Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf
  • 52. RQ3: Generalization Effort 52  Both authors conducted comparable amount of generalization  Effort spent in hours • NUnit: 2.8 hrs • DSA: 13.8 hrs • QuickGraph: 1.5 hrs  Effort is worthwhile compared to benefits of test generalization Thummalapenta et al. Retrofitting Unit Tests for Parameterized Unit Testing. In FASE 2011 http://taoxie.cs.illinois.edu/publications/fase11-put.pdf