
![]() |
@wtf | |
Pytest Python Testing Framework |
||
1
Replies
19
Views
1 Bookmarks
|
![]() |
@wtf | 8 August 25 |
Pytest is a mature, full-featured testing framework for Python. It makes writing simple and scalable test cases easy and intuitive. Why Use Pytest? Simple syntax for writing tests Powerful fixture system Auto-discovery of tests Supports unit, functional & integration testing Detailed failure reports and plugin support Installation bash pip install pytest Writing a Basic Test python def add(x, y): return x + y def test_add(): assert add(2, 3) == 5 Running Tests bash pytest test_file.py Using Fixtures python import pytest @pytest.fixture def sample_data(): return [1, 2, 3] def test_length(sample_data): assert len(sample_data) == 3 Parameterized Tests python import pytest @pytest.mark.parametrize(a, b, result, [(2, 3, 5), (1, 1, 2)]) def test_add(a, b, result): assert a + b == result Real-World Use Cases Unit testing for Python apps Testing APIs (with requests) TDD & CI/CD pipelines Testing Flask, Django, FastAPI apps Summary Ideal For: All levels of testing in Python projects Strength: Easy syntax + advanced features Bonus: Huge ecosystem of plugins (e.g. pytest-django, pytest-cov) |
||


