Notes from pytest with eric
This is a collection of notes from pytest with eric.
Take a look here: https://pytest-with-eric.com/
Basic concepts
- Maximizing Quality - A Comprehensive Look at Testing in Software Development 1
- different kinds of tests
- How To Test Python Exception Handling Using Pytest Assert (A Simple Guide)
- assert is a great way to write a basic test suite
Running tests
- How To Run A Single Test In Pytest (Using CLI And Markers) - Basic usages
- How Pytest Fixtures Can Help You Write More Readable And Efficient Tests - Overview on fixtures
- What Are Pytest Fixture Scopes (How To Choose The Best Scope For Your Test) - Scopes of fixtures
- A Step-by-Step Guide To Using Pytest Fixtures With Arguments - Parametrize
- How to Auto-Request Pytest Fixtures Using Autouse - Autouse
- How To Access Test Details With Pytest Request Fixture - Special Request fixture
- Yield vs Return
- Use
return
when you want to send back one final result and stop the function. - Use
yield
when you want to produce a series of values, one at a time, without losing the function’s current state. This is common when working with large datasets or when the next value depends on the previous ones.
- Use
- Yield vs Return
- What is Setup and Teardown in Pytest (Importance of a Clean Test Environment) - Yield
- What Is Pytest Caplog (Everything You Need To Know) - Log
- Caplog has different log levels, use the more appropriate one
- How To Manage Temporary Files with Pytest tmp_path - Temp files
- Pytest will take care of temp files by itself, trying to be the more platform-agnostic ever
- How to Effortlessly Generate Unit Test Cases with Pytest Parameterized Tests - Unit test generated test cases
- Use range, parametrize and fixtures
- [[A Beginner’s Guide To
pytest_generate_tests
]] - Pytest generate tests- We can use parametrize instead of pytest_generate_tests
Mocking
- Common Mocking Problems & How To Avoid Them (+ Best Practices)
- when to use mocking
- Mocking vs Patching in short
- Mocking = create a fake version you can play with.
- Patching = temporarily cover up a real thing with a fake one during the test.
- Mocking Vs. Patching (A Quick Guide For Beginners)
- How To Mock In Pytest (A Comprehensive Guide)
- Mocking is used when you want to test the functionality as a system you need to test the real connections e.g. your API can connect to the database or external API.
- What Are Pytest Mock Assert Called Methods and How To Leverage Them
- Magic Mock vs Mock: one is special and it has all the capabilities of a regular Mock, but with the addition of most of the “magic” or “dunder” methods pre-created.
- If you need a mock object to emulate special methods or operations, using MagicMock is often more convenient than using a basic Mock.
- Magic Mock vs Mock: one is special and it has all the capabilities of a regular Mock, but with the addition of most of the “magic” or “dunder” methods pre-created.
- Monkeypatch with 2 Code Examples
- modify a code snippet temporarily “on-the-fly”.
- IMPORTANT: it is temporary, once you are done it’s gone.
- def say_hello(): return “hello”
- say_hello = lambda: “goodnight”
- print(say_hello) → goodnight
- modify a code snippet temporarily “on-the-fly”.
- How To Test Raised Exceptions with Pytest MagicMock (Advanced Guide)
- How to test exceptions
- Again, Mock vs Magic Mock vs Patch
- How To Return Multiple Values From Pytest Mock (Practical Guide)
side_effect
: ifside_effect = [1, 2, 3]
, the mock will return1
the first time it’s called,2
the next time, and3
after that.
- Mock Celery
Configuration & Environment Variables
- How To Overwrite Dynaconf Variables For Testing In Python
- solve the problem of switching .env files in dev and prod, particularly useful!
- Pytest Conftest With Best Practices And Real Examples
- nothing so relevant
- [[What Is
pytest.ini
And How To Save Time Using Pytest Config]]- set log level, input output log, required plugins
- see also https://pytest-with-eric.com/pytest-best-practices/pytest-logging/
- Pytest Config Files - A Practical Guide To Good Config Management
- pytest vs pyproject vs setup.cfg vs tox.ini
- How To Ignore Warnings In Pytest (With Examples)
- just use the appropriate flag
- you can always filter them
- 3 Simple Ways To Ignore Test Directories in Pytest
- CLI, pytest.ini and marker (xskip, xfail…)
- How To Use Pytest With Command Line Options (Easy To Follow Guide)
- you can define custom CLI command to be executed with pytest (example: a pwd generator)
- 4 Proven Ways To Define Pytest PythonPath and Avoid Module Import Errors
- nothing so relevant
- 3 Simple Ways To Define Environment Variables In Pytest
- env_vars in test
- python-dotenv or pytest-dotenv
- pytest-env
Markers
Ultimate Guide To Pytest Markers And Good Test Management
- common markers, what they are, the most common ones
Property Based Testing
How to Use Hypothesis and Pytest for Robust Property-Based Testing in Python
- a new way to generate test cases, based on properties / property-based
Async Testing
A Practical Guide To Async Testing With Pytest-Asyncio
- just use the proper fixture for async tasks
FastAPI // TODO
-
https://pytest-with-eric.com/api-testing/pytest-api-testing-1/
-
https://pytest-with-eric.com/api-testing/pytest-api-testing-2/
-
https://pytest-with-eric.com/pytest-advanced/pytest-fastapi-testing/
-
Django Testing
https://pytest-with-eric.com/pytest-advanced/pytest-django-restapi-testing/
- a practical hands-on, not much to say
External/3rd Party API Call Testing
- How To Write Tests For External (3rd Party) API Calls with Pytest
- different ways to test API calls (examples: sandbox)
- Python REST API Unit Testing for External APIs
- test external API using mocking and patches
Database Testing
How To Test Database Transactions With Pytest And SQLModel
- we can use pytest for manual creating objects and test with sqalchemy if it’s what we need
Web Automation Testing
- How To Use Pytest With Selenium For Web Automation Testing
- basically browse the page throught selenium webdriver and look for specific elements if they contains specific values
- Test Automation Made Easy with Pytest and Playwright
- PlayWright it’s slightly more complicate than Selenium but it’s newer
- How To Create Interactive Test Reports with Pytest and Allure
- it is possible to create test reports with Allure, a platform that could be setup in a machine and used to interact with pytest results
CI/CD Pipelines
- https://pytest-with-eric.com/integrations/pytest-github-actions/
- how to create a github actions and run pytest in a nutshell
- How To Test And Build Python Packages With Pytest, Tox And Poetry
- poetry dependencies management
Plugins and Integrations
- 8 Useful Pytest Plugins To Make Your Python Unit Tests Easier, Faster and Prettier
- pytest-cov
- code coverage reporting for Python projects.
- pytest-mock
- simple but powerful mocking functionality
- pytest-xdist
- enables parallel testing for Python projects
- pytest-timeout
- provides a simple way to set timeouts for your test functions
- pytest-asyncio
- test async code
- pytest-sugar
- enhances the output of Pytest’s test execution progress by providing a more detailed and visually appealing representation of test results
- pytest-html
- generates HTML reports for your test results
- pytest-profiling
- provides easy profiling of your code during test execution
- pytest-cov
- How To Create Custom HTML Test Reports With pytest-html
--html
option for beautiful reports > you can even customize title, colors and so on
- Parallel Testing Made Easy With pytest-xdist
- you can specify number of workers with -n
- A Simple Guide To Controlling Time in Pytest Using Freezegun
- freezegun: “run as date”, mocking a specific datetime
- How To Measure And Improve Code Efficiency with Pytest Benchmark (The Ultimate Guide)
- use pytest-benchmark plugin
- How To Avoid Hanging Tests Using Pytest Timeout (And Save Compute Resource)
- An Ultimate Guide To Using Pytest Skip Test And XFail - With Examples
- already covered
- Save Money On You CI/CD Pipelines Using Pytest Parallel (with Example)
- use pytest-xdist and specify number of workers with
-n auto
- use pytest-xdist and specify number of workers with
- A Comprehensive Guide to Pytest Approx for Accurate Numeric Testing
Coverage
- 3 Simple Ways To Omit Subfolders From Python Coverage Reports
- Configuring .coveragerc ⇒
[run] omit = */migrations/*
- Configuring pyproject.toml ⇒
[tool.coverage.run] omit = */migrations/*
- Using Environment Variables ⇒
export AVERAGE_OMIT="src/migrations/*,src/legacy_code/*"
- Configuring .coveragerc ⇒
- How To Measure And Improve Test Coverage With Poetry And Pytest
- use Pytest-Cov
- How To Generate Beautiful & Comprehensive Pytest Code Coverage Reports (With Example)
coverage html
BDD (Behavior-Driven Development)
TDD (Test-Driven Development)
- How To Practice Test-Driven Development In Python? (Deep Dive)
- see Clean Code Notes
Hooks
- Introduction To Pytest Hooks (A Practical Guide For Beginners)
- What Is The pytest_configure Hook (A Simple Guide)
-
pytest_sessionstart and pytest_sessionfinish (A Complete Guide)
Testing CLI Applications
- https://pytest-with-eric.com/pytest-advanced/pytest-argparse-typer/
- commands as usual
Misc
- https://pytest-with-eric.com/pytest-best-practices/pytest-logging/
- https://pytest-with-eric.com/pytest-best-practices/pytest-read-json/
- Define JSON in Test
- Read An External JSON File
- Provide JSON Data as a Fixture
- Define JSON Using Pytest Parameters
@pytest.mark.parametrize
, like a parameter - Provide JSON At Runtime
--input-json
Comparisons
- https://pytest-with-eric.com/comparisons/python-testing-frameworks/
- https://pytest-with-eric.com/comparisons/pytest-vs-unittest/#Recommendation-Unittest-vs-Pytest
- Unittest is a good choice for simple testing scenarios, PyTest is more powerful, flexible and extensible, making it a better choice for larger, more complex testing projects.