際際滷

際際滷Share a Scribd company logo
Test driven development
The right way
Pawe Gasek
@PawelGlasek
Test driven development - the right way
Test driven development - the right way
The right way?
Czym TDD nie jest
Napisaniem test坦w wszystkich obiekt坦w, przed
implementacj funkcjonalnoci.
Czym TDD nie jest
Wszystkie testy przed implementacj
 Ci甜ko zaprojektowa odpowiedni design oraz zmienia
go w trakcie kodowania
 Ci甜ko przewidzie zmiany w obiektach klas ju甜 obecnych
w systemie
 Ci甜ko przewidzie wsp坦dziaanie komponent坦w
 Niekt坦re warunki brzegowe nigdy nie wystpuj  zbyt
du甜a ilo niepotrzebnych test坦w
 Czsto niepene pokrycie
Czym TDD nie jest
Sztuk pisania unit test坦w
Czym TDD nie jest
Just Unit tests
 Byskawicznie prowadzi do przemockowania
 Nie testuje wsp坦dziaania poszczeg坦lnych obiekt坦w
 Pomimo zmian w powizanych, ale zmockowanych
klasach testy przechodz
 Niekt坦re warunki brzegowe nigdy nie wystpuj  zbyt
du甜a ilo niepotrzebnych test坦w
 Brak motywacji do pisania test坦w
Czym TDD nie jest
Napisaniem test坦w po zakoczeniu pisania kodu
produkcyjnego.
Czym TDD nie jest
Testy po napisaniu kodu produkcyjnego
 Brak motywacji do pisania test坦w po fakcie
 Wiele przypadk坦w pozostaje nieprzetestowanych
 Niekt坦re warunki brzegowe nigdy nie wystpuj
 Brak poczucia sensu pisania test坦w w ten spos坦b
 Czste problemy z testowalnoci napisanego kodu
 Nuda
