SlideShare a Scribd company logo
Unit Testing in Python
Haim Michael
August 1st
, 2022
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
life
michae
l
Quality Assurance Automation
www.lifemichael.com
© 2008 Haim Michael 20150805
What is Unit Testing?
© 2008 Haim Michael 20150805
What is Unit Testing?
 Unit test is a way of testing a unit (the smallest piece of code
that can be logically isolated in a system). In most
programming languages, that is a function, a subroutine, a
method or a property.
© 2008 Haim Michael 20150805
The TDD Methodology
 Test Driven Development (TDD) is a software development
methodology that focuses on creating unit test cases before
developing the actual code.
© 2008 Haim Michael 20150805
The TDD Methodology
 TDD leads to repetitive work flow in which we write the test,
write the code to be tested, execute the test and repeat these
two steps till the test passes.
Image Credit: TutorialPoint.com
© 2008 Haim Michael 20150805
The pytest Framework
© 2008 Haim Michael 20150805
The pytest Framework
 The pytest framework assists us with the development of
small readable tests. In addition, we can also use this library
when developing complex functional tests.
https://pytest.org
© 2008 Haim Michael 20150805
Installing The pytest Framework
 We can easily install this framework using the pip utility.
pip install -U pytest
 We can easily check the version of the pytest library we have
just installed using the following command:
pytest --version
Upgrading to Newest Version
© 2008 Haim Michael 20150805
Using The pytest Framework
 We can easily use the pytest framework by executing the
utility. Doing so, all files of the form test_*.py or
_test.py in the current directory and its subdirectories will
be examined, and all tests (functions their names start with
test_) will be called.
pytest
© 2008 Haim Michael 20150805
Simple Code Sample
def total(num1: int, num2: int) -> int:
result = num1 + num2
return result
def multiply(num1: int, num2: int) -> int:
result = num1 * num2
return result
utils.py
© 2008 Haim Michael 20150805
Simple Code Sample
import utils
def test_total():
actual_value = utils.total(3, 5)
expected_value = 8
assert actual_value == expected_value
def test_multiply():
actual_value = utils.multiply(3, 5)
expected_value = 15
assert actual_value == expected_value
test_utils.py
© 2008 Haim Michael 20150805
Simple Code Sample
© 2008 Haim Michael 20150805
Grouping Multiple Tests in Class
 We can group our tests in a class. We just need to define
each and every function with a name that starts with test_.
 There is no need to define a class that extends a specific
class that already exists. We just need to make sure that the
name of our class starts with Test.
© 2008 Haim Michael 20150805
Simple Code Sample
class BankAccount:
def __init__(self,id,balance):
self.id = id
self.balance = balance
def deposit(self,sum):
self.balance += sum
def withdraw(self,sum):
self.balance -= sum
banking.py
© 2008 Haim Michael 20150805
Simple Code Sample
import banking
class TestBankAccount:
def test_deposit(self):
ob = banking.BankAccount(123123,1000)
ob.deposit(80)
ob.deposit(120)
ob.deposit(400)
assert ob.balance == 1600
def test_withdraw(self):
ob = banking.BankAccount(123123,1000)
ob.withdraw(80)
ob.withdraw(120)
ob.withdraw(400)
assert ob.balance == 400
test_banking.py
© 2008 Haim Michael 20150805
Simple Code Sample
© 2008 Haim Michael 20150805
Assert Exceptions Raising
 We can assert whether a specific function raised an exception
using the raises helper.
© 2008 Haim Michael 20150805
Simple Code Sample
class BankAccount:
def __init__(self, id, balance):
self.id = id
self.balance = balance
def deposit(self, sum):
self.balance += sum
def withdraw(self, sum):
if sum<self.balance:
self.balance -= sum
else:
raise BankException("balance cannot be negative")
class BankException(Exception):
def __init__(self, message):
super().__init__(self, message)
banking.py
© 2008 Haim Michael 20150805
Simple Code Sample
import banking
import pytest
class TestBankAccount:
def perform_withdraws(self):
ob = banking.BankAccount(123123, 100)
ob.withdraw(80)
ob.withdraw(10)
ob.withdraw(30)
def test_withdraw(self):
with pytest.raises(banking.BankException):
self.perform_withdraws()
test_banking.py
© 2008 Haim Michael 20150805
Simple Code Sample
© 2008 Haim Michael 20150805
The requests Library
© 2008 Haim Michael 20150805
The Requests Library
 Requests is a simple HTTP library for Python. It allows us to
send HTTP requests.
https://docs.python-requests.org
© 2008 Haim Michael 20150805
Installation of Requests
 We can easily install Requests by using the pip command:
pip install requests
© 2008 Haim Michael 20150805
The GET Request
 We can easily initiate a GET request by calling the
requests.get() method.
© 2008 Haim Michael 20150805
The GET Request
import requests
data = requests.get("https://api.coinbase.com/v2/currencies")
print(data.status_code)
print(data.content)
print(data.text)
print(data.json()) #dict object
print(data.headers) #dict object
© 2008 Haim Michael 20150805
The GET Request
© 2008 Haim Michael 20150805
Query String Parameters
 We can easily customize the GET request, and send query
string parameters in the URL, by passing over a reference for
a dict object that holds those parameters.
© 2008 Haim Michael 20150805
Query String Parameters
import requests
response = requests.get(
"https://api.github.com/search/repositories",
params={'q': 'requests+language:python'})
data = response.json()
print(data)
© 2008 Haim Michael 20150805
Request Headers
 We can easily customize the headers that are sent together
with the request by passing over a reference for a dict object
that holds the headers we want to customize. The names of
the headers are the keys, and the values of the headers are
the values of those keys.
© 2008 Haim Michael 20150805
Request Headers
import requests
response = requests.get(
"https://api.github.com/search/repositories",
params={'q': 'requests+language:python'},
headers={'sec-ch-ua-platform': 'macOS'})
data = response.json()
print(data)
© 2008 Haim Michael 20150805
Other HTTP Methods
 In addition to the get() method, The Requests library
provides us with other similar methods for each one of the
other HTTP methods.
© 2008 Haim Michael 20150805
Other HTTP Methods
import requests
response = requests.post('https://httpbin.org/post',
data={'key':'value'})
print(response.json())
response = requests.put('https://httpbin.org/put',
data={'key':'value'})
print(response.json())
response = requests.delete('https://httpbin.org/delete')
print(response.json())
© 2008 Haim Michael 20150805
Other HTTP Methods
© 2008 Haim Michael 20150805
Introspecting The Request
 When making the request, the Requests library prepares the
request before sending it.
 We can introspect the request by referring the request
attribute of the response object.
© 2008 Haim Michael 20150805
Introspecting The Request
import requests
response = requests.post('https://httpbin.org/post', data={'key':'value'})
print(response.request.headers['Content-Type'])
print(response.request.url)
print(response.request.body)
© 2008 Haim Michael 20150805
REStful Web Services Testing
 We can easily write unit tests for REStful web services using
the PyTest unit testing framework and the Requests library.
© 2008 Haim Michael 20150805
REStful Web Services Testing
import requests
def test_get_status_code_ok_from_httpbin():
response = requests.get(
'https://httpbin.org/get',
data={'key': 'value'})
assert response.status_code == 200
© 2008 Haim Michael 20150805
Best Practices
08/01/22 © Abelski eLearning 39
Readable Code
 The unit tests we develop should be updated in accordance
with the changes that take place in the code we test.
 Having readable code will eases the update of the unit tests
code, either by us or by others.
08/01/22 © Abelski eLearning 40
Deterministic Tests
 It would be better if we use deterministic data instead of
randomized generated one.
 Deterministic tests either pass in all executions or not. They
exhibit the same behavior every time they run.
08/01/22 © Abelski eLearning 41
Avoid Interdependencies
 We better have each test case with its own setup and
teardown mechanism in order to avoid interdependencies
between the tests.
08/01/22 © Abelski eLearning 42
Avoid Logic
 Writing unit tests with logical conditions, manual strings
concatenation and various calculations might increase the
risk for bugs in our unit tests. The tests should focus on the
expected result.
08/01/22 © Abelski eLearning 43
Implementing TDD
 When we develop our unit tests before we develop the code
the quality of our tests improves.
08/01/22 © Abelski eLearning 44
CI/CD Integration
 Integrating our unit tests into the CI/CD pipeline will allow us
to run them several times per day (automatically). Doing so
will improve the quality of our code.
08/01/22 © Abelski eLearning 45
Update Our Tests
 Maintaining and updating the tests periodically will improve
their quality.
© 2009 Haim Michael All Rights Reserved 46
Questions & Answers
Thanks for Your Time!
Haim Michael
haim.michael@lifemichael.com
+972+3+3726013 ext:700
+972+54+6655837 (whatsapp)
life
michael

More Related Content

PPTX
Python-for-Data-Analysis.pptx
PPTX
Unit Testing with Python
PDF
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
PPSX
Coding standard
PPTX
Introduction to JUnit
PPT
Path testing, data flow testing
PDF
Image analysis using python
PDF
Introduction to RapidMiner Studio V7
Python-for-Data-Analysis.pptx
Unit Testing with Python
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
Coding standard
Introduction to JUnit
Path testing, data flow testing
Image analysis using python
Introduction to RapidMiner Studio V7

