SlideShare a Scribd company logo
1 of 38
Download to read offline
Unit Testing
Venkatesh-Prasad Ranganath
Kansas State University
Slides with * in title capture whiteboard content
Unit Testing Terms
• Test
• UUT / SUT
• Test case
• Structure
• Test Data and Test Logic
• Assertions
• Test Suite
• Test Fixture
• Setup
• Teardown
• Test Runner
• Testing Framework
Unit Testing Terms
• Test(ing) - The act of exercising software with test cases
• UUT / SUT - Unit / System Under Test
• Test Case - An identifiable unit of testing; often, bundled as a
method/function
• Structure - Setup, Execution, Verify, Teardown (Arrange, Act,
Assert)
• Test Data and Test Logic - Integral components of a test case
• Assertion - Preferred mechanism to state/communicate test
outcome
if <actual outcome is not the same as expected outcome> then
raise AssertionError() # test failed
else
pass
Unit Testing Terms
• Test Suite - A collection of test cases
• Test Fixture
• Setup - Preparation to be performed before tests
• Teardown - Cleanup to be performed after tests
• Test Runner - A component that orchestrates the
execution of test cases and collects the test
verdicts.
• Testing Framework - A framework that supports test
automation; typically, includes test runner, reporting
capability, coverage calculation, and other features.
XUnit Testing Frameworks in
Different Languages
Python
• TestCar — test class name
• test_drive — test method name
• unittest, Nose, PyTest
• Mostly based on convention and
consistent practice
Java
• CarTest
• testDrive
• @Test — Test method annotation
• JUnit, TestNG, Spock
• Mostly based on convention and
consistent practice
Common wisdom in unit testing
• Many test cases for one method/function
• Many test classes for one class
Coding Session*
checkId(x) Specification
• x is a string
• If not, raise TypeError
• x represents a numerical value
• If not, raise ValueError
• True if x has 9 characters
• False otherwise
Test Runner
How does a test runner work?
Test Runner
How does a test runner work?
• find/discover test cases
• based on rules or conventions or annotations
• E.g., functions whose name starts with “test”
• execute each test case
• collect and compile the test verdicts
Testing Framework
How do most XUnit testing framework determine test
verdict?
Testing Framework
How do most XUnit testing framework determine test
verdict?
• Failure if test throws exception X
• Pass if test completes (without any exception /
failures)
• Error otherwise
• Most XUnit framework rely on assert statements
and use the corresponding exception (e.g.,
AssertionError) as exception X
Assertions
• What's the deal with assertion?
• What are the pros and cons of using assertions?
• With regards to inputs/outputs, when to use
assertion and when to use error handling?
Assertions
What's the deal with assertion?
• Assertion is a mechanism to check if (should-be-
true) properties are true; typically, guarantees
• E.g., input array to a binary search function
should be sorted
• E.g., output array returned by a sorting function
should be of the same length as the input array
What are the pros and cons of using assertions?
• + Easily and succinctly add checks to programs
• - Imposes performance penalties
Assertions
With regards to inputs, when to use assertion and when to use error
checking? (Guidance)
• Use error checking to handle both valid and invalid inputs and
generate corresponding outcome; when both valid and invalid
inputs are expected.
• E.g., sq_root will return the square root of the input integer if it
is positive; otherwise, raise ValueError exception.
• Input provider does not promise to ensure the validity of input.
• Use assertions to check inputs are valid; when only valid inputs
are expected.
• E.g., sq_root will return square root of input integer and it
should be invoked only with positive integers.
• Input provider promises to provide the validity of input.
Test Case Structure
Setup, Execution, Verify, Teardown (Arrange, Act,
Assert)
• What do we do in Execution?
• What do we do in Verify?
• What is the common code pattern to test for
outcome and absence/presence of exceptional
behavior?
• Setup and Teardown are considered part of Test
Fixture
Test Case Structure
• What do we do in Execution?
• Set up data/objects necessary to obtain actual
outcome and observe the outcome
• What do we do in Verify?
• Compare the observed outcome to expected
outcome
Test Case Structure
What is the common code pattern to test for output?
<action_1>
<action_2>
…
output = <action_n>
assert output == <expected_output>
We use an appropriate operator in place of == to
compare observed output and expected output
Test Case Structure
What is the common code pattern to test for
absence of exceptional behavior/outcome?
try:
<action_1>
<action_2>
…
<action_n>
except:
assert False
Test Case Structure
What is the common code pattern to test for
presence of exceptional behavior/outcome?
try:
<action_1>
<action_2>
…
assert False
except:
pass
Test Case Structure
What is the common code pattern to test for presence of
specific exceptional behavior/outcome, i.e., raises exception X?
try:
<action_1>
<action_2>
…
assert False
except X:
pass
except:
assert False
Test Fixture
What should we do in Setup?
• ??
What should we do in Teardown?
• ??
Test Fixture
What should we do in Setup?
• Put the UUT in the state required for testing
• Perform actions common to a set of test cases
What should we do in Teardown?
• Perform clean up actions
• Release external resources
• Delete interim artifacts
• Restore system back to a good state
Parameterized Unit Tests
void TestAdd() {
ArrayList a = new ArrayList(0);
object o = new object();
a.Add(o);
Assert.IsTrue(a[0] == o);
}
- Parameterized Unit Tests by Nikolai Tillmann and Wolfram Schultz [FSE’05] (Section 2)
Parameterized Unit Tests
void TestAdd() {
ArrayList a = new ArrayList(0);
object o = new object();
a.Add(o);
Assert.IsTrue(a[0] == o);
}
void TestAdd(ArrayList a, object o) {
if (a != null) {
int i = a.Count;
a.Add(o);
Assert.IsTrue(a[i] == o);
}
}
- Parameterized Unit Tests by Nikolai Tillmann and Wolfram Schultz [FSE’05] (Section 2)
Parameterized Unit Tests
void TestAdd() {
ArrayList a = new ArrayList(0);
object o = new object();
a.Add(o);
Assert.IsTrue(a[0] == o);
}
void TestAdd(ArrayList a, object o) {
Assume.IsTrue(a != null);
int i = a.Count;
a.Add(o);
Assert.IsTrue(a[i] == o);
}
- Parameterized Unit Tests by Nikolai Tillmann and Wolfram Schultz [FSE’05] (Section 2)
Parameterized Unit Tests
• PUTs are test methods/cases generalized by allowing
parameters
• Most often, parameters correspond to test data (as in
example-based testing)
• PUTs are not test cases
• PUTs are templates of test cases
• Test cases are instantiations of PUTs with specific
arguments (inputs)
Test Structure
Example-based Test Case
1. Setup
2. Execute
3. Verify
4. Teardown
Parameterized Unit Tests
1. Check assumptions about
parameters/inputs
2. Setup
3. Execute
4. Verify
5. Teardown
Test Doubles (Harness)
What is a test double?
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
Test Doubles (Harness)
What is a test double?
• SUT may depend on components (DOCs) that
are not under test
• Test doubles are replacements for DOCs
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
Why use Test Doubles?
Why use Test Doubles?
• DOC is overly large / long setup time
• DOC is slow
• Do not want to mess existing DOC
• DOC is unavailable or not ready
• Test dependency on DOC interface (not
implementation)
Test
Usual Setup
Setup
Exercise
Verify
Teardown
SUT
DOC
Test
Test Doubles (Harness)
Setup
Exercise
Verify
Teardown
SUT
DOC
Test
Double
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
Test Doubles (Harness)
What are the different sorts of test doubles?
• Dummy
• Fake
• (Test) Stub
• Test Spy
• Mock Object
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
Test
Dummy
Setup
Exercise
Verify
Teardown
SUT
Dummy
Create
Install
Query
Empty
methods
that
throw
error if
invoked
- xUnit Test Patterns by Gerard Meszaros (Chapter 27)
How do we specify the values to be used in tests when their only usage is as irrelevant
arguments to SUT method calls?
We pass an object that has no implementation as an argument of a method called on the SUT.
Test
Fake
Setup
Exercise
Verify
Teardown
SUT
Fake
Create and Initialize
Install
Query
Data
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
How can we verify logic independently when depended-on objects cannot be used? How can
we avoid slow tests?
We replace a component that the SUT depends on with a much lighter-weight implementation.
Data
Test
Test Stub
Setup
Exercise
Verify
Teardown
SUT
Test
Stub
Create
Install
Query
Return
stock
values
Indirect input
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
How can we verify logic independently when it depends on indirect inputs from other software
components?
We replace a real object with a test-specific object that feeds the desired indirect inputs into the
system under test.
Test
Test Spy
Setup
Exercise
Verify
Teardown
SUT
Test
Spy
Create
Install
Query
Capture
output
values
for later
checking
Indirect output
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
How do we implement behavior verification? How can we verify logic independently when it has
indirect outputs to other software components?
We use a test double to capture the indirect output calls made to another component by the SUT
for later verification by the test.
Test
Mock Object
Setup
Exercise
Verify
Teardown
SUT
Mock
Object
Create
Install
Final verification
Check
output
values
and
perform
final
check
Indirect output
- xUnit Test Patterns by Gerard Meszaros (Chapter 23)
How do we implement behavior verification for indirect outputs of the SUT? How can we verify
logic independently when it depends on indirect inputs from other software components?
We replace an object on which the SUT depends on with a test-specific object that verifies it is
being used correctly by the SUT.