The right way
TDD
Agile'owa technika wytwarzania
programowania
Kent Beck
Test Driven
Development
By Example
Test driven development - the right way
Red  green  refactor
Zasady wujka Boba:
1. Nie wolno pisa kodu produkcyjnego, dop坦ki nie istnieje
failujcy test.
2. Nie wolno napisa wicej test坦w, ni甜 jest to niezbdne
do faila (niekompilujcy si kod r坦wnie甜 uznajemy jako
fail).
3. Nie wolno pisa wicej kodu produkcyjnego ni甜 jest to
niezbdne, by testy przechodziy.
Powiedz mi, a zapomn.
Poka甜 mi, a zapamitam.
Rozkad liczby na czynniki
pierwsze
TDD style
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
Prime factorization  TDD style
>> nose2 test.prime_factors
==================================================
ERROR: test_prime_factors (test.prime_factors.TestPrimeFactors)
----------------------------------------------------------------------
NameError: global name 'prime_factors' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
Prime factorization  TDD style
def prime_factors(number):
"""
Performs prime factorization.
:param number: Number to be factorized.
:type number: int
:rtype: list
"""
return []
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
Prime factorization  TDD style
>>nose2 test.prime_factors
F
================================================
FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors)
----------------------------------------------------------------------
AssertionError: Lists differ: [2] != []
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Prime factorization  TDD style
def prime_factors(number):
return []
def prime_factors(number):
factors = []
if number > 1:
factors.append(2)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
Prime factorization  TDD style
>>nose2 test.prime_factors
F
================================================
FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors)
----------------------------------------------------------------------
AssertionError: Lists differ: [3] != [2]
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Prime factorization  TDD style
def prime_factors(number):
factors = []
if number > 1:
factors.append(2)
return factors
def prime_factors(number):
factors = []
if number > 1:
factors.append(number)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
self.assertListEqual([2, 2], prime_factors(4))
Prime factorization  TDD style
>>nose2 test.prime_factors
F
================================================
FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors)
----------------------------------------------------------------------
AssertionError: Lists differ: [2, 2] != [4]
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Prime factorization  TDD style
def prime_factors(number):
factors = []
if number > 1:
factors.append(number)
return factors
def prime_factors(number):
factors = []
if number > 1:
if number % 2 == 0:
factors.append(2)
number /= 2
factors.append(number)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
F
================================================
FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors)
----------------------------------------------------------------------
AssertionError: Lists differ: [2] != [2, 1]
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Prime factorization  TDD style
def prime_factors(number):
factors = []
if number > 1:
if number % 2 == 0:
factors.append(2)
number /= 2
factors.append(number)
return factors
def prime_factors(number):
factors = []
if number > 1:
if number % 2 == 0:
factors.append(2)
number /= 2
if number > 1:
factors.append(number)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
def prime_factors(number):
factors = []
if number > 1:
if number % 2 == 0:
factors.append(2)
number /= 2
if number > 1:
factors.append(number)
return factors
def prime_factors(number):
factors = []
if number > 1:
if number % 2 == 0:
factors.append(2)
number /= 2
if number > 1:
factors.append(number)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
self.assertListEqual([2, 2], prime_factors(4))
self.assertListEqual([5], prime_factors(5))
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
self.assertListEqual([2, 2], prime_factors(4))
self.assertListEqual([5], prime_factors(5))
self.assertListEqual([2, 3], prime_factors(6))
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
self.assertListEqual([2, 2], prime_factors(4))
self.assertListEqual([5], prime_factors(5))
self.assertListEqual([2, 3], prime_factors(6))
self.assertListEqual([7], prime_factors(7))
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
self.assertListEqual([2, 2], prime_factors(4))
self.assertListEqual([5], prime_factors(5))
self.assertListEqual([2, 3], prime_factors(6))
self.assertListEqual([7], prime_factors(7))
self.assertListEqual([2, 2, 2], prime_factors(8))
Prime factorization  TDD style
>>nose2 test.prime_factors
F
================================================
FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors)
----------------------------------------------------------------------
AssertionError: Lists differ: [2, 2, 2] != [2, 4]
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Prime factorization  TDD style
def prime_factors(number):
factors = []
if number > 1:
if number % 2 == 0:
factors.append(2)
number /= 2
if number > 1:
factors.append(number)
return factors
def prime_factors(number):
factors = []
if number > 1:
while number % 2 == 0:
factors.append(2)
number /= 2
if number > 1:
factors.append(number)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
self.assertListEqual([2, 2], prime_factors(4))
self.assertListEqual([5], prime_factors(5))
self.assertListEqual([2, 3], prime_factors(6))
self.assertListEqual([7], prime_factors(7))
self.assertListEqual([2, 2, 2], prime_factors(8))
self.assertListEqual([3, 3], prime_factors(9))
Prime factorization  TDD style
>>nose2 test.prime_factors
F
================================================
FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors)
----------------------------------------------------------------------
AssertionError: Lists differ: [3, 3] != [9]
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Prime factorization  TDD style
def prime_factors(number):
factors = []
if number > 1:
while number % 2 == 0:
factors.append(2)
number /= 2
if number > 1:
factors.append(number)
return factors
def prime_factors(number):
factors = []
if number > 1:
while number % 2 == 0:
factors.append(2)
number /= 2
while number % 3 == 0:
factors.append(3)
number /= 3
if number > 1:
factors.append(number)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
def prime_factors(number):
factors = []
if number > 1:
while number % 2 == 0:
factors.append(2)
number /= 2
while number % 3 == 0:
factors.append(3)
number /= 3
if number > 1:
factors.append(number)
return factors
def prime_factors(number):
factors = []
divisor = 2
while number > 1:
while number % divisor == 0:
factors.append(divisor)
number /= divisor
divisor += 1
if number > 1:
factors.append(number)
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
def prime_factors(number):
factors = []
divisor = 2
while number > 1:
while number % divisor == 0:
factors.append(divisor)
number /= divisor
divisor += 1
if number > 1:
factors.append(number)
return factors
def prime_factors(number):
factors = []
divisor = 2
while number > 1:
while number % divisor == 0:
factors.append(divisor)
number /= divisor
divisor += 1
return factors
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Prime factorization  TDD style
class TestPrimeFactors(unittest.TestCase):
"""
Prime factors test suite.
"""
def test_prime_factors(self):
self.assertListEqual([], prime_factors(1))
self.assertListEqual([2], prime_factors(2))
self.assertListEqual([3], prime_factors(3))
self.assertListEqual([2, 2], prime_factors(4))
self.assertListEqual([5], prime_factors(5))
self.assertListEqual([2, 3], prime_factors(6))
self.assertListEqual([7], prime_factors(7))
self.assertListEqual([2, 2, 2], prime_factors(8))
self.assertListEqual([3, 3], prime_factors(9))
self.assertListEqual([2, 2, 3, 3, 5, 7 , 11, 13], prime_factors(2*2*3*3*5*7*11*13))
Prime factorization  TDD style
>>nose2 test.prime_factors
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Clean code that works
Zalety TDD
 Prostota iteracji designu
 Pokrycie testami
 Mo甜liwo refactoringu
 Zdecouplowany, testowalny kod
 Nietrywialny design
 Ograniczenie wykorzystania debugera
 Brak strachu przez zmianami w kodzie
