SlideShare a Scribd company logo
Quality of Life
through Unit Testing
PyCon Malaysia 2015 Workshop
WHAT we wish to accomplish
today?
1. Understand Unit Test in improving quality of life
2. Practice Unit Testing using python 3
3. Integrate Unit Testing as part of workflow
Materials
GitHub https://github.com/kiawin/pyconmy2015
git clone git@github.com:kiawin/pyconmy2015.git
vagrant up
vagrant ssh
Sian Lerk Lau
linkedin.com/in/sianlerk
sianlerk.lau@onapp.com | kiawin@gmail.com
Software Engineer
Volunteer
Educator-in-Exile
Quality of life through Unit Testing
https://twitter.com/OnApp - https://facebook.com/OnApp
Quality of life through Unit Testing
1. Understand Unit Test in improving quality of life
Quality of Life
is determined by
Quality of Code
Quality of Life
(of one)
is determined by
(the)
Quality of Code
(that one wrote)
Quality of Life
(of one)
is determined by
(the)
Quality of Code
(that someone wrote)
WHAT is unit?
A unit of work is a single logical functional
use case in the system that can be invoked by
some public interface (in most cases). A unit
of work can span a single method, a whole
class or multiple classes working together to
achieve one single logical purpose that can
be verified.
- The Art of Unit Test, Roy Osherove
WHAT is a good unit test
● Able to be fully automated
● Has full control over all the pieces running (Use mocks
or stubs to achieve this isolation when needed)
● Can be run in any order if part of many other tests
● Runs in memory (no DB or File access, for example)
● Consistently returns the same result (You always run
the same test, so no random numbers, for example.
save those for integration or range tests)
● Runs fast
● Tests a single logical concept in the system
● Readable
● Maintainable
● Trustworthy (when you see its result, you don’t need to
debug the code just to be sure)
2. Practice Unit Testing using python 3
vagrant up
vagrant provision
vagrant ssh
cd /vagrant/src
# Module / Guide
https://docs.python.org/3.4/library/unittest.html#module-
unittest
http://docs.python-guide.org/en/latest/writing/tests/
2. Practice Unit Testing Trick #1 Doc-less Reference
vagrant@archlinux:/vagrant$ python
Python 3.4.3 (default, Mar 25 2015, 17:13:50)
[GCC 4.9.2 20150304 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest
>>> dir(unittest)
['BaseTestSuite', 'FunctionTestCase', 'SkipTest', 'TestCase', 'TestLoader',
'TestProgram', 'TestResult', 'TestSuite', 'TextTestResult', 'TextTestRunner',
'_TextTestResult', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__path__', '__spec__', '__unittest',
'case', 'defaultTestLoader', 'expectedFailure', 'findTestCases',
'getTestCaseNames', 'installHandler', 'loader', 'main', 'makeSuite',
'registerResult', 'removeHandler', 'removeResult', 'result', 'runner', 'signals',
'skip', 'skipIf', 'skipUnless', 'suite', 'util']
2. Practice Unit Testing Trick #1 Doc-less Reference
vagrant@archlinux:/vagrant$ python
Python 3.4.3 (default, Mar 25 2015, 17:13:50)
[GCC 4.9.2 20150304 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest
>>> dir(unittest.TestCase)
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', '_addExpectedFailure', '_addSkip',
'_addUnexpectedSuccess', '_baseAssertEqual', '_classSetupFailed', '_deprecate',
'_diffThreshold', '_feedErrorsToResult', '_formatMessage',
'_getAssertEqualityFunc', '_truncateMessage', 'addCleanup', 'addTypeEqualityFunc',
'assertAlmostEqual', 'assertAlmostEquals', 'assertCountEqual',
'assertDictContainsSubset', 'assertDictEqual', 'assertEqual', 'assertEquals',
'assertFalse', 'assertGreater', 'assertGreaterEqual', 'assertIn', 'assertIs',
'assertIsInstance', 'assertIsNone', 'assertIsNot', 'assertIsNotNone',
'assertLess', 'assertLessEqual', 'assertListEqual', 'assertLogs',
'assertMultiLineEqual', 'assertNotAlmostEqual', 'assertNotAlmostEquals',
'assertNotEqual', 'assertNotEquals', 'assertNotIn', 'assertNotIsInstance',
'assertNotRegex', 'assertRaises', 'assertRaisesRegex', 'assertRaisesRegexp',
2. Practice Unit Testing Trick #1 Doc-less Reference
vagrant@archlinux:/vagrant$ python
Python 3.4.3 (default, Mar 25 2015, 17:13:50)
[GCC 4.9.2 20150304 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest
>>> import inspect
>>> inspect.getargspec(unittest.TestCase.assertEqual)
ArgSpec(args=['self', 'first', 'second', 'msg'], varargs=None, keywords=None,
defaults=(None,))
Ref: http://stackoverflow.com/questions/218616/getting-method-parameter-names-in-python
2. Practice Unit Testing Trick #2 Import Module
vagrant@archlinux:/vagrant/src$ python tests/calculator_tests.py
Traceback (most recent call last):
File "tests/calculator_tests.py", line 1, in <module>
import calculator
ImportError: No module named 'calculator'
# Solution - Set PYTHONPATH
PYTHONPATH=./ python tests/calculator_tests.py
Ref: http://stackoverflow.com/questions/5602559/where-is-the-python-path-set-when-i-dont-have-a-bash-profile
2. Practice Unit Testing Trick #3 Autorun Test
watch -n 2 "PYTHONPATH=./ python tests/calculator_tests.py"
Every 2.0s: PYTHONPATH=./ python tests/calculator_tests.py Thu Aug 20 13:02:39 2015
F
======================================================================
FAIL: test_add (__main__.TestCalculator)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/calculator_tests.py", line 14, in test_add
self.assertEqual(c.add(1,2),2,"Alamak?")
AssertionError: 3 != 2 : Alamak?
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
2. Practice Unit Testing Trick #3 Autorun Test
watch -n 2 "PYTHONPATH=./ python tests/calculator_tests.py"
Every 2.0s: PYTHONPATH=./ python tests/calculator_tests.py Thu Aug 20 13:11:44 2015
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
2. Practice Unit Testing Examples
1. simple calculator
2. simple assertion
3. use setUp
4. use tearDown
5. mock / patch
3. Integrate Unit Testing as part of workflow
● Validate your work - Integration
● Part of development flow - TDD
Our Way
● Unit tests as part of development process
● Continuous integration using Jenkins
● Behavioral tests to perform functional,
integration and regression tests on
applications.
● Performance tests based on defined
metrics
We’re hiring!
● System Admins as integral role in managing and
develop tools for our ecosystem
● Software Developers as engineering role in
creating bleeding edge applications for our ecosystem
Wonderful things we use
Python, Java, Ruby, Lua, Nginx, Wowza, Puppet, Vagrant,
Docker, Debian, Cucumber, RabbitMQ, MariaDB,
MongoDB, ELK, etc.
Q&A

More Related Content

PDF
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
PPT
RPG Program for Unit Testing RPG
PPT
Unit Testing RPG with JUnit
PPTX
Interpreter RPG to Java
PDF
JUnit 5 - The Next Generation
PDF
Modern Python Testing
ODT
Testing in-python-and-pytest-framework
PDF
What is new in JUnit5
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
RPG Program for Unit Testing RPG
Unit Testing RPG with JUnit
Interpreter RPG to Java
JUnit 5 - The Next Generation
Modern Python Testing
Testing in-python-and-pytest-framework
What is new in JUnit5

What's hot (20)

ODP
Python unit testing
PPTX
Introduction to JUnit testing in OpenDaylight
PDF
PDF
Automated Testing for Embedded Software in C or C++
PDF
Doing the Impossible
PDF
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
PPT
Google C++ Testing Framework in Visual Studio 2008
PDF
Unit testing on embedded target with C++Test
PPTX
Junit4&testng presentation
PPTX
Quickly Testing Qt Desktop Applications
PDF
DIY in 5 Minutes: Testing Django App with Pytest
PPTX
Renaissance of JUnit - Introduction to JUnit 5
PPT
Google mock for dummies
KEY
Unit Test Your Database
KEY
iOS Unit Testing
PPT
20111018 boost and gtest
ODP
Automated testing in Python and beyond
 
PDF
Qtp interview questions and answers
PPT
AUTOMATED TESTING USING PYTHON (ATE)
Python unit testing
Introduction to JUnit testing in OpenDaylight
Automated Testing for Embedded Software in C or C++
Doing the Impossible
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
Google C++ Testing Framework in Visual Studio 2008
Unit testing on embedded target with C++Test
Junit4&testng presentation
Quickly Testing Qt Desktop Applications
DIY in 5 Minutes: Testing Django App with Pytest
Renaissance of JUnit - Introduction to JUnit 5
Google mock for dummies
Unit Test Your Database
iOS Unit Testing
20111018 boost and gtest
Automated testing in Python and beyond
 
Qtp interview questions and answers
AUTOMATED TESTING USING PYTHON (ATE)
Ad

Similar to Quality of life through Unit Testing (20)

PDF
Write unit test from scratch
PPTX
Introduction to unit testing in python
PPTX
unittestinginpythonfor-PYDevelopers.pptx
PDF
PresentationqwertyuiopasdfghUnittest.pdf
PPTX
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
PPTX
1.Python_Testing_Using_PyUnit_Pytest.pptx
PDF
pytest로 파이썬 코드 테스트하기
PPTX
Upstate CSCI 540 Unit testing
PDF
Python and test
PPTX
Workshop: Unit Testing in Python
PDF
10 ways to shoot yourself in the foot with tests - Shai Geva, PyCon IL, 2024
PDF
Unit Testing in Software Development: Why It Matters and How to Do It Right
PDF
Unit Testing in Python
PDF
Effective testing with pytest
PPT
Python testing
PDF
Testing Django Applications
PDF
MT_01_unittest_python.pdf
PDF
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
PDF
Python Advanced – Building on the foundation
PPTX
2.Python_Testing_Using_PyUnit_PyTest.pptx
Write unit test from scratch
Introduction to unit testing in python
unittestinginpythonfor-PYDevelopers.pptx
PresentationqwertyuiopasdfghUnittest.pdf
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
1.Python_Testing_Using_PyUnit_Pytest.pptx
pytest로 파이썬 코드 테스트하기
Upstate CSCI 540 Unit testing
Python and test
Workshop: Unit Testing in Python
10 ways to shoot yourself in the foot with tests - Shai Geva, PyCon IL, 2024
Unit Testing in Software Development: Why It Matters and How to Do It Right
Unit Testing in Python
Effective testing with pytest
Python testing
Testing Django Applications
MT_01_unittest_python.pdf
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Python Advanced – Building on the foundation
2.Python_Testing_Using_PyUnit_PyTest.pptx
Ad

More from Sian Lerk Lau (7)

PDF
Solving performance issues in Django ORM
PDF
The journey of an (un)orthodox optimization
PDF
Velocity. Agility. Python. (Pycon APAC 2017)
PDF
DevOps - Myth or Real
PPTX
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
PDF
Python and you
PDF
Install Archlinux in 10 Steps (Sort of) :)
Solving performance issues in Django ORM
The journey of an (un)orthodox optimization
Velocity. Agility. Python. (Pycon APAC 2017)
DevOps - Myth or Real
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Python and you
Install Archlinux in 10 Steps (Sort of) :)

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
sap open course for s4hana steps from ECC to s4
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
Chapter 3 Spatial Domain Image Processing.pdf
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx

Quality of life through Unit Testing

  • 1. Quality of Life through Unit Testing PyCon Malaysia 2015 Workshop
  • 2. WHAT we wish to accomplish today? 1. Understand Unit Test in improving quality of life 2. Practice Unit Testing using python 3 3. Integrate Unit Testing as part of workflow
  • 3. Materials GitHub https://github.com/kiawin/pyconmy2015 git clone git@github.com:kiawin/pyconmy2015.git vagrant up vagrant ssh
  • 4. Sian Lerk Lau linkedin.com/in/sianlerk sianlerk.lau@onapp.com | kiawin@gmail.com Software Engineer Volunteer Educator-in-Exile
  • 8. 1. Understand Unit Test in improving quality of life
  • 9. Quality of Life is determined by Quality of Code
  • 10. Quality of Life (of one) is determined by (the) Quality of Code (that one wrote)
  • 11. Quality of Life (of one) is determined by (the) Quality of Code (that someone wrote)
  • 12. WHAT is unit? A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases). A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified. - The Art of Unit Test, Roy Osherove
  • 13. WHAT is a good unit test ● Able to be fully automated ● Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed) ● Can be run in any order if part of many other tests ● Runs in memory (no DB or File access, for example) ● Consistently returns the same result (You always run the same test, so no random numbers, for example. save those for integration or range tests) ● Runs fast ● Tests a single logical concept in the system ● Readable ● Maintainable ● Trustworthy (when you see its result, you don’t need to debug the code just to be sure)
  • 14. 2. Practice Unit Testing using python 3 vagrant up vagrant provision vagrant ssh cd /vagrant/src # Module / Guide https://docs.python.org/3.4/library/unittest.html#module- unittest http://docs.python-guide.org/en/latest/writing/tests/
  • 15. 2. Practice Unit Testing Trick #1 Doc-less Reference vagrant@archlinux:/vagrant$ python Python 3.4.3 (default, Mar 25 2015, 17:13:50) [GCC 4.9.2 20150304 (prerelease)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import unittest >>> dir(unittest) ['BaseTestSuite', 'FunctionTestCase', 'SkipTest', 'TestCase', 'TestLoader', 'TestProgram', 'TestResult', 'TestSuite', 'TextTestResult', 'TextTestRunner', '_TextTestResult', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__unittest', 'case', 'defaultTestLoader', 'expectedFailure', 'findTestCases', 'getTestCaseNames', 'installHandler', 'loader', 'main', 'makeSuite', 'registerResult', 'removeHandler', 'removeResult', 'result', 'runner', 'signals', 'skip', 'skipIf', 'skipUnless', 'suite', 'util']
  • 16. 2. Practice Unit Testing Trick #1 Doc-less Reference vagrant@archlinux:/vagrant$ python Python 3.4.3 (default, Mar 25 2015, 17:13:50) [GCC 4.9.2 20150304 (prerelease)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import unittest >>> dir(unittest.TestCase) ['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addExpectedFailure', '_addSkip', '_addUnexpectedSuccess', '_baseAssertEqual', '_classSetupFailed', '_deprecate', '_diffThreshold', '_feedErrorsToResult', '_formatMessage', '_getAssertEqualityFunc', '_truncateMessage', 'addCleanup', 'addTypeEqualityFunc', 'assertAlmostEqual', 'assertAlmostEquals', 'assertCountEqual', 'assertDictContainsSubset', 'assertDictEqual', 'assertEqual', 'assertEquals', 'assertFalse', 'assertGreater', 'assertGreaterEqual', 'assertIn', 'assertIs', 'assertIsInstance', 'assertIsNone', 'assertIsNot', 'assertIsNotNone', 'assertLess', 'assertLessEqual', 'assertListEqual', 'assertLogs', 'assertMultiLineEqual', 'assertNotAlmostEqual', 'assertNotAlmostEquals', 'assertNotEqual', 'assertNotEquals', 'assertNotIn', 'assertNotIsInstance', 'assertNotRegex', 'assertRaises', 'assertRaisesRegex', 'assertRaisesRegexp',
  • 17. 2. Practice Unit Testing Trick #1 Doc-less Reference vagrant@archlinux:/vagrant$ python Python 3.4.3 (default, Mar 25 2015, 17:13:50) [GCC 4.9.2 20150304 (prerelease)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import unittest >>> import inspect >>> inspect.getargspec(unittest.TestCase.assertEqual) ArgSpec(args=['self', 'first', 'second', 'msg'], varargs=None, keywords=None, defaults=(None,)) Ref: http://stackoverflow.com/questions/218616/getting-method-parameter-names-in-python
  • 18. 2. Practice Unit Testing Trick #2 Import Module vagrant@archlinux:/vagrant/src$ python tests/calculator_tests.py Traceback (most recent call last): File "tests/calculator_tests.py", line 1, in <module> import calculator ImportError: No module named 'calculator' # Solution - Set PYTHONPATH PYTHONPATH=./ python tests/calculator_tests.py Ref: http://stackoverflow.com/questions/5602559/where-is-the-python-path-set-when-i-dont-have-a-bash-profile
  • 19. 2. Practice Unit Testing Trick #3 Autorun Test watch -n 2 "PYTHONPATH=./ python tests/calculator_tests.py" Every 2.0s: PYTHONPATH=./ python tests/calculator_tests.py Thu Aug 20 13:02:39 2015 F ====================================================================== FAIL: test_add (__main__.TestCalculator) ---------------------------------------------------------------------- Traceback (most recent call last): File "tests/calculator_tests.py", line 14, in test_add self.assertEqual(c.add(1,2),2,"Alamak?") AssertionError: 3 != 2 : Alamak? ---------------------------------------------------------------------- Ran 1 test in 0.002s FAILED (failures=1)
  • 20. 2. Practice Unit Testing Trick #3 Autorun Test watch -n 2 "PYTHONPATH=./ python tests/calculator_tests.py" Every 2.0s: PYTHONPATH=./ python tests/calculator_tests.py Thu Aug 20 13:11:44 2015 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
  • 21. 2. Practice Unit Testing Examples 1. simple calculator 2. simple assertion 3. use setUp 4. use tearDown 5. mock / patch
  • 22. 3. Integrate Unit Testing as part of workflow ● Validate your work - Integration ● Part of development flow - TDD
  • 23. Our Way ● Unit tests as part of development process ● Continuous integration using Jenkins ● Behavioral tests to perform functional, integration and regression tests on applications. ● Performance tests based on defined metrics
  • 24. We’re hiring! ● System Admins as integral role in managing and develop tools for our ecosystem ● Software Developers as engineering role in creating bleeding edge applications for our ecosystem Wonderful things we use Python, Java, Ruby, Lua, Nginx, Wowza, Puppet, Vagrant, Docker, Debian, Cucumber, RabbitMQ, MariaDB, MongoDB, ELK, etc.
  • 25. Q&A