More Related Content

What's hot

What's hot (20)

IT Talk TestNG 6 vs JUnit 4
IT Talk TestNG 6 vs JUnit 4IT Talk TestNG 6 vs JUnit 4
IT Talk TestNG 6 vs JUnit 4
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
 
3 j unit
3 j unit3 j unit
3 j unit
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
05 junit
05 junit05 junit
05 junit
 
Effective Readable unit testing with junit5
Effective Readable unit testing with junit5Effective Readable unit testing with junit5
Effective Readable unit testing with junit5
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 
testng
testngtestng
testng
 
When assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsWhen assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() fails
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Junit
JunitJunit
Junit
 

Similar to Unit testing [4] - Software Testing Techniques (CIS640)

Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnitAktuğ Urun
 
Tests and Testability: Apex Structure and Strategy
Tests and Testability: Apex Structure and StrategyTests and Testability: Apex Structure and Strategy
Tests and Testability: Apex Structure and StrategySalesforce Developers
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTscatherinewall
 
Dynamic analysis in Software Testing
Dynamic analysis in Software TestingDynamic analysis in Software Testing
Dynamic analysis in Software TestingSagar Pednekar
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxKnoldus Inc.
 
xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database TestingChris Oldwood
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 
Software engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designSoftware engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designMaitree Patel
 
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)Jen Wong
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)Steve Upton
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkHumberto Marchezi
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
utPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLutPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLSteven Feuerstein
 