Problemy z TDD
 Konieczno zmiany stylu mylenia
 Pocztkowo mao intuicyjna technika
 Pokusa pisania kodu produkcyjnego bez test坦w
Pytania
禽噛庄一顎逮
Ad

Recommended

PDF
Unit Testing - The Whys, Whens and Hows
atesgoral
PDF
"How keep normal blood pressure using TDD" By Roman Loparev
Ciklum Ukraine
PDF
C++ Unit Test with Google Testing Framework
Humberto Marchezi
PDF
Test driven development
christoforosnalmpantis
PDF
An introduction to Google test framework
Abner Chih Yi Huang
KEY
Unit Test Your Database
David Wheeler
PPT
xUnit Style Database Testing
Chris Oldwood
PPTX
Ch 6 randomization
Team-VLSI-ITMU
PPTX
Test Driven Development: Why I hate it; but secretly love it.
Tom Crinson
PDF
TDD and Related Techniques for Non Developers (2012)
Peter Kofler
PDF
Prime Factors Code Kata - Practicing TDD (2014)
Peter Kofler
PDF
Introducci坦n practica a Test-Driven Development (TDD)
Software Craftsmanship Alicante
PDF
Introducci坦n practica a TDD
rubocoptero
PPTX
Tieto tdd from-dreams_to_reality_s.narkevicius_v.pozdniakov_2013 (1)
Agile Lietuva
KEY
Testing My Patience
Adam Lowry
PDF
TDD CrashCourse Part2: TDD
David Rodenas
PPTX
Test-Driven Development
Meilan Ou
PDF
TDD and Simple Design Workshop - Session 1 - March 2019
Paulo Clavijo
PDF
How to complement TDD with static analysis
PVS-Studio
PDF
What we talk about when we talk about testing, or beyond red, green, refactor
noelrap
PPTX
TDD Training
Manuela Grindei
PDF
TDD for Coding Practices - by Zhifu Ge
ottawaruby
PPTX
10 ways to shoot yourself in the foot with tests - Shai Geva, PyConUS 2023
Shai Geva
PPTX
Pair programming and introduction to TDD
Arati Joshi
PDF
How Do We Teach TDD Keith Ray
C. Keith Ray
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
PDF
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
PDF
Sysinfo OST to PST Converter Infographic
SysInfo Tools
PDF
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed

More Related Content

Similar to Test driven development - the right way (18)

PPTX
Test Driven Development: Why I hate it; but secretly love it.
Tom Crinson
PDF
TDD and Related Techniques for Non Developers (2012)
Peter Kofler
PDF
Prime Factors Code Kata - Practicing TDD (2014)
Peter Kofler
PDF
Introducci坦n practica a Test-Driven Development (TDD)
Software Craftsmanship Alicante
PDF
Introducci坦n practica a TDD
rubocoptero
PPTX
Tieto tdd from-dreams_to_reality_s.narkevicius_v.pozdniakov_2013 (1)
Agile Lietuva
KEY
Testing My Patience
Adam Lowry
PDF
TDD CrashCourse Part2: TDD
David Rodenas
PPTX
Test-Driven Development
Meilan Ou
PDF
TDD and Simple Design Workshop - Session 1 - March 2019
Paulo Clavijo
PDF
How to complement TDD with static analysis
PVS-Studio
PDF
What we talk about when we talk about testing, or beyond red, green, refactor
noelrap
PPTX
TDD Training
Manuela Grindei
PDF
TDD for Coding Practices - by Zhifu Ge
ottawaruby
PPTX
10 ways to shoot yourself in the foot with tests - Shai Geva, PyConUS 2023
Shai Geva
PPTX
Pair programming and introduction to TDD
Arati Joshi
PDF
How Do We Teach TDD Keith Ray
C. Keith Ray
Test Driven Development: Why I hate it; but secretly love it.
Tom Crinson
TDD and Related Techniques for Non Developers (2012)
Peter Kofler
Prime Factors Code Kata - Practicing TDD (2014)
Peter Kofler
Introducci坦n practica a Test-Driven Development (TDD)
Software Craftsmanship Alicante
Introducci坦n practica a TDD
rubocoptero
Tieto tdd from-dreams_to_reality_s.narkevicius_v.pozdniakov_2013 (1)
Agile Lietuva
Testing My Patience
Adam Lowry
TDD CrashCourse Part2: TDD
David Rodenas
Test-Driven Development
Meilan Ou
TDD and Simple Design Workshop - Session 1 - March 2019
Paulo Clavijo
How to complement TDD with static analysis
PVS-Studio
What we talk about when we talk about testing, or beyond red, green, refactor
noelrap
TDD Training
Manuela Grindei
TDD for Coding Practices - by Zhifu Ge
ottawaruby
10 ways to shoot yourself in the foot with tests - Shai Geva, PyConUS 2023
Shai Geva
Pair programming and introduction to TDD
Arati Joshi
How Do We Teach TDD Keith Ray
C. Keith Ray

Recently uploaded (20)

PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
PDF
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
PDF
Sysinfo OST to PST Converter Infographic
SysInfo Tools
PDF
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
DOCX
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
PDF
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
PPTX
Test Case Design Techniques Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
PDF
University Campus Navigation for All - Peak of Data & AI
Safe Software
PPTX
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
PDF
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
DOCX
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
PDF
Which Hiring Management Tools Offer the Best ROI?
HireME
PDF
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
PDF
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
PPTX
AI for PV: Development and Governance for a Regulated Industry
Biologit
PDF
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
PPTX
declaration of Variables and constants.pptx
meemee7378
PPTX
Sap basis role in public cloud in s/4hana.pptx
htmlprogrammer987
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
Sysinfo OST to PST Converter Infographic
SysInfo Tools
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
Test Case Design Techniques Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
University Campus Navigation for All - Peak of Data & AI
Safe Software
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
Which Hiring Management Tools Offer the Best ROI?
HireME
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
AI for PV: Development and Governance for a Regulated Industry
Biologit
Simplify Task, Team, and Project Management with Orangescrum Work
Orangescrum
declaration of Variables and constants.pptx
meemee7378
Sap basis role in public cloud in s/4hana.pptx
htmlprogrammer987
Ad

Test driven development - the right way

  • 1. Test driven development The right way Pawe Gasek @PawelGlasek
  • 5. Czym TDD nie jest Napisaniem test坦w wszystkich obiekt坦w, przed implementacj funkcjonalnoci.
  • 6. Czym TDD nie jest Wszystkie testy przed implementacj Ci甜ko zaprojektowa odpowiedni design oraz zmienia go w trakcie kodowania Ci甜ko przewidzie zmiany w obiektach klas ju甜 obecnych w systemie Ci甜ko przewidzie wsp坦dziaanie komponent坦w Niekt坦re warunki brzegowe nigdy nie wystpuj zbyt du甜a ilo niepotrzebnych test坦w Czsto niepene pokrycie
  • 7. Czym TDD nie jest Sztuk pisania unit test坦w
  • 8. Czym TDD nie jest Just Unit tests Byskawicznie prowadzi do przemockowania Nie testuje wsp坦dziaania poszczeg坦lnych obiekt坦w Pomimo zmian w powizanych, ale zmockowanych klasach testy przechodz Niekt坦re warunki brzegowe nigdy nie wystpuj zbyt du甜a ilo niepotrzebnych test坦w Brak motywacji do pisania test坦w
  • 9. Czym TDD nie jest Napisaniem test坦w po zakoczeniu pisania kodu produkcyjnego.
  • 10. Czym TDD nie jest Testy po napisaniu kodu produkcyjnego Brak motywacji do pisania test坦w po fakcie Wiele przypadk坦w pozostaje nieprzetestowanych Niekt坦re warunki brzegowe nigdy nie wystpuj Brak poczucia sensu pisania test坦w w ten spos坦b Czste problemy z testowalnoci napisanego kodu Nuda
  • 16. Red green refactor Zasady wujka Boba: 1. Nie wolno pisa kodu produkcyjnego, dop坦ki nie istnieje failujcy test. 2. Nie wolno napisa wicej test坦w, ni甜 jest to niezbdne do faila (niekompilujcy si kod r坦wnie甜 uznajemy jako fail). 3. Nie wolno pisa wicej kodu produkcyjnego ni甜 jest to niezbdne, by testy przechodziy.
  • 17. Powiedz mi, a zapomn. Poka甜 mi, a zapamitam.
  • 18. Rozkad liczby na czynniki pierwsze
  • 20. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1))
  • 21. Prime factorization TDD style >> nose2 test.prime_factors ================================================== ERROR: test_prime_factors (test.prime_factors.TestPrimeFactors) ---------------------------------------------------------------------- NameError: global name 'prime_factors' is not defined ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1)
  • 22. Prime factorization TDD style def prime_factors(number): """ Performs prime factorization. :param number: Number to be factorized. :type number: int :rtype: list """ return []
  • 23. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 24. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2))
  • 25. Prime factorization TDD style >>nose2 test.prime_factors F ================================================ FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors) ---------------------------------------------------------------------- AssertionError: Lists differ: [2] != [] ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
  • 26. Prime factorization TDD style def prime_factors(number): return [] def prime_factors(number): factors = [] if number > 1: factors.append(2) return factors
  • 27. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 28. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3))
  • 29. Prime factorization TDD style >>nose2 test.prime_factors F ================================================ FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors) ---------------------------------------------------------------------- AssertionError: Lists differ: [3] != [2] ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
  • 30. Prime factorization TDD style def prime_factors(number): factors = [] if number > 1: factors.append(2) return factors def prime_factors(number): factors = [] if number > 1: factors.append(number) return factors
  • 31. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 32. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3)) self.assertListEqual([2, 2], prime_factors(4))
  • 33. Prime factorization TDD style >>nose2 test.prime_factors F ================================================ FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors) ---------------------------------------------------------------------- AssertionError: Lists differ: [2, 2] != [4] ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
  • 34. Prime factorization TDD style def prime_factors(number): factors = [] if number > 1: factors.append(number) return factors def prime_factors(number): factors = [] if number > 1: if number % 2 == 0: factors.append(2) number /= 2 factors.append(number) return factors
  • 35. Prime factorization TDD style >>nose2 test.prime_factors F ================================================ FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors) ---------------------------------------------------------------------- AssertionError: Lists differ: [2] != [2, 1] ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
  • 36. Prime factorization TDD style def prime_factors(number): factors = [] if number > 1: if number % 2 == 0: factors.append(2) number /= 2 factors.append(number) return factors def prime_factors(number): factors = [] if number > 1: if number % 2 == 0: factors.append(2) number /= 2 if number > 1: factors.append(number) return factors
  • 37. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 38. Prime factorization TDD style def prime_factors(number): factors = [] if number > 1: if number % 2 == 0: factors.append(2) number /= 2 if number > 1: factors.append(number) return factors def prime_factors(number): factors = [] if number > 1: if number % 2 == 0: factors.append(2) number /= 2 if number > 1: factors.append(number) return factors
  • 39. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 40. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3)) self.assertListEqual([2, 2], prime_factors(4)) self.assertListEqual([5], prime_factors(5))
  • 41. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 42. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3)) self.assertListEqual([2, 2], prime_factors(4)) self.assertListEqual([5], prime_factors(5)) self.assertListEqual([2, 3], prime_factors(6))
  • 43. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 44. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3)) self.assertListEqual([2, 2], prime_factors(4)) self.assertListEqual([5], prime_factors(5)) self.assertListEqual([2, 3], prime_factors(6)) self.assertListEqual([7], prime_factors(7))
  • 45. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 46. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3)) self.assertListEqual([2, 2], prime_factors(4)) self.assertListEqual([5], prime_factors(5)) self.assertListEqual([2, 3], prime_factors(6)) self.assertListEqual([7], prime_factors(7)) self.assertListEqual([2, 2, 2], prime_factors(8))
  • 47. Prime factorization TDD style >>nose2 test.prime_factors F ================================================ FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors) ---------------------------------------------------------------------- AssertionError: Lists differ: [2, 2, 2] != [2, 4] ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
  • 48. Prime factorization TDD style def prime_factors(number): factors = [] if number > 1: if number % 2 == 0: factors.append(2) number /= 2 if number > 1: factors.append(number) return factors def prime_factors(number): factors = [] if number > 1: while number % 2 == 0: factors.append(2) number /= 2 if number > 1: factors.append(number) return factors
  • 49. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 50. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3)) self.assertListEqual([2, 2], prime_factors(4)) self.assertListEqual([5], prime_factors(5)) self.assertListEqual([2, 3], prime_factors(6)) self.assertListEqual([7], prime_factors(7)) self.assertListEqual([2, 2, 2], prime_factors(8)) self.assertListEqual([3, 3], prime_factors(9))
  • 51. Prime factorization TDD style >>nose2 test.prime_factors F ================================================ FAIL: test_prime_factors (test.prime_factors.TestPrimeFactors) ---------------------------------------------------------------------- AssertionError: Lists differ: [3, 3] != [9] ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
  • 52. Prime factorization TDD style def prime_factors(number): factors = [] if number > 1: while number % 2 == 0: factors.append(2) number /= 2 if number > 1: factors.append(number) return factors def prime_factors(number): factors = [] if number > 1: while number % 2 == 0: factors.append(2) number /= 2 while number % 3 == 0: factors.append(3) number /= 3 if number > 1: factors.append(number) return factors
  • 53. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 54. Prime factorization TDD style def prime_factors(number): factors = [] if number > 1: while number % 2 == 0: factors.append(2) number /= 2 while number % 3 == 0: factors.append(3) number /= 3 if number > 1: factors.append(number) return factors def prime_factors(number): factors = [] divisor = 2 while number > 1: while number % divisor == 0: factors.append(divisor) number /= divisor divisor += 1 if number > 1: factors.append(number) return factors
  • 55. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 56. Prime factorization TDD style def prime_factors(number): factors = [] divisor = 2 while number > 1: while number % divisor == 0: factors.append(divisor) number /= divisor divisor += 1 if number > 1: factors.append(number) return factors def prime_factors(number): factors = [] divisor = 2 while number > 1: while number % divisor == 0: factors.append(divisor) number /= divisor divisor += 1 return factors
  • 57. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 58. Prime factorization TDD style class TestPrimeFactors(unittest.TestCase): """ Prime factors test suite. """ def test_prime_factors(self): self.assertListEqual([], prime_factors(1)) self.assertListEqual([2], prime_factors(2)) self.assertListEqual([3], prime_factors(3)) self.assertListEqual([2, 2], prime_factors(4)) self.assertListEqual([5], prime_factors(5)) self.assertListEqual([2, 3], prime_factors(6)) self.assertListEqual([7], prime_factors(7)) self.assertListEqual([2, 2, 2], prime_factors(8)) self.assertListEqual([3, 3], prime_factors(9)) self.assertListEqual([2, 2, 3, 3, 5, 7 , 11, 13], prime_factors(2*2*3*3*5*7*11*13))
  • 59. Prime factorization TDD style >>nose2 test.prime_factors . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 61. Zalety TDD Prostota iteracji designu Pokrycie testami Mo甜liwo refactoringu Zdecouplowany, testowalny kod Nietrywialny design Ograniczenie wykorzystania debugera Brak strachu przez zmianami w kodzie
  • 62. Problemy z TDD Konieczno zmiany stylu mylenia Pocztkowo mao intuicyjna technika Pokusa pisania kodu produkcyjnego bez test坦w