SlideShare a Scribd company logo
Writing Unit Test from
Scratch
Using Pytest
Wen-Shih Chao a.k.a Yen3
Overview
● What is unit test
● PyUnit (unittest) and pytest
● Pytest basic usage
● Some useful pytest usage and plugin
● How to start writing your own unit test ?
The talk would uncover
● Mock (because I still learn how to use it XD)
● Test-Driven Development
● Integration Test
● How to become a test master XD
What it unit test ?
● A unit test is a program to test your target program
● A program is possible to have wrong behaviour and so does unit test.
DEFINITION: A unit test is a piece of a code (usually a method) that
invokes another piece of code and checks the correctness of some
assumptions after- ward. If the assumptions turn out to be wrong, the unit
test has failed. A unit is a method or function.
Example
Real World
Example
Real World
Example
Real World
Example
Prepare
Run
Check
(Assert)
Unittest library
● Unittest (PyUnit) - python standard library
○ xUnit style
○ No need to install
○ Mock support (python version >= 3.3)
● Pytest
○ Need to install
○ Pythonic style
○ Mock support (through plugin)
Why pytest ?
● You can write `assert XXX == YYY` rather than `self.assertZZZ(XXX, YYY)`
○ For example, if you want to compare two dictionaries x and y are equal.
■ PyUnit: `self.assertDictEqual(x, y)`
■ Pytest: `assert x == y`
● You can write fixture (pytest term) to replace `setUp()` and `tearDown()`
function. A test case could use multiple fixtures.
● Use function notation `@` to mark attribute of each test case.
● You can define manual command line option to control the test flow.
● Some useful plugins can reduce the pain to write unit tests. You can also
write/maintain plugins for your requirement.
xUnit
style
test
case
Pytest
style
test
case
Pytest basic
1. Write the test function
2. Run `pytest`
3. Check the result
Pytest folder structure
● A test folder contains
○ `__init__.py`: Please write pytest as a python module
○ `conftest.py` (optional): config and settings for the pytest module (Only valid in pytest)
○ `test_xxx.py`: Test cases
● You can make submodule for tests.
○ The submodule can use upper modules config
Real
world
example
Pytest - Fixture
● The fixture provides resources which test cases need.
● 2 types fixture
○ A normal fixture - Just return
○ A yield fixture - Declare a resource and free it after finished
● The fixture concept is like context manager
● A test case can use multiple fixtures.
● A fixture can rely on other fixture
Pytest fixture
● Normal fixture
Other fixture
Pytest fixture
● Yield fixture
Pytest - monkeypatch
● A simple approach for mock
● You can replace any method to your method
Example
● `check_worker_alive` would call `ping_worker` to get the ping result. For avoiding to access
the real internet, the test fakes a function that always return true value to test the main
function part of `check_worker_alive`
Pytest - mark
● Mark is like a tag. Before starting the test, you can check the mark to do
what you want to do.
Mark example
● In the example, when you mark some test as slow `@pytest.makr.slow` and you run pytest
without `--slow` option (e.g. `pytest`). The pytest would ignore the test
Pytest - parametrize test
● Parameterize the argument
Pytest plugin
● pytest-tornado/pytest-tornado-yen3: The plugin lets you to test coroutine
function as native function and provides some useful fixture.
● pytest-mock: wrapper for unittest.mock
● pytest-cov: Generate test coverage report
● pytest-pep8: Generate pep8 style checking report
● pytest-selenium: I have no experience about the plugin, maybe sw2 needs
it
Pytest-tornado/Pytest-tornado-yen3
● Pytest-tornado provides some marks to run your test as a coroutine
○ It would start a new ioloop and use `IOLoop.run_sync` to run each test case
● The last update of pytest-tornado is 2 years ago. I have some needs and
change for the plugin so I maintain a patched version for my project. You
can get more details from https://github.com/yen3/pytest-tornado
Pytest-cov
● Generate coverage
● Usage is simple `pytest --cov=<module_name>`. You can use `--cov`
multiple time
Coverage
Example
docker-compose -f docker-compose-test.yml exec test-server sh -c "cd /app && pytest
--cov=common --cov=api_server --cov=worker --slow"
Coverage
example
docker-compose -f docker-compose-test.yml exec test-server sh -c 
"cp -r /app/* /app_cov && cd /app_cov && pytest -sv --cov-report=html --cov=common
--cov=api_server --cov=worker --slow --pep8 | tee htmlcov/test.log"
Should I write unit test for my program ?
● The answer is probably. In actually, some program is hard to test.
● If your program is easy to test, you should try to write some tests for it.
● Writing unit test would cost your extra time. If you have plan to write unit
test, remember to request more develop time to your manager.
● Unit test is also a program. It’s possible to go wrong.
● Unit test can not find all bugs. But it can find many bugs if you write
tests well.
Should I write unit test for my program ?
● How to write unit tests for existing program ?
○ It’s up-on your situation. If the time schedule of the project/program has no enough time,
maybe you have to quit the idea.
○ If you have enough time, there are several possible ways.
■ Start from easy function (e.g. utility functions). You can feel how to write unit tests.
■ Start from function that are easy to be buggy. It’s usually hard to test. If you can
finish, maybe you can get rid of the buggy function (for a while)
■ Start from the interface.
● I take the policy for middleware-network. The policy is like to write integration
tests. It can make sure your program works after each modification.
My experience about unit test
● If you are a front-end developer, I am sorry that I have no experience
about it.
● If you are a back-end developer and use python, you could write test with
pytest.
● Consider writing unit test to replace testing function/module manually
● Run test in docker container is very important. It can avoid reset the
testing environment every time.
● Take the advantage of fixture to initial/release resource for test cases
My experience about unit test
● The textbook says writing unit test is easy. In fact, a lot of problems need
to be resolve.
○ It costs lots of effort to maintain a good unit test.
○ Resource problems
■ Interactive/Isolate with real world environment like database, file system … etc
■ Resource initialize and restore
● I also write tests for third-party packages (e.g. tornadis, sqlalchemy … etc)
○ learn how to use these packages and avoid the unexpected behaviour when it updates.
○ The type of test is also called learning test.
My experience about unit test
● Prepare test environment. The test environment must isolate the
production environment. The straightforward way is to use docker to
achieve the effect.
● If you have multiple service need to start, you can
○ Use docker-compose to start several containers
○ User docker with supervisor to start several services in a container
○ Mock all services
○ … etc. It’s upon your requirement and situation
Questions ?
Reference
● Software test concept
a. Software Testing - https://en.wikipedia.org/wiki/Software_testing#The_box_approach
b. The Art of unit testing with C# 2nd edition, ISBN: 9781617290893 (中譯: 單元測試的藝術 2/e,
ISBN: 9789864342471)
● Pytest
a. Pytest - https://docs.pytest.org/en/latest/
b. Pytest 還有他的快樂夥伴 - https://www.slideshare.net/excusemejoe/pytest-and-friends
c. Python Testing with pytest, ISBN: 9781680502404
● Practical test experience sharing
a. Clean code, ISBN: 9780132350884 (中譯: 無瑕的程式碼, ISBN: 9789862017050)
b. Code Complete 2/e, ISBN: 9780735619678

