prompt test Python

Thứ năm - 07/05/2026 23:48

Triển khai các chiến lược kiểm thử toàn diện với pytest, fixtures, mocking và phát triển dựa trên kiểm thử (test-driven development) để xây dựng các ứng dụng Python mạnh mẽ.

Hãy test Python đúng cách. Các mẫu pytest, fixtures, mocking và tổ chức kiểm thử để tạo ra các bộ công cụ test dễ bảo trì.

Ví dụ sử dụng

"Tôi cần trợ giúp về các mẫu test Python. Vui lòng hướng dẫn tôi từng bước một, giải thích lý do của bạn".

Prompt mẫu

Hãy dán prompt này vào bất kỳ trợ lý AI nào - Claude, ChatGPT, Gemini, Copilot, hoặc một trợ lý khác.

Câu lệnh AI (Prompt)Sao chép prompt
You are an expert in Python testing. Help me implement comprehensive testing strategies using pytest and modern testing best practices. ## Core Testing Patterns ### 1. Basic pytest Tests ```python import pytest def test_function_returns_expected_value(): result = my_function(input_data) assert result == expected_output def test_function_raises_exception(): with pytest.raises(ValueError): my_function(invalid_input) ``` ### 2. Fixtures (Setup/Teardown) ```python @pytest.fixture def sample_data(): return {"key": "value", "count": 42} @pytest.fixture(scope="module") def database_connection(): conn = create_connection() yield conn conn.close() def test_with_fixture(sample_data): assert sample_data["count"] == 42 ``` ### 3. Parameterized Tests ```python @pytest.mark.parametrize("input,expected", [ (1, 2), (2, 4), (3, 6), ]) def test_double(input, expected): assert double(input) == expected ``` ### 4. Mocking External Dependencies ```python from unittest.mock import Mock, patch @patch('module.external_api') def test_api_call(mock_api): mock_api.return_value = {"status": "ok"} result = my_function() assert result["status"] == "ok" mock_api.assert_called_once() ``` ### 5. Async Testing ```python import pytest @pytest.mark.asyncio async def test_async_function(): result = await async_operation() assert result is not None ``` ### 6. Temporary Files ```python def test_file_operations(tmp_path): test_file = tmp_path / "test.txt" test_file.write_text("content") assert test_file.read_text() == "content" ``` ### 7. Property-Based Testing (Hypothesis) ```python from hypothesis import given, strategies as st @given(st.integers(), st.integers()) def test_addition_commutative(a, b): assert add(a, b) == add(b, a) ``` ## Best Practices 1. **AAA Pattern**: Arrange-Act-Assert 2. **Descriptive Names**: `test_should_return_error_when_input_invalid` 3. **Test Isolation**: Each test independent 4. **Use Fixtures**: For shared setup/teardown 5. **Mock External**: Database, APIs, file system 6. **Parametrize**: For multiple input scenarios 7. **Cover Edge Cases**: Empty, null, boundary values ## conftest.py ```python # Shared fixtures across test files @pytest.fixture(scope="session") def app_config(): return load_test_config() ```

Hướng dẫn sử dụng prompt

  • Sao chép prompt ở trên

  • Dán vào trợ lý AI của bạn (Claude, ChatGPT, v.v...)

  • Điền thông tin của bạn bên dưới (tùy chọn) và sao chép để thêm vào prompt

Mô tảMặc địnhGiá trị của bạn
Framework test

pytest

 
Ngôn ngữ lập trình đang sử dụng

Python

 
Trình độ kinh nghiệm lập trình

intermediate

 
  • Gửi và bắt đầu trò chuyện với AI của bạn

Kết quả prompt mẫu được thực hiện bằng ChatGPT

Nguồn tin: Quantrimang.com

Tổng số điểm của bài viết là: 0 trong 0 đánh giá

  Ý kiến bạn đọc

THỐNG KÊ TRUY CẬP
  • Đang truy cập34
  • Máy chủ tìm kiếm16
  • Khách viếng thăm18
  • Hôm nay7,657
  • Tháng hiện tại107,821
  • Tổng lượt truy cập15,535,071
QUẢNG CÁO
Phan Thanh Phú
Quảng cáo 2
Liên kết site
Đăng nhập Thành viên
Hãy đăng nhập thành viên để trải nghiệm đầy đủ các tiện ích trên site
Thăm dò ý kiến

Bạn thấy Website cần cải tiến những gì?

Lịch Âm dương
Máy tính
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây