SlideShare una empresa de Scribd logo
1 de 34
Stéphane Ducasse 1
Stéphane Ducasse
stephane.ducasse@inria.fr
http://stephane.ducasse.free.fr/
Unit Testing
S.Ducasse 2
Goal
How can I trust that the
changes did not destroy
something?
What is my confidence in
the system?
How do I write tests?
What is unit testing?
What is SUnit?
S.Ducasse 3
Tests
• Tests represent your trust in the system
• Build them incrementally
• Do not need to focus on everything
• When a new bug shows up, write a test
• Even better write them before the code
• Act as your first client, better interface
• Active documentation always in sync
S.Ducasse 4
Testing Style
“The style here is to write a few lines of code, then a test that
should run, or even better, to write a test that won't run, then
write the code that will make it run.”
• write unit tests that thoroughly test a single class
• write tests as you develop (even before you implement)
• write tests for every new piece of functionality
“Developers should spend 25-50% of their time developing tests.”
S.Ducasse 5
But I can’t cover everything!
• Sure! Nobody can but
• When someone discovers a defect in your code, first write
a test that demonstrates the defect.
• Then debug until the test succeeds.
“Whenever you are tempted to type something into a print
statement or a debugger expression, write it as a test
instead.” Martin Fowler
S.Ducasse 6
Unit Testing
• Ensure that you get the specified behavior of the public
interface of a class
• Normally tests a single class
• A test
• Create a context,
• Send a stimulus,
• Check the results
S.Ducasse 7
SetTestCase
Class: SetTestCase
superclass:TestCase
SetTestCase>>testAdd
| empty |
empty := Set new. “Context”
empty add: 5. “Stimulus”
self assert: (empty includes: 5). “Check”
SetTestCase run: #testAdd
S.Ducasse 8
In a subclass ofTestCase
Each method starting with test*
Represents a test
Is automatically executed
The results of the test are collected in a TestResult
object
S.Ducasse 9
Examples
testExampleRunArray3
"this demonstrates that adjancent runs with equal
attributes are merged. "
| runArray |
runArray := RunArray new.
runArray
addLast:TextEmphasis normal times: 5;
addLast:TextEmphasis bold times: 5;
addLast:TextEmphasis bold times: 5.
self assert: (runArray runs size = 2).
S.Ducasse 10
Failures and Errors
• A failure is a failed assertion, i.e., an anticipated problem
that you test.
• An error is a condition you didn’t check for.
SetTestCase>>removeElementNotInSet
self should: [Set new remove: 1]
raise: Error
S.Ducasse 11
Good Tests
• Repeatable
• No human intervention
• “self-described”
• Change less often than the system
• Tells a story
S.Ducasse 12
_Unit Frameworks
• _Unit is a simple “testing framework” that provides:
• classes for writing Test Cases and Test Suites
• methods for setting up and cleaning up test data (“fixtures”)
• methods for making assertions
• textual and graphical tools for running tests
• _Unit distinguishes between failures and errors:
• A failure is a failed assertion, i.e., an anticipated problem that
you test.
• An error is a condition you didn’t check for.
S.Ducasse 13
JUnit
S.Ducasse 14
JUnit
• Junit (inspired by SUnit) is a simple “testing framework”
that provides:
• classes for writing Test Cases and Test Suites
• methods for setting up and cleaning up test data (“fixtures”)
• methods for making assertions
• textual and graphical tools for running tests
S.Ducasse 15
The JUnit Framework
S.Ducasse 16
ATesting Scenario
The framework calls the test methods that you define for your test cases.
S.Ducasse 17
SUnit
Original framework
S.Ducasse 18
SetTestCase
Class: SetTestCase
superclass:TestCase
SetTestCase>>testAddition
| s |
s := Set new.
s add: 5; add: 3.
self assert: s size = 2.
s add: 5.
self assert: s size = 2
S.Ducasse 19
Testing Remove
SetTestCase>>testAddition
| s |
s := Set new.
s add: 6.
self assert: s size = 1.
s remove: 6.
self assert: s size = 0.
self should: [s remove: 1000] raise: Error.
res := s remove: 5 ifAbsent: [33].
self assert: (res = 33)
S.Ducasse 20
Duplicating the Context
SetTestCase>>testOccurrences
| empty |
empty := Set new.
self assert: (empty occurrencesOf: 0) = 0.
empty add: 5; add:5.
self assert: (empty occurrencesOf: 5) = 1
S.Ducasse 21
setUp and TearDown
• Executed before and after each test
• setUp allows us to specify and reuse the context
• tearDown to clean after.
S.Ducasse 22
Example:Testing Set
• Class: SetTestCase
superclass:TestCase
instance variable:‘empty full’
• SetTestCase>>setUp
empty := Set new.
full := Set with: 6 with: 5
• The setUp is the context in which each test is run.
S.Ducasse 23
Tests…
SetTestCase>>testAdd
empty add: 5.
self assert: (empty includes: 5).
SetTestCase>>testOccurrences
self assert: (empty occurrenceOf: 0) = 0.
self assert: (full occurrencesOf: 5) = 1.
full add: 5.
self assert: (full occurrencesOf: 5) = 1
SetTestCase>>testRemove
full remove: 5.
self assert: (full includes: 6).
self deny: (full includes: 5)
S.Ducasse 24
SUnit Core
S.Ducasse 25
TestSuite,TestCase and TestResult
a TestCase represents one test
SetTestCase>>testOccurenceOf
A testSuite is a group of tests
SUnit automatically builds a suite from the methods starting
with ‘test*’
TestResult represents a test execution results
S.Ducasse 26
Test Ressources
• ATest Resource is an object which is needed by a number
of Test Cases, and whose instantiation is so costly in terms
of time or resources that it becomes advantageous to only
initialize it once for a Test Suite run.
S.Ducasse 27
TestResources
A TestResources is invoked once before any test is
run.
Does not work if you have mutually exclusive
TestResources.
S.Ducasse 28
Refactorings and Tests
S.Ducasse 29
What is Refactoring?
• The process of changing a software system in such a way
that it does not alter the external behaviour of the code,
yet improves its internal structure [Fowl99a]
• A behaviour-preserving source-to-source program
transformation [Robe98a]
• A change to the system that leaves its behaviour
unchanged, but enhances some non-functional quality -
simplicity, flexibility, understandability, ... [Beck99a]
S.Ducasse 30
Typical Refactorings
Class Refactorings Method Refactorings Attribute Refactorings
add (sub)class to
hierarchy
add method to class add variable to class
rename class rename method rename variable
remove class remove method remove variable
push method down push variable down
push method up pull variable up
add parameter to method create accessors
move method to component abstract variable
extract code in new method
List of refactorings provided by the refactoring browser
S.Ducasse 31
Why Refactoring?
“Grow, don’t build software” Fred Brooks
•Some argue that good design does not lead to code needing
refactoring,
•But in reality
– Extremely difficult to get the design right the first time
– You cannot fully understand the problem domain
– You cannot understand user requirements, if he does!
– You cannot really plan how the system will evolve in five years
– Original design is often inadequate
– System becomes brittle, difficult to change
•Refactoring helps you to
– Manipulate code in a safe environment (behavior preserving)
– Create an environment a situation where evolution is possible
– Understand existing code
S.Ducasse 32
Refactor To Understand
Obvious
• Programs hard to read => Programs hard to understand =>
Programs hard to modify
• Programs with duplicated logic are hard to understand
• Programs with complex conditionals are hard to understand
• Programs hard to modify
Refactoring code creates and supports the understanding
• Renaming instance variables helps understanding methods
• Renaming methods helps understanding responsibility
• Iterations are necessary
The refactored code does not have to be used!
S.Ducasse 33
Test and Refactorings
• Tests can cover places where you have to manually change
the code
• Changing 3 by 33, nil but NewObject new
• Tests let you been more aggressive to change and improve
your code
S.Ducasse 34
Summary
If you are serious about programming
If you do not have time to lose
If you want to have synchronized documentations
Write unit tests...