What's hot (20)

PDF
Python programming : Classes objects
ODP
Python unit testing
PPTX
Unit Testing And Mocking
PPT
Python Dictionaries and Sets
PDF
Py.test
PDF
Modern Python Testing
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PDF
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
PPTX
Object oriented programming in python
PPTX
This keyword and final keyword
PDF
Page Object Model and Implementation in Selenium
PDF
Java Serialization
PDF
Java Collection framework
PDF
JUnit & Mockito, first steps
PDF
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
PPTX
Python OOPs
PDF
PDF
Python regular expressions
PDF
Python - gui programming (tkinter)
ODP
OOP java
Python programming : Classes objects
Python unit testing
Unit Testing And Mocking
Python Dictionaries and Sets
Py.test
Modern Python Testing
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Object oriented programming in python
This keyword and final keyword
Page Object Model and Implementation in Selenium
Java Serialization
Java Collection framework
JUnit & Mockito, first steps
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Python OOPs
Python regular expressions
Python - gui programming (tkinter)
OOP java
Ad

Similar to Unit Testing in Python (20)

PDF
Python Advanced – Building on the foundation
PDF
PresentationqwertyuiopasdfghUnittest.pdf
PDF
Effective testing with pytest
PDF
MT_01_unittest_python.pdf
PPTX
Upstate CSCI 540 Unit testing
PPTX
Unit testing and mocking in Python - PyCon 2018 - Kenya
PDF
Python - code quality and production monitoring
PPTX
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
PPTX
1.Python_Testing_Using_PyUnit_Pytest.pptx
PPTX
Write tests, please
PDF
DSR Testing (Part 1)
PPTX
Python: Object-Oriented Testing (Unit Testing)
PDF
Testing Django Applications
PDF
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
ODT
Testing in-python-and-pytest-framework
PDF
DSR Testing (Part 2)
PDF
Write unit test from scratch
PPTX
Standard Libraries in Python Programming
PDF
Robust Python Write Clean And Maintainable Code 1st Edition Patrick Viafore
PPTX
Python Programming Essentials - M39 - Unit Testing
Python Advanced – Building on the foundation
PresentationqwertyuiopasdfghUnittest.pdf
Effective testing with pytest
MT_01_unittest_python.pdf
Upstate CSCI 540 Unit testing
Unit testing and mocking in Python - PyCon 2018 - Kenya
Python - code quality and production monitoring
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
1.Python_Testing_Using_PyUnit_Pytest.pptx
Write tests, please
DSR Testing (Part 1)
Python: Object-Oriented Testing (Unit Testing)
Testing Django Applications
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
Testing in-python-and-pytest-framework
DSR Testing (Part 2)
Write unit test from scratch
Standard Libraries in Python Programming
Robust Python Write Clean And Maintainable Code 1st Edition Patrick Viafore
Python Programming Essentials - M39 - Unit Testing
Ad

More from Haim Michael (20)

PDF
Prompt Engineering Jump Start [Free Meetup]
PDF
IntelliJ Debugging Essentials for Java Developers
PDF
The Visitor Classic Design Pattern [Free Meetup]
PDF
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
PDF
Introduction to Pattern Matching in Java [Free Meetup]
PDF
Mastering The Collections in JavaScript [Free Meetup]
PDF
Beyond Java - Evolving to Scala and Kotlin
PDF
JavaScript Promises Simplified [Free Meetup]
PDF
Scala Jump Start [Free Online Meetup in English]
PDF
The MVVM Architecture in Java [Free Meetup]
PDF
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
PDF
Anti Patterns
PDF
Virtual Threads in Java
PDF
MongoDB Design Patterns
PDF
Introduction to SQL Injections
PDF
Record Classes in Java
PDF
Microservices Design Patterns
PDF
Structural Pattern Matching in Python
PDF
OOP Best Practices in JavaScript
PDF
Java Jump Start
Prompt Engineering Jump Start [Free Meetup]
IntelliJ Debugging Essentials for Java Developers
The Visitor Classic Design Pattern [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]
Beyond Java - Evolving to Scala and Kotlin
JavaScript Promises Simplified [Free Meetup]
Scala Jump Start [Free Online Meetup in English]
The MVVM Architecture in Java [Free Meetup]
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Anti Patterns
Virtual Threads in Java
MongoDB Design Patterns
Introduction to SQL Injections
Record Classes in Java
Microservices Design Patterns
Structural Pattern Matching in Python
OOP Best Practices in JavaScript
Java Jump Start

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
Spectroscopy.pptx food analysis technology
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25 Week I
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
sap open course for s4hana steps from ECC to s4
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf

