153 lines
4.2 KiB
Python
153 lines
4.2 KiB
Python
import pytest
|
|
import os
|
|
import sys
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from functions.math_utils import *
|
|
import functions.math_utils as math_utils
|
|
import math
|
|
import importlib
|
|
from unittest import mock
|
|
|
|
class CustomError(Exception):
|
|
pass
|
|
|
|
def test_broken():
|
|
# simulating a broken or faulty test implementation that will cause the test to error
|
|
want_the_test_to_fail
|
|
|
|
def test_call_missing_function():
|
|
# Accessing a missing function attribute raises AttributeError
|
|
getattr(math_utils, "non_existent_function")()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"a, b, expected",
|
|
[
|
|
(2, 2, 4),
|
|
(1, 5, 6),
|
|
(3, 4, 7),
|
|
(3, 5, 8),
|
|
(3, 6, 9),
|
|
]
|
|
)
|
|
def test_addition_pass(a, b, expected):
|
|
# Has to pass
|
|
assert addition(a, b) == expected
|
|
|
|
@pytest.mark.parametrize(
|
|
"a, b, expected",
|
|
[
|
|
pytest.param(2, 2, 4, id="a=2,b=2,expected=4"),
|
|
pytest.param(1, 5, 6, id="a=1,b=5,expected=6"),
|
|
pytest.param(3, 4, 7, id="a=3,b=4,expected=7"),
|
|
]
|
|
)
|
|
def test_addition_pass_id(a, b, expected):
|
|
# Has to pass
|
|
assert addition(a, b) == expected
|
|
|
|
def test_addition_fail():
|
|
# Assertion failure: expected incorrect result
|
|
assert addition(2, 2) == 5
|
|
|
|
def test_division_zero():
|
|
# Will raise ZeroDivisionError if not handled in division
|
|
division(1, 0)
|
|
|
|
@pytest.mark.xfail(reason="Expected failure")
|
|
def test_multiply_xfail():
|
|
# Expected fail test (xfail): incorrect expected multiply result
|
|
assert multiply(2, 2) == 5
|
|
|
|
def test_runtime_error():
|
|
# Test raises an uncaught RuntimeError
|
|
raise RuntimeError("Forced runtime error")
|
|
|
|
def test_memory_error():
|
|
# Manually raise MemoryError to simulate out-of-memory condition
|
|
raise MemoryError("Simulated memory error")
|
|
|
|
def test_timeout_error():
|
|
# Manually raise TimeoutError simulating timeout conditions
|
|
raise TimeoutError("Simulated timeout error")
|
|
|
|
def test_recursion_error():
|
|
# Infinite recursion triggers RecursionError
|
|
def recursive():
|
|
return recursive()
|
|
recursive()
|
|
|
|
def test_floating_point_error():
|
|
# Manually raise FloatingPointError (rare in practice)
|
|
raise FloatingPointError("Simulated floating point error")
|
|
|
|
def test_floating_point_overflow():
|
|
# Exponential overflow triggers OverflowError
|
|
math.exp(1000)
|
|
|
|
def test_value_error():
|
|
# ValueError on invalid integer conversion
|
|
int("invalid")
|
|
|
|
def test_type_error():
|
|
# TypeError when passing wrong argument type to sum
|
|
sum(5)
|
|
|
|
def test_unhandled_exception():
|
|
# Raises generic unhandled Exception
|
|
raise Exception("Generic unhandled exception")
|
|
|
|
def test_custom_error():
|
|
# Raises user-defined CustomError exception
|
|
raise CustomError("Custom error simulation")
|
|
|
|
def test_stop_iteration_direct():
|
|
# Directly raise StopIteration exception
|
|
raise StopIteration()
|
|
|
|
def test_generator_exit_direct():
|
|
# Directly raise GeneratorExit exception
|
|
raise GeneratorExit()
|
|
|
|
def test_recursion_limit():
|
|
# Lower recursion limit to force RecursionError on deep recursion
|
|
original_limit = sys.getrecursionlimit()
|
|
sys.setrecursionlimit(50)
|
|
def recurse():
|
|
return recurse()
|
|
try:
|
|
recurse()
|
|
finally:
|
|
sys.setrecursionlimit(original_limit)
|
|
|
|
def test_malformed_code():
|
|
# SyntaxError when executing malformed Python code
|
|
exec("def bad(:\n pass")
|
|
|
|
def test_sys_exit():
|
|
# Simulate SystemExit via sys.exit
|
|
sys.exit(1)
|
|
|
|
def test_broken_function():
|
|
# Simulate broken function raising TypeError
|
|
def broken_func(*args, **kwargs):
|
|
raise TypeError("Broken function")
|
|
broken_func()
|
|
|
|
def test_import_error_patch():
|
|
# Patch import to simulate ImportError on specific module
|
|
original_import = __import__
|
|
def fake_import(name, *args, **kwargs):
|
|
if name == "fake_module":
|
|
raise ImportError("Simulated ImportError")
|
|
return original_import(name, *args, **kwargs)
|
|
import builtins
|
|
builtins.__import__, old_import = fake_import, builtins.__import__
|
|
try:
|
|
__import__("fake_module")
|
|
finally:
|
|
builtins.__import__ = old_import
|
|
|
|
def test_module_not_found_error():
|
|
# Raises ModuleNotFoundError (subclass of ImportError) for missing module
|
|
importlib.import_module("non_existent_module_xyz") |