Más contenido relacionado

Destacado (20)

01 intro
01 intro01 intro
01 intro
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
Stoop 436-strategy
Stoop 436-strategyStoop 436-strategy
Stoop 436-strategy
 
Stoop 305-reflective programming5
Stoop 305-reflective programming5Stoop 305-reflective programming5
Stoop 305-reflective programming5
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop 430-design patternsintro
Stoop 430-design patternsintroStoop 430-design patternsintro
Stoop 430-design patternsintro
 
Stoop 400 o-metaclassonly
Stoop 400 o-metaclassonlyStoop 400 o-metaclassonly
Stoop 400 o-metaclassonly
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Debugging VisualWorks
Debugging VisualWorksDebugging VisualWorks
Debugging VisualWorks
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
Stoop 437-proxy
Stoop 437-proxyStoop 437-proxy
Stoop 437-proxy
 
Stoop 422-naming idioms
Stoop 422-naming idiomsStoop 422-naming idioms
Stoop 422-naming idioms
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
06 debugging
06 debugging06 debugging
06 debugging
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 

Similar a Stoop 450-s unit

Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql ServerDavid P. Moore
 
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
 
Agile db testing_techniques
Agile db testing_techniquesAgile db testing_techniques
Agile db testing_techniquesTarik Essawi
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Thibaud Desodt
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development IntroductionNguyen Hai
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
Implementing test scripting Ian McDonald updated (minor changes) 26-04-2013
Implementing test scripting   Ian McDonald updated (minor changes) 26-04-2013Implementing test scripting   Ian McDonald updated (minor changes) 26-04-2013
Implementing test scripting Ian McDonald updated (minor changes) 26-04-2013Ian McDonald
 