Unit Testing in Python

  • 1. Unit Testing in Python Haim Michael August 1st , 2022 All logos, trade marks and brand names used in this presentation belong to the respective owners. life michae l Quality Assurance Automation www.lifemichael.com
  • 2. © 2008 Haim Michael 20150805 What is Unit Testing?
  • 3. © 2008 Haim Michael 20150805 What is Unit Testing?  Unit test is a way of testing a unit (the smallest piece of code that can be logically isolated in a system). In most programming languages, that is a function, a subroutine, a method or a property.
  • 4. © 2008 Haim Michael 20150805 The TDD Methodology  Test Driven Development (TDD) is a software development methodology that focuses on creating unit test cases before developing the actual code.
  • 5. © 2008 Haim Michael 20150805 The TDD Methodology  TDD leads to repetitive work flow in which we write the test, write the code to be tested, execute the test and repeat these two steps till the test passes. Image Credit: TutorialPoint.com
  • 6. © 2008 Haim Michael 20150805 The pytest Framework
  • 7. © 2008 Haim Michael 20150805 The pytest Framework  The pytest framework assists us with the development of small readable tests. In addition, we can also use this library when developing complex functional tests. https://pytest.org
  • 8. © 2008 Haim Michael 20150805 Installing The pytest Framework  We can easily install this framework using the pip utility. pip install -U pytest  We can easily check the version of the pytest library we have just installed using the following command: pytest --version Upgrading to Newest Version
  • 9. © 2008 Haim Michael 20150805 Using The pytest Framework  We can easily use the pytest framework by executing the utility. Doing so, all files of the form test_*.py or _test.py in the current directory and its subdirectories will be examined, and all tests (functions their names start with test_) will be called. pytest
  • 10. © 2008 Haim Michael 20150805 Simple Code Sample def total(num1: int, num2: int) -> int: result = num1 + num2 return result def multiply(num1: int, num2: int) -> int: result = num1 * num2 return result utils.py
  • 11. © 2008 Haim Michael 20150805 Simple Code Sample import utils def test_total(): actual_value = utils.total(3, 5) expected_value = 8 assert actual_value == expected_value def test_multiply(): actual_value = utils.multiply(3, 5) expected_value = 15 assert actual_value == expected_value test_utils.py
  • 12. © 2008 Haim Michael 20150805 Simple Code Sample
  • 13. © 2008 Haim Michael 20150805 Grouping Multiple Tests in Class  We can group our tests in a class. We just need to define each and every function with a name that starts with test_.  There is no need to define a class that extends a specific class that already exists. We just need to make sure that the name of our class starts with Test.
  • 14. © 2008 Haim Michael 20150805 Simple Code Sample class BankAccount: def __init__(self,id,balance): self.id = id self.balance = balance def deposit(self,sum): self.balance += sum def withdraw(self,sum): self.balance -= sum banking.py
  • 15. © 2008 Haim Michael 20150805 Simple Code Sample import banking class TestBankAccount: def test_deposit(self): ob = banking.BankAccount(123123,1000) ob.deposit(80) ob.deposit(120) ob.deposit(400) assert ob.balance == 1600 def test_withdraw(self): ob = banking.BankAccount(123123,1000) ob.withdraw(80) ob.withdraw(120) ob.withdraw(400) assert ob.balance == 400 test_banking.py
  • 16. © 2008 Haim Michael 20150805 Simple Code Sample
  • 17. © 2008 Haim Michael 20150805 Assert Exceptions Raising  We can assert whether a specific function raised an exception using the raises helper.
  • 18. © 2008 Haim Michael 20150805 Simple Code Sample class BankAccount: def __init__(self, id, balance): self.id = id self.balance = balance def deposit(self, sum): self.balance += sum def withdraw(self, sum): if sum<self.balance: self.balance -= sum else: raise BankException("balance cannot be negative") class BankException(Exception): def __init__(self, message): super().__init__(self, message) banking.py
  • 19. © 2008 Haim Michael 20150805 Simple Code Sample import banking import pytest class TestBankAccount: def perform_withdraws(self): ob = banking.BankAccount(123123, 100) ob.withdraw(80) ob.withdraw(10) ob.withdraw(30) def test_withdraw(self): with pytest.raises(banking.BankException): self.perform_withdraws() test_banking.py
  • 20. © 2008 Haim Michael 20150805 Simple Code Sample
  • 21. © 2008 Haim Michael 20150805 The requests Library
  • 22. © 2008 Haim Michael 20150805 The Requests Library  Requests is a simple HTTP library for Python. It allows us to send HTTP requests. https://docs.python-requests.org
  • 23. © 2008 Haim Michael 20150805 Installation of Requests  We can easily install Requests by using the pip command: pip install requests
  • 24. © 2008 Haim Michael 20150805 The GET Request  We can easily initiate a GET request by calling the requests.get() method.
  • 25. © 2008 Haim Michael 20150805 The GET Request import requests data = requests.get("https://api.coinbase.com/v2/currencies") print(data.status_code) print(data.content) print(data.text) print(data.json()) #dict object print(data.headers) #dict object
  • 26. © 2008 Haim Michael 20150805 The GET Request
  • 27. © 2008 Haim Michael 20150805 Query String Parameters  We can easily customize the GET request, and send query string parameters in the URL, by passing over a reference for a dict object that holds those parameters.
  • 28. © 2008 Haim Michael 20150805 Query String Parameters import requests response = requests.get( "https://api.github.com/search/repositories", params={'q': 'requests+language:python'}) data = response.json() print(data)
  • 29. © 2008 Haim Michael 20150805 Request Headers  We can easily customize the headers that are sent together with the request by passing over a reference for a dict object that holds the headers we want to customize. The names of the headers are the keys, and the values of the headers are the values of those keys.
  • 30. © 2008 Haim Michael 20150805 Request Headers import requests response = requests.get( "https://api.github.com/search/repositories", params={'q': 'requests+language:python'}, headers={'sec-ch-ua-platform': 'macOS'}) data = response.json() print(data)
  • 31. © 2008 Haim Michael 20150805 Other HTTP Methods  In addition to the get() method, The Requests library provides us with other similar methods for each one of the other HTTP methods.
  • 32. © 2008 Haim Michael 20150805 Other HTTP Methods import requests response = requests.post('https://httpbin.org/post', data={'key':'value'}) print(response.json()) response = requests.put('https://httpbin.org/put', data={'key':'value'}) print(response.json()) response = requests.delete('https://httpbin.org/delete') print(response.json())
  • 33. © 2008 Haim Michael 20150805 Other HTTP Methods
  • 34. © 2008 Haim Michael 20150805 Introspecting The Request  When making the request, the Requests library prepares the request before sending it.  We can introspect the request by referring the request attribute of the response object.
  • 35. © 2008 Haim Michael 20150805 Introspecting The Request import requests response = requests.post('https://httpbin.org/post', data={'key':'value'}) print(response.request.headers['Content-Type']) print(response.request.url) print(response.request.body)
  • 36. © 2008 Haim Michael 20150805 REStful Web Services Testing  We can easily write unit tests for REStful web services using the PyTest unit testing framework and the Requests library.
  • 37. © 2008 Haim Michael 20150805 REStful Web Services Testing import requests def test_get_status_code_ok_from_httpbin(): response = requests.get( 'https://httpbin.org/get', data={'key': 'value'}) assert response.status_code == 200
  • 38. © 2008 Haim Michael 20150805 Best Practices
  • 39. 08/01/22 © Abelski eLearning 39 Readable Code  The unit tests we develop should be updated in accordance with the changes that take place in the code we test.  Having readable code will eases the update of the unit tests code, either by us or by others.
  • 40. 08/01/22 © Abelski eLearning 40 Deterministic Tests  It would be better if we use deterministic data instead of randomized generated one.  Deterministic tests either pass in all executions or not. They exhibit the same behavior every time they run.
  • 41. 08/01/22 © Abelski eLearning 41 Avoid Interdependencies  We better have each test case with its own setup and teardown mechanism in order to avoid interdependencies between the tests.
  • 42. 08/01/22 © Abelski eLearning 42 Avoid Logic  Writing unit tests with logical conditions, manual strings concatenation and various calculations might increase the risk for bugs in our unit tests. The tests should focus on the expected result.
  • 43. 08/01/22 © Abelski eLearning 43 Implementing TDD  When we develop our unit tests before we develop the code the quality of our tests improves.
  • 44. 08/01/22 © Abelski eLearning 44 CI/CD Integration  Integrating our unit tests into the CI/CD pipeline will allow us to run them several times per day (automatically). Doing so will improve the quality of our code.
  • 45. 08/01/22 © Abelski eLearning 45 Update Our Tests  Maintaining and updating the tests periodically will improve their quality.
  • 46. © 2009 Haim Michael All Rights Reserved 46 Questions & Answers Thanks for Your Time! Haim Michael [email protected] +972+3+3726013 ext:700 +972+54+6655837 (whatsapp) life michael