Skip to content

Instantly share code, notes, and snippets.

@cheroliv
Last active January 5, 2024 22:38
Show Gist options
  • Save cheroliv/dc636d9f623bcb106a8774b3a73e97d5 to your computer and use it in GitHub Desktop.
Save cheroliv/dc636d9f623bcb106a8774b3a73e97d5 to your computer and use it in GitHub Desktop.
test factorial in python fact.py
import pytest
from unittest.mock import patch
from fact import run_computation, fact, Error
def test_run_computation_valid_input():
with patch('builtins.input', return_value='5'):
assert run_computation(5) == 120
def test_run_computation_invalid_input():
with patch('builtins.input', return_value='abc'):
assert run_computation('abc') is None # Change this based on how you handle invalid input
def test_run_computation_negative_input():
with patch('builtins.input', return_value='-5'):
assert run_computation(-5) is None # Change this based on how you handle negative input
def test_run_computation_exception():
with patch('builtins.input', side_effect=ValueError('Invalid input')):
assert run_computation('Invalid input') is None # Change this based on how you handle exceptions
def test_fact_positive():
assert fact(5) == 120
def test_fact_zero():
assert fact(0) == 1
def test_fact_negative():
with pytest.raises(Error, match='Invalid input'):
fact(-5) # Adjust this based on how you handle negative input
def test_fact_exception():
with pytest.raises(Error, match='Invalid input'):
fact('abc') # Adjust this based on how you handle exceptions
def test_fact_memoization():
with patch('fact.memo', {}):
assert fact(5) == 120
assert fact(5) == 120
assert fact(4) == 24
assert fact(4) == 24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment