pytest

bởi microsoft

Các phương pháp hay nhất để viết và tổ chức kiểm thử với pytest bao gồm fixtures, parametrize và plugins.

npx skills add https://github.com/microsoft/debugpy --skill pytest

Skill: pytest

Best practices for writing and organizing tests with pytest including fixtures, parametrize, and plugins.

When to Use

Apply this skill when writing and organizing tests with pytest — fixtures, parametrize, markers, plugins, and test structure.

Test Organization

  • Place tests in a tests/ directory mirroring the source structure.
  • Name test files test_<module>.py and test functions test_<behavior>().
  • Group related tests in classes only when they share fixtures/setup.

Fixtures

  • Define fixtures at the narrowest scope needed (function > class > module > session).
  • Use conftest.py for shared fixtures; put it at the appropriate directory level.
  • Prefer factory fixtures over complex fixture inheritance.
  • Use yield fixtures for setup/teardown; prefer tmp_path over tempfile.

Parametrize

  • Use @pytest.mark.parametrize for data-driven tests with multiple inputs.
  • Give test IDs (ids=...) for readable test output.
  • Combine parametrize with fixtures for cross-product testing.

Assertions

  • Use plain assert statements — pytest rewrites them for clear failure messages.
  • Use pytest.raises(ExceptionType, match=...) for exception testing.
  • Use pytest.approx() for floating-point comparisons.

Plugins

  • Common plugins: pytest-cov, pytest-mock, pytest-asyncio, pytest-xdist, pytest-timeout.
  • Use pytest-mock's mocker fixture over raw unittest.mock.patch.

Pitfalls

  • Don't use session-scoped fixtures for mutable state.
  • Don't assert on implementation details — test observable behavior.
  • Avoid test interdependence; each test should be runnable in isolation.

Thêm skills từ microsoft

oss-growth
microsoft
Cá tính tăng trưởng OSS
official
winapp-ui-automation
microsoft
Kiểm tra và tương tác với giao diện ứng dụng Windows đang chạy từ dòng lệnh bằng UI Automation (UIA). Sử dụng khi một tác nhân AI hoặc nhà phát triển cần kiểm tra giao diện người dùng...
official
accessibility-aria-expert
microsoft
Phát hiện và sửa các vấn đề về khả năng tiếp cận trong giao diện web React/Fluent UI. Sử dụng khi xem xét mã để đảm bảo tương thích với trình đọc màn hình, sửa nhãn ARIA, đảm bảo…
official
generate-canvas-app
microsoft
[DEPRECATED — sử dụng canvas-app thay thế] Tạo một ứng dụng canvas Power Apps hoàn chỉnh.
official
django
microsoft
Các phương pháp tốt nhất cho phát triển web Django bao gồm models, views, templates và testing.
official
github-issue-creator
microsoft
Chuyển đổi ghi chú thô, nhật ký lỗi, ghi âm giọng nói hoặc ảnh chụp màn hình thành báo cáo vấn đề markdown sắc nét theo phong cách GitHub. Sử dụng khi người dùng dán thông tin lỗi, lỗi…
official
python-package-management
microsoft
Sử dụng uv để quản lý phụ thuộc và poethepoet để tự động hóa tác vụ.
official
runtime-validation
microsoft
Xác thực thời gian chạy cho các ứng dụng đã di chuyển — bao gồm chiến lược kiểm thử (giai đoạn lập kế hoạch) và thực thi kiểm thử (giai đoạn xác thực): xác minh khởi động,…
official