Unit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGUnit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGegoodwintx
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Red Gate Software
 

Similar a Stoop 450-s unit (20)

Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
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
 
Tdd
TddTdd
Tdd
 
Agile db testing_techniques
Agile db testing_techniquesAgile db testing_techniques
Agile db testing_techniques
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ?
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Test Driven
Test DrivenTest Driven
Test Driven
 
Tdd
TddTdd
Tdd
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Implementing test scripting Ian McDonald updated (minor changes) 26-04-2013
Implementing test scripting   Ian McDonald updated (minor changes) 26-04-2013Implementing test scripting   Ian McDonald updated (minor changes) 26-04-2013
Implementing test scripting Ian McDonald updated (minor changes) 26-04-2013
 
Unit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGUnit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUG
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
Testing 101
Testing 101Testing 101
Testing 101
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014
 
Tdd guide
Tdd guideTdd guide
Tdd guide
 

Más de The World of Smalltalk (20)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
10 reflection
10 reflection10 reflection
10 reflection
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 

Stoop 450-s unit

  • 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Unit Testing
  • 2. S.Ducasse 2 Goal How can I trust that the changes did not destroy something? What is my confidence in the system? How do I write tests? What is unit testing? What is SUnit?
  • 3. S.Ducasse 3 Tests • Tests represent your trust in the system • Build them incrementally • Do not need to focus on everything • When a new bug shows up, write a test • Even better write them before the code • Act as your first client, better interface • Active documentation always in sync
  • 4. S.Ducasse 4 Testing Style “The style here is to write a few lines of code, then a test that should run, or even better, to write a test that won't run, then write the code that will make it run.” • write unit tests that thoroughly test a single class • write tests as you develop (even before you implement) • write tests for every new piece of functionality “Developers should spend 25-50% of their time developing tests.”
  • 5. S.Ducasse 5 But I can’t cover everything! • Sure! Nobody can but • When someone discovers a defect in your code, first write a test that demonstrates the defect. • Then debug until the test succeeds. “Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” Martin Fowler
  • 6. S.Ducasse 6 Unit Testing • Ensure that you get the specified behavior of the public interface of a class • Normally tests a single class • A test • Create a context, • Send a stimulus, • Check the results
  • 7. S.Ducasse 7 SetTestCase Class: SetTestCase superclass:TestCase SetTestCase>>testAdd | empty | empty := Set new. “Context” empty add: 5. “Stimulus” self assert: (empty includes: 5). “Check” SetTestCase run: #testAdd
  • 8. S.Ducasse 8 In a subclass ofTestCase Each method starting with test* Represents a test Is automatically executed The results of the test are collected in a TestResult object
  • 9. S.Ducasse 9 Examples testExampleRunArray3 "this demonstrates that adjancent runs with equal attributes are merged. " | runArray | runArray := RunArray new. runArray addLast:TextEmphasis normal times: 5; addLast:TextEmphasis bold times: 5; addLast:TextEmphasis bold times: 5. self assert: (runArray runs size = 2).
  • 10. S.Ducasse 10 Failures and Errors • A failure is a failed assertion, i.e., an anticipated problem that you test. • An error is a condition you didn’t check for. SetTestCase>>removeElementNotInSet self should: [Set new remove: 1] raise: Error
  • 11. S.Ducasse 11 Good Tests • Repeatable • No human intervention • “self-described” • Change less often than the system • Tells a story
  • 12. S.Ducasse 12 _Unit Frameworks • _Unit is a simple “testing framework” that provides: • classes for writing Test Cases and Test Suites • methods for setting up and cleaning up test data (“fixtures”) • methods for making assertions • textual and graphical tools for running tests • _Unit distinguishes between failures and errors: • A failure is a failed assertion, i.e., an anticipated problem that you test. • An error is a condition you didn’t check for.
  • 14. S.Ducasse 14 JUnit • Junit (inspired by SUnit) is a simple “testing framework” that provides: • classes for writing Test Cases and Test Suites • methods for setting up and cleaning up test data (“fixtures”) • methods for making assertions • textual and graphical tools for running tests
  • 16. S.Ducasse 16 ATesting Scenario The framework calls the test methods that you define for your test cases.
  • 18. S.Ducasse 18 SetTestCase Class: SetTestCase superclass:TestCase SetTestCase>>testAddition | s | s := Set new. s add: 5; add: 3. self assert: s size = 2. s add: 5. self assert: s size = 2
  • 19. S.Ducasse 19 Testing Remove SetTestCase>>testAddition | s | s := Set new. s add: 6. self assert: s size = 1. s remove: 6. self assert: s size = 0. self should: [s remove: 1000] raise: Error. res := s remove: 5 ifAbsent: [33]. self assert: (res = 33)
  • 20. S.Ducasse 20 Duplicating the Context SetTestCase>>testOccurrences | empty | empty := Set new. self assert: (empty occurrencesOf: 0) = 0. empty add: 5; add:5. self assert: (empty occurrencesOf: 5) = 1
  • 21. S.Ducasse 21 setUp and TearDown • Executed before and after each test • setUp allows us to specify and reuse the context • tearDown to clean after.
  • 22. S.Ducasse 22 Example:Testing Set • Class: SetTestCase superclass:TestCase instance variable:‘empty full’ • SetTestCase>>setUp empty := Set new. full := Set with: 6 with: 5 • The setUp is the context in which each test is run.
  • 23. S.Ducasse 23 Tests… SetTestCase>>testAdd empty add: 5. self assert: (empty includes: 5). SetTestCase>>testOccurrences self assert: (empty occurrenceOf: 0) = 0. self assert: (full occurrencesOf: 5) = 1. full add: 5. self assert: (full occurrencesOf: 5) = 1 SetTestCase>>testRemove full remove: 5. self assert: (full includes: 6). self deny: (full includes: 5)
  • 25. S.Ducasse 25 TestSuite,TestCase and TestResult a TestCase represents one test SetTestCase>>testOccurenceOf A testSuite is a group of tests SUnit automatically builds a suite from the methods starting with ‘test*’ TestResult represents a test execution results
  • 26. S.Ducasse 26 Test Ressources • ATest Resource is an object which is needed by a number of Test Cases, and whose instantiation is so costly in terms of time or resources that it becomes advantageous to only initialize it once for a Test Suite run.
  • 27. S.Ducasse 27 TestResources A TestResources is invoked once before any test is run. Does not work if you have mutually exclusive TestResources.
  • 29. S.Ducasse 29 What is Refactoring? • The process of changing a software system in such a way that it does not alter the external behaviour of the code, yet improves its internal structure [Fowl99a] • A behaviour-preserving source-to-source program transformation [Robe98a] • A change to the system that leaves its behaviour unchanged, but enhances some non-functional quality - simplicity, flexibility, understandability, ... [Beck99a]
  • 30. S.Ducasse 30 Typical Refactorings Class Refactorings Method Refactorings Attribute Refactorings add (sub)class to hierarchy add method to class add variable to class rename class rename method rename variable remove class remove method remove variable push method down push variable down push method up pull variable up add parameter to method create accessors move method to component abstract variable extract code in new method List of refactorings provided by the refactoring browser
  • 31. S.Ducasse 31 Why Refactoring? “Grow, don’t build software” Fred Brooks •Some argue that good design does not lead to code needing refactoring, •But in reality – Extremely difficult to get the design right the first time – You cannot fully understand the problem domain – You cannot understand user requirements, if he does! – You cannot really plan how the system will evolve in five years – Original design is often inadequate – System becomes brittle, difficult to change •Refactoring helps you to – Manipulate code in a safe environment (behavior preserving) – Create an environment a situation where evolution is possible – Understand existing code
  • 32. S.Ducasse 32 Refactor To Understand Obvious • Programs hard to read => Programs hard to understand => Programs hard to modify • Programs with duplicated logic are hard to understand • Programs with complex conditionals are hard to understand • Programs hard to modify Refactoring code creates and supports the understanding • Renaming instance variables helps understanding methods • Renaming methods helps understanding responsibility • Iterations are necessary The refactored code does not have to be used!
  • 33. S.Ducasse 33 Test and Refactorings • Tests can cover places where you have to manually change the code • Changing 3 by 33, nil but NewObject new • Tests let you been more aggressive to change and improve your code
  • 34. S.Ducasse 34 Summary If you are serious about programming If you do not have time to lose If you want to have synchronized documentations Write unit tests...