Similar to Unit testing [4] - Software Testing Techniques (CIS640) (20)

Unit testing basics
Unit testing basicsUnit testing basics
Unit testing basics
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Tests and Testability: Apex Structure and Strategy
Tests and Testability: Apex Structure and StrategyTests and Testability: Apex Structure and Strategy
Tests and Testability: Apex Structure and Strategy
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Dynamic analysis in Software Testing
Dynamic analysis in Software TestingDynamic analysis in Software Testing
Dynamic analysis in Software Testing
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
 
Skillwise Unit Testing
Skillwise Unit TestingSkillwise Unit Testing
Skillwise Unit Testing
 
xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Software engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit designSoftware engineering Testing technique,test case,test suit design
Software engineering Testing technique,test case,test suit design
 
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
 
Unit testing
Unit testingUnit testing
Unit testing
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
utPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLutPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQL
 

More from Venkatesh Prasad Ranganath

SeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android AppsSeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android AppsVenkatesh Prasad Ranganath
 
Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...Venkatesh Prasad Ranganath
 
Benchpress: Analyzing Android App Vulnerability Benchmark Suites
Benchpress:  Analyzing Android App Vulnerability Benchmark SuitesBenchpress:  Analyzing Android App Vulnerability Benchmark Suites
Benchpress: Analyzing Android App Vulnerability Benchmark SuitesVenkatesh Prasad Ranganath
 
Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Compatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace ComparisonCompatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace ComparisonVenkatesh Prasad Ranganath
 

More from Venkatesh Prasad Ranganath (17)

SeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android AppsSeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android Apps
 
Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...
 
Benchpress: Analyzing Android App Vulnerability Benchmark Suites
Benchpress:  Analyzing Android App Vulnerability Benchmark SuitesBenchpress:  Analyzing Android App Vulnerability Benchmark Suites
Benchpress: Analyzing Android App Vulnerability Benchmark Suites
 
Why do Users kill HPC Jobs?
Why do Users kill HPC Jobs?Why do Users kill HPC Jobs?
Why do Users kill HPC Jobs?
 
Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)
 
Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)
 
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
 
Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)
 
Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)
 
Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)
 
Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)
 
Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)
 
Compatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace ComparisonCompatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace Comparison
 
My flings with data analysis
My flings with data analysisMy flings with data analysis
My flings with data analysis
 
Data analytics, a (short) tour
Data analytics, a (short) tourData analytics, a (short) tour
Data analytics, a (short) tour
 
R language, an introduction
R language, an introductionR language, an introduction
R language, an introduction
 
Pattern-based Features
Pattern-based FeaturesPattern-based Features
Pattern-based Features
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Unit testing [4] - Software Testing Techniques (CIS640)

  • 1. Unit Testing Venkatesh-Prasad Ranganath Kansas State University Slides with * in title capture whiteboard content
  • 2. Unit Testing Terms • Test • UUT / SUT • Test case • Structure • Test Data and Test Logic • Assertions • Test Suite • Test Fixture • Setup • Teardown • Test Runner • Testing Framework
  • 3. Unit Testing Terms • Test(ing) - The act of exercising software with test cases • UUT / SUT - Unit / System Under Test • Test Case - An identifiable unit of testing; often, bundled as a method/function • Structure - Setup, Execution, Verify, Teardown (Arrange, Act, Assert) • Test Data and Test Logic - Integral components of a test case • Assertion - Preferred mechanism to state/communicate test outcome if <actual outcome is not the same as expected outcome> then raise AssertionError() # test failed else pass
  • 4. Unit Testing Terms • Test Suite - A collection of test cases • Test Fixture • Setup - Preparation to be performed before tests • Teardown - Cleanup to be performed after tests • Test Runner - A component that orchestrates the execution of test cases and collects the test verdicts. • Testing Framework - A framework that supports test automation; typically, includes test runner, reporting capability, coverage calculation, and other features.
  • 5. XUnit Testing Frameworks in Different Languages Python • TestCar — test class name • test_drive — test method name • unittest, Nose, PyTest • Mostly based on convention and consistent practice Java • CarTest • testDrive • @Test — Test method annotation • JUnit, TestNG, Spock • Mostly based on convention and consistent practice Common wisdom in unit testing • Many test cases for one method/function • Many test classes for one class
  • 6. Coding Session* checkId(x) Specification • x is a string • If not, raise TypeError • x represents a numerical value • If not, raise ValueError • True if x has 9 characters • False otherwise
  • 7. Test Runner How does a test runner work?
  • 8. Test Runner How does a test runner work? • find/discover test cases • based on rules or conventions or annotations • E.g., functions whose name starts with “test” • execute each test case • collect and compile the test verdicts
  • 9. Testing Framework How do most XUnit testing framework determine test verdict?
  • 10. Testing Framework How do most XUnit testing framework determine test verdict? • Failure if test throws exception X • Pass if test completes (without any exception / failures) • Error otherwise • Most XUnit framework rely on assert statements and use the corresponding exception (e.g., AssertionError) as exception X
  • 11. Assertions • What's the deal with assertion? • What are the pros and cons of using assertions? • With regards to inputs/outputs, when to use assertion and when to use error handling?
  • 12. Assertions What's the deal with assertion? • Assertion is a mechanism to check if (should-be- true) properties are true; typically, guarantees • E.g., input array to a binary search function should be sorted • E.g., output array returned by a sorting function should be of the same length as the input array What are the pros and cons of using assertions? • + Easily and succinctly add checks to programs • - Imposes performance penalties
  • 13. Assertions With regards to inputs, when to use assertion and when to use error checking? (Guidance) • Use error checking to handle both valid and invalid inputs and generate corresponding outcome; when both valid and invalid inputs are expected. • E.g., sq_root will return the square root of the input integer if it is positive; otherwise, raise ValueError exception. • Input provider does not promise to ensure the validity of input. • Use assertions to check inputs are valid; when only valid inputs are expected. • E.g., sq_root will return square root of input integer and it should be invoked only with positive integers. • Input provider promises to provide the validity of input.
  • 14. Test Case Structure Setup, Execution, Verify, Teardown (Arrange, Act, Assert) • What do we do in Execution? • What do we do in Verify? • What is the common code pattern to test for outcome and absence/presence of exceptional behavior? • Setup and Teardown are considered part of Test Fixture
  • 15. Test Case Structure • What do we do in Execution? • Set up data/objects necessary to obtain actual outcome and observe the outcome • What do we do in Verify? • Compare the observed outcome to expected outcome
  • 16. Test Case Structure What is the common code pattern to test for output? <action_1> <action_2> … output = <action_n> assert output == <expected_output> We use an appropriate operator in place of == to compare observed output and expected output
  • 17. Test Case Structure What is the common code pattern to test for absence of exceptional behavior/outcome? try: <action_1> <action_2> … <action_n> except: assert False
  • 18. Test Case Structure What is the common code pattern to test for presence of exceptional behavior/outcome? try: <action_1> <action_2> … assert False except: pass
  • 19. Test Case Structure What is the common code pattern to test for presence of specific exceptional behavior/outcome, i.e., raises exception X? try: <action_1> <action_2> … assert False except X: pass except: assert False
  • 20. Test Fixture What should we do in Setup? • ?? What should we do in Teardown? • ??
  • 21. Test Fixture What should we do in Setup? • Put the UUT in the state required for testing • Perform actions common to a set of test cases What should we do in Teardown? • Perform clean up actions • Release external resources • Delete interim artifacts • Restore system back to a good state
  • 22. Parameterized Unit Tests void TestAdd() { ArrayList a = new ArrayList(0); object o = new object(); a.Add(o); Assert.IsTrue(a[0] == o); } - Parameterized Unit Tests by Nikolai Tillmann and Wolfram Schultz [FSE’05] (Section 2)
  • 23. Parameterized Unit Tests void TestAdd() { ArrayList a = new ArrayList(0); object o = new object(); a.Add(o); Assert.IsTrue(a[0] == o); } void TestAdd(ArrayList a, object o) { if (a != null) { int i = a.Count; a.Add(o); Assert.IsTrue(a[i] == o); } } - Parameterized Unit Tests by Nikolai Tillmann and Wolfram Schultz [FSE’05] (Section 2)
  • 24. Parameterized Unit Tests void TestAdd() { ArrayList a = new ArrayList(0); object o = new object(); a.Add(o); Assert.IsTrue(a[0] == o); } void TestAdd(ArrayList a, object o) { Assume.IsTrue(a != null); int i = a.Count; a.Add(o); Assert.IsTrue(a[i] == o); } - Parameterized Unit Tests by Nikolai Tillmann and Wolfram Schultz [FSE’05] (Section 2)
  • 25. Parameterized Unit Tests • PUTs are test methods/cases generalized by allowing parameters • Most often, parameters correspond to test data (as in example-based testing) • PUTs are not test cases • PUTs are templates of test cases • Test cases are instantiations of PUTs with specific arguments (inputs)
  • 26. Test Structure Example-based Test Case 1. Setup 2. Execute 3. Verify 4. Teardown Parameterized Unit Tests 1. Check assumptions about parameters/inputs 2. Setup 3. Execute 4. Verify 5. Teardown
  • 27. Test Doubles (Harness) What is a test double? - xUnit Test Patterns by Gerard Meszaros (Chapter 23)
  • 28. Test Doubles (Harness) What is a test double? • SUT may depend on components (DOCs) that are not under test • Test doubles are replacements for DOCs - xUnit Test Patterns by Gerard Meszaros (Chapter 23)
  • 29. Why use Test Doubles?
  • 30. Why use Test Doubles? • DOC is overly large / long setup time • DOC is slow • Do not want to mess existing DOC • DOC is unavailable or not ready • Test dependency on DOC interface (not implementation)
  • 32. Test Test Doubles (Harness) Setup Exercise Verify Teardown SUT DOC Test Double - xUnit Test Patterns by Gerard Meszaros (Chapter 23)
  • 33. Test Doubles (Harness) What are the different sorts of test doubles? • Dummy • Fake • (Test) Stub • Test Spy • Mock Object - xUnit Test Patterns by Gerard Meszaros (Chapter 23)
  • 34. Test Dummy Setup Exercise Verify Teardown SUT Dummy Create Install Query Empty methods that throw error if invoked - xUnit Test Patterns by Gerard Meszaros (Chapter 27) How do we specify the values to be used in tests when their only usage is as irrelevant arguments to SUT method calls? We pass an object that has no implementation as an argument of a method called on the SUT.
  • 35. Test Fake Setup Exercise Verify Teardown SUT Fake Create and Initialize Install Query Data - xUnit Test Patterns by Gerard Meszaros (Chapter 23) How can we verify logic independently when depended-on objects cannot be used? How can we avoid slow tests? We replace a component that the SUT depends on with a much lighter-weight implementation. Data
  • 36. Test Test Stub Setup Exercise Verify Teardown SUT Test Stub Create Install Query Return stock values Indirect input - xUnit Test Patterns by Gerard Meszaros (Chapter 23) How can we verify logic independently when it depends on indirect inputs from other software components? We replace a real object with a test-specific object that feeds the desired indirect inputs into the system under test.
  • 37. Test Test Spy Setup Exercise Verify Teardown SUT Test Spy Create Install Query Capture output values for later checking Indirect output - xUnit Test Patterns by Gerard Meszaros (Chapter 23) How do we implement behavior verification? How can we verify logic independently when it has indirect outputs to other software components? We use a test double to capture the indirect output calls made to another component by the SUT for later verification by the test.
  • 38. Test Mock Object Setup Exercise Verify Teardown SUT Mock Object Create Install Final verification Check output values and perform final check Indirect output - xUnit Test Patterns by Gerard Meszaros (Chapter 23) How do we implement behavior verification for indirect outputs of the SUT? How can we verify logic independently when it depends on indirect inputs from other software components? We replace an object on which the SUT depends on with a test-specific object that verifies it is being used correctly by the SUT.