Summary of "Zrozum testy jednostkowe i asercje 🧐 Python 🐍 Pytest 🤖"
Summary of the Video
“Zrozum testy jednostkowe i asercje 🧐 Python 🐍 Pytest 🤖” by Mirosław Zalent
Main Ideas and Concepts
1. Introduction to Unit Testing
Unit testing involves writing automated tests to verify the correctness of small pieces of code such as functions, methods, or algorithms. Manual testing—checking code correctness by human inspection during development—is limited and error-prone. Automated unit tests run repeatedly and catch regressions caused by new code changes, improving software quality and reducing bugs before they reach users.
2. Benefits of Unit Testing
- Regression detection: Automated tests identify when new code breaks existing functionality.
- Quality assurance: Early bug detection leads to better software quality.
- Team collaboration: Unit tests lower onboarding costs for new team members by providing immediate feedback on code changes.
- Safe refactoring: Tests allow developers to confidently improve or rewrite code without fear of breaking it.
- Living documentation: Tests serve as executable documentation showing how code is expected to behave, including edge cases.
- Cleaner code: Writing tests encourages adherence to principles like single responsibility and modular design.
- Supports Test-Driven Development (TDD): Writing tests before code helps design better, cleaner functions.
3. Types of Testing Beyond Unit Testing
Other testing types include end-to-end tests, integration tests, system tests, API tests, load tests, smoke tests, etc. Unit testing is foundational and typically the starting point for learning software testing.
4. Choosing a Testing Framework in Python
- Python includes a built-in
unittestmodule. - The video prefers pytest due to its simplicity, less boilerplate, powerful assertion introspection, fixtures, and parameterization capabilities.
- Pytest helps beginners focus on test logic rather than framework syntax.
5. Setting Up Python and Pytest
- Check Python version using
python --versionorpython -V(capital V). - Download and install the latest Python from python.org (avoid the big yellow button; use the installer link below).
- Add Python executable to system PATH during installation.
-
Update
pippackage manager:bash python -m pip install --upgrade pip -
Install pytest:
bash pip install pytest -
Verify pytest installation:
bash pytest -V
6. Writing and Running Unit Tests with Pytest
- Test functions must start with the
test_prefix (e.g.,test_triangle). - Use Python’s
assertkeyword to make assertions about expected behavior. - A single test function can contain multiple assertions.
- If all assertions pass, the test is green; if any fail, the test is red.
-
Run tests via terminal command:
bash python -m pytest -
Use the
-v(verbose) flag with pytest to see detailed test names and results. - Tests are counted by functions, not assertions.
7. Example Code Under Test: Triangle Functions
is_triangle(a, b, c): Checks if three side lengths can form a valid triangle.- Validates sides are positive numbers.
- Checks triangle inequality (longest side < sum of other two).
- Includes protection against invalid data types and infinite/NaN values.
triangle_area(a, b, c): Calculates area using Heron’s formula if the triangle is valid; otherwise returns 0.- Writing tests helped discover edge cases and improve
is_trianglefunction robustness.
8. Manual Testing vs Automated Testing
- Manual testing (e.g., using
input()andprint()statements) is one-time and not repeatable. - Automated tests are repeatable, reliable, and integrated into the development workflow.
9. Project Structure for Testing
- Separate source code and tests into different folders:
src/for source files (e.g.,main.py)tests/for test files (e.g.,test_main.py)
- Import source modules in test files using Python import statements with aliases for clarity.
- Use a virtual environment (
venv) per project to manage dependencies and Python versions. - Configure pytest with a
pytest.inifile specifying:- Python path to source modules
- Test folder location
- Naming conventions for test files
- Verbose mode options
10. Running Tests in Visual Studio Code
- Use the Testing tab in VS Code to discover and run pytest tests automatically.
- Configure the Python interpreter correctly to use the desired Python version.
- Verbose mode can be enabled via pytest config to show detailed output in the UI.
11. Avoiding Conflicts Between Manual and Automated Tests
Wrap manual test code (e.g., input reading and print statements) inside:
if __name__ == "__main__":
# manual test code here
This prevents pytest from trying to execute manual input code during automated test runs, avoiding errors.
12. Additional Pytest Features (Mentioned but Not Covered in Depth)
- Fixtures for setup/teardown and sharing test data.
- Parameterization to run the same test with multiple data sets.
- Capturing output streams for testing print statements.
- Rich reporting and mocking capabilities.
13. Using Other IDEs
The project and tests also work in PyCharm with the same folder structure and configuration.
14. Call to Action
If viewers want a part two on pytest and unit testing, comment with the phrase:
“Why do you need cabbage?”
Thanks were given to patrons and the educational partner, WSKZ Higher Vocational School.
Methodology / Instructions for Writing Unit Tests in Python with Pytest
Setup
- Install Python and ensure the latest version is used in your IDE.
- Install
pytestvia pip. - Create a virtual environment for project isolation.
Project Structure
- Create a
src/folder for source code. - Create a
tests/folder for test code. - Separate test files from source files.
Writing Tests
- Name test files starting with
test_(e.g.,test_main.py). - Name test functions starting with
test_. - Import source modules in test files.
- Use
assertstatements to check expected outcomes. - Test normal cases and edge cases, including invalid inputs.
Running Tests
-
Run tests via terminal with:
bash python -m pytest -
Use
-vfor verbose output. - Use IDE test runners for convenience.
Best Practices
-
Avoid manual input/output in source modules or guard them with:
python if __name__ == "__main__": # manual test code here -
Write tests to cover boundary and error cases.
- Use pytest features like fixtures and parameterization for more complex scenarios (advanced usage).
- Keep tests small, focused, and readable.
Speakers / Sources Featured
- Mirosław Zalent — main speaker, Python programmer and educator presenting the tutorial.
- WSKZ Higher Vocational School — educational partner sponsoring the video.
- Historical reference: Heron of Alexandria — ancient mathematician and engineer, originator of Heron’s formula used in the example.
This summary captures the key lessons, concepts, and practical instructions presented in the video on understanding unit tests and assertions in Python using pytest.
Category
Educational
Share this summary
Is the summary off?
If you think the summary is inaccurate, you can reprocess it with the latest model.