tg-me.com/pyproglib/6541
Last Update:
Используйте следующий промпт для генерации юнит-тестов с помощью pytest:
💬 Промпт:
Please generate comprehensive unit tests for the following Python function using pytest.
Requirements:
1. Carefully analyze the function’s functionality, inputs, and outputs.
2. Generate unit tests that cover standard use cases and edge cases.
3. Ensure that the tests are clear, easy to understand, and well-organized.
4. Challenge potential inaccuracies with false outcomes, testing against possible mistakes.
5. Use assertions that test both expected outcomes and exception handling.
[insert function code here]
Что вы получите?
▪️ Полный набор юнит-тестов, охватывающих все аспекты функции.
▪️ Применение лучших практик тестирования.
▪️ Обеспечение надежности кода и упрощение отладки.
✔️ Пример:
Предположим, у нас есть функция, которая вычисляет факториал числа:
def factorial(n: int) -> int:
if n == 0 or n == 1:
return 1
return n * factorial(n-1)
Промпт позволит получить набор юнит-тестов для этой функции:
import pytest
from your_module import factorial
def test_factorial_base_case():
"""Проверка, что факториал 0 и 1 возвращает 1."""
assert factorial(0) == 1
assert factorial(1) == 1
def test_factorial_positive_integers():
"""Проверка факториала для положительных целых чисел."""
assert factorial(2) == 2
assert factorial(3) == 6
assert factorial(4) == 24
assert factorial(5) == 120
def test_factorial_large_number():
"""Проверка факториала для большего числа."""
assert factorial(10) == 3628800
def test_factorial_negative():
"""Проверка, что функция выбрасывает исключение для отрицательных чисел."""
with pytest.raises(ValueError):
factorial(-1)
def test_factorial_edge_case():
"""Проверка факториала для граничного случая."""
assert factorial(6) == 720
Библиотека питониста #буст