More Related Content

ODP
Python unit testing
PDF
TDD in Python With Pytest
PPT
N Unit Presentation
PPTX
TDD in Go with Ginkgo and Gomega
PDF
An Introduction to Unit Test Using NUnit
PPTX
Unit testing with NUnit
PPTX
Unit testing
PPS
JUnit Presentation
Python unit testing
TDD in Python With Pytest
N Unit Presentation
TDD in Go with Ginkgo and Gomega
An Introduction to Unit Test Using NUnit
Unit testing with NUnit
Unit testing
JUnit Presentation

What's hot (20)

PPTX
NUnit Features Presentation
PDF
Php unit (eng)
ODP
Automated testing in Python and beyond
 
PPTX
Testing with Junit4
PDF
Unit testing (eng)
PDF
Automated Testing for Embedded Software in C or C++
PDF
How and what to unit test
PPT
05 junit
PDF
Unit testing with Junit
PPTX
Introduction To J unit
PDF
Python Testing Fundamentals
PPTX
Unit Testing with JUnit4 by Ravikiran Janardhana
ODP
Advanced junit and mockito
PDF
Unit testing with JUnit
PPTX
Introduction to unit testing in python
PPT
Simple Unit Testing With Netbeans 6.1
PPT
PDF
Unit testing, principles
PPT
NUnit Features Presentation
Php unit (eng)
Automated testing in Python and beyond
 
Testing with Junit4
Unit testing (eng)
Automated Testing for Embedded Software in C or C++
How and what to unit test
05 junit
Unit testing with Junit
Introduction To J unit
Python Testing Fundamentals
Unit Testing with JUnit4 by Ravikiran Janardhana
Advanced junit and mockito
Unit testing with JUnit
Introduction to unit testing in python
Simple Unit Testing With Netbeans 6.1
Unit testing, principles
Ad

Similar to Write unit test from scratch (20)

PPTX
JUnit Test Case With Processminer modules.pptx
PDF
Test all the things! Automated testing with Drupal 8
ODP
Pyunit
PPTX
Project Onion unit test environment
PDF
Unit testing in Unity
PDF
Automation for developers
PDF
stackconf 2024 | Test like a ninja with Go by Ivan Presenti.pdf
PDF
UPC Plone Testing Talk
PDF
PresentationqwertyuiopasdfghUnittest.pdf
PDF
Writing Tests with the Unity Test Framework
ODP
Beginners - Get Started With Unit Testing in .NET
PPT
Test automation principles, terminologies and implementations
PDF
Why and how to develop OpenERP test scenarios (in python and using OERPScenar...
PDF
Pytest - testing tips and useful plugins
PDF
Test Driven Development with Sql Server
PDF
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
PDF
Test Automation
PDF
DIY in 5 Minutes: Testing Django App with Pytest
PDF
TDD done right - tests immutable to refactor
PPTX
Testing Django APIs
JUnit Test Case With Processminer modules.pptx
Test all the things! Automated testing with Drupal 8
Pyunit
Project Onion unit test environment
Unit testing in Unity
Automation for developers
stackconf 2024 | Test like a ninja with Go by Ivan Presenti.pdf
UPC Plone Testing Talk
PresentationqwertyuiopasdfghUnittest.pdf
Writing Tests with the Unity Test Framework
Beginners - Get Started With Unit Testing in .NET
Test automation principles, terminologies and implementations
Why and how to develop OpenERP test scenarios (in python and using OERPScenar...
Pytest - testing tips and useful plugins
Test Driven Development with Sql Server
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
Test Automation
DIY in 5 Minutes: Testing Django App with Pytest
TDD done right - tests immutable to refactor
Testing Django APIs
Ad

More from Wen-Shih Chao (6)

PDF
Programming with effects - Graham Hutton
PDF
近未來三人女子電音偶像團體 Perfume
PDF
Matrix Chain Scheduling Algorithm
PDF
Java Technicalities
PDF
漫談 Source Control Management
PDF
淺談排版系統 Typesetting System
Programming with effects - Graham Hutton
近未來三人女子電音偶像團體 Perfume
Matrix Chain Scheduling Algorithm
Java Technicalities
漫談 Source Control Management
淺談排版系統 Typesetting System

Recently uploaded (20)

PPTX
additive manufacturing of ss316l using mig welding
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Geodesy 1.pptx...............................................
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Sustainable Sites - Green Building Construction
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Artificial Intelligence
DOCX
573137875-Attendance-Management-System-original
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT
introduction to datamining and warehousing
additive manufacturing of ss316l using mig welding
Safety Seminar civil to be ensured for safe working.
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Geodesy 1.pptx...............................................
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Sustainable Sites - Green Building Construction
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Mechanical Engineering MATERIALS Selection
Artificial Intelligence
573137875-Attendance-Management-System-original
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
introduction to datamining and warehousing

Write unit test from scratch

  • 1. Writing Unit Test from Scratch Using Pytest Wen-Shih Chao a.k.a Yen3
  • 2. Overview ● What is unit test ● PyUnit (unittest) and pytest ● Pytest basic usage ● Some useful pytest usage and plugin ● How to start writing your own unit test ?
  • 3. The talk would uncover ● Mock (because I still learn how to use it XD) ● Test-Driven Development ● Integration Test ● How to become a test master XD
  • 4. What it unit test ? ● A unit test is a program to test your target program ● A program is possible to have wrong behaviour and so does unit test. DEFINITION: A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions after- ward. If the assumptions turn out to be wrong, the unit test has failed. A unit is a method or function.
  • 9. Unittest library ● Unittest (PyUnit) - python standard library ○ xUnit style ○ No need to install ○ Mock support (python version >= 3.3) ● Pytest ○ Need to install ○ Pythonic style ○ Mock support (through plugin)
  • 10. Why pytest ? ● You can write `assert XXX == YYY` rather than `self.assertZZZ(XXX, YYY)` ○ For example, if you want to compare two dictionaries x and y are equal. ■ PyUnit: `self.assertDictEqual(x, y)` ■ Pytest: `assert x == y` ● You can write fixture (pytest term) to replace `setUp()` and `tearDown()` function. A test case could use multiple fixtures. ● Use function notation `@` to mark attribute of each test case. ● You can define manual command line option to control the test flow. ● Some useful plugins can reduce the pain to write unit tests. You can also write/maintain plugins for your requirement.
  • 13. Pytest basic 1. Write the test function 2. Run `pytest` 3. Check the result
  • 14. Pytest folder structure ● A test folder contains ○ `__init__.py`: Please write pytest as a python module ○ `conftest.py` (optional): config and settings for the pytest module (Only valid in pytest) ○ `test_xxx.py`: Test cases ● You can make submodule for tests. ○ The submodule can use upper modules config
  • 16. Pytest - Fixture ● The fixture provides resources which test cases need. ● 2 types fixture ○ A normal fixture - Just return ○ A yield fixture - Declare a resource and free it after finished ● The fixture concept is like context manager ● A test case can use multiple fixtures. ● A fixture can rely on other fixture
  • 17. Pytest fixture ● Normal fixture Other fixture
  • 19. Pytest - monkeypatch ● A simple approach for mock ● You can replace any method to your method
  • 20. Example ● `check_worker_alive` would call `ping_worker` to get the ping result. For avoiding to access the real internet, the test fakes a function that always return true value to test the main function part of `check_worker_alive`
  • 21. Pytest - mark ● Mark is like a tag. Before starting the test, you can check the mark to do what you want to do.
  • 22. Mark example ● In the example, when you mark some test as slow `@pytest.makr.slow` and you run pytest without `--slow` option (e.g. `pytest`). The pytest would ignore the test
  • 23. Pytest - parametrize test ● Parameterize the argument
  • 24. Pytest plugin ● pytest-tornado/pytest-tornado-yen3: The plugin lets you to test coroutine function as native function and provides some useful fixture. ● pytest-mock: wrapper for unittest.mock ● pytest-cov: Generate test coverage report ● pytest-pep8: Generate pep8 style checking report ● pytest-selenium: I have no experience about the plugin, maybe sw2 needs it
  • 25. Pytest-tornado/Pytest-tornado-yen3 ● Pytest-tornado provides some marks to run your test as a coroutine ○ It would start a new ioloop and use `IOLoop.run_sync` to run each test case ● The last update of pytest-tornado is 2 years ago. I have some needs and change for the plugin so I maintain a patched version for my project. You can get more details from https://github.com/yen3/pytest-tornado
  • 26. Pytest-cov ● Generate coverage ● Usage is simple `pytest --cov=<module_name>`. You can use `--cov` multiple time
  • 27. Coverage Example docker-compose -f docker-compose-test.yml exec test-server sh -c "cd /app && pytest --cov=common --cov=api_server --cov=worker --slow"
  • 28. Coverage example docker-compose -f docker-compose-test.yml exec test-server sh -c "cp -r /app/* /app_cov && cd /app_cov && pytest -sv --cov-report=html --cov=common --cov=api_server --cov=worker --slow --pep8 | tee htmlcov/test.log"
  • 29. Should I write unit test for my program ? ● The answer is probably. In actually, some program is hard to test. ● If your program is easy to test, you should try to write some tests for it. ● Writing unit test would cost your extra time. If you have plan to write unit test, remember to request more develop time to your manager. ● Unit test is also a program. It’s possible to go wrong. ● Unit test can not find all bugs. But it can find many bugs if you write tests well.
  • 30. Should I write unit test for my program ? ● How to write unit tests for existing program ? ○ It’s up-on your situation. If the time schedule of the project/program has no enough time, maybe you have to quit the idea. ○ If you have enough time, there are several possible ways. ■ Start from easy function (e.g. utility functions). You can feel how to write unit tests. ■ Start from function that are easy to be buggy. It’s usually hard to test. If you can finish, maybe you can get rid of the buggy function (for a while) ■ Start from the interface. ● I take the policy for middleware-network. The policy is like to write integration tests. It can make sure your program works after each modification.
  • 31. My experience about unit test ● If you are a front-end developer, I am sorry that I have no experience about it. ● If you are a back-end developer and use python, you could write test with pytest. ● Consider writing unit test to replace testing function/module manually ● Run test in docker container is very important. It can avoid reset the testing environment every time. ● Take the advantage of fixture to initial/release resource for test cases
  • 32. My experience about unit test ● The textbook says writing unit test is easy. In fact, a lot of problems need to be resolve. ○ It costs lots of effort to maintain a good unit test. ○ Resource problems ■ Interactive/Isolate with real world environment like database, file system … etc ■ Resource initialize and restore ● I also write tests for third-party packages (e.g. tornadis, sqlalchemy … etc) ○ learn how to use these packages and avoid the unexpected behaviour when it updates. ○ The type of test is also called learning test.
  • 33. My experience about unit test ● Prepare test environment. The test environment must isolate the production environment. The straightforward way is to use docker to achieve the effect. ● If you have multiple service need to start, you can ○ Use docker-compose to start several containers ○ User docker with supervisor to start several services in a container ○ Mock all services ○ … etc. It’s upon your requirement and situation
  • 35. Reference ● Software test concept a. Software Testing - https://en.wikipedia.org/wiki/Software_testing#The_box_approach b. The Art of unit testing with C# 2nd edition, ISBN: 9781617290893 (中譯: 單元測試的藝術 2/e, ISBN: 9789864342471) ● Pytest a. Pytest - https://docs.pytest.org/en/latest/ b. Pytest 還有他的快樂夥伴 - https://www.slideshare.net/excusemejoe/pytest-and-friends c. Python Testing with pytest, ISBN: 9781680502404 ● Practical test experience sharing a. Clean code, ISBN: 9780132350884 (中譯: 無瑕的程式碼, ISBN: 9789862017050) b. Code Complete 2/e, ISBN: 9780735619678