Boa tarde, acabo de finalizar o curso, mas me restaram algumas dúvidas. No processo de coverage do meu projeto, obtive alguns missings: O que me desperta dúvidas é que a Exception de addition.py foi coberta pelos testes:
addition.py
def addition(tuple):
"""addition
Args:
tuple (tuple)
Returns:
float: result of addition between tuples first and second element
"""
try:
result = float(tuple[0] + tuple[1])
return result
except Exception as err:
print(f'[ADDITION] Something is wrong: [{err}]')`
test_addition.py
from src.addition import addition
from .conftest import valid_numbers, invalid_numbers, invalid_first_number, invalid_second_number
import pytest
from pytest import mark
@mark.addition
def test_addition_valid_numbers(valid_numbers):
"""test_addition
"""
# When
result = addition(valid_numbers)
# Then
assert result == (valid_numbers[0] + valid_numbers[1])
@mark.addition
def test_addition_invalid_numbers(invalid_numbers):
"""test_addition
"""
# When
with pytest.raises(Exception):
result = addition(invalid_numbers)
# Then
assert result
@mark.addition
def test_addition_invalid_first_number(invalid_first_number):
# When
with pytest.raises(Exception):
result = addition(invalid_first_number)
# Then
assert result
@mark.addition
def test_addition_invalid_second_number(invalid_second_number):
# When
with pytest.raises(Exception):
result = addition(invalid_second_number)
# Then
assert result
Já sobre as Exceptions do meu código de multiplicação, a primeira Exception foi coberta, mas a segunda não:
multiplication.py
def multiplication(tuple):
"""multiplication
Args:
tuple (tuple)
Returns:
float: result of multiplication between tuples first and second element
"""
try:
result = float(tuple[0] * tuple[1])
return result
except TypeError as err:
print(
f'[MULTIPLICATION] Impossible multiplicate this values: [{err}]')
except Exception as err:
print(f'[MULTIPLICATION] Something is wrong: [{err}]')
test_multiplication.py
from src.multiplication import multiplication
from .conftest import valid_numbers, invalid_numbers, invalid_first_number, invalid_second_number
import pytest
from pytest import mark
@mark.multiplication
def test_multiplication_valid_numbers(valid_numbers):
"""test_multiplication
"""
# When
result = multiplication(valid_numbers)
# Then
assert result == (valid_numbers[0] * valid_numbers[1])
@mark.multiplication
def test_multiplication_invalid_numbers(invalid_numbers):
"""test_multiplication
"""
# When
with pytest.raises(Exception):
result = multiplication(invalid_numbers)
# Then
assert result
@mark.multiplication
def test_multiplication_invalid_first_number(invalid_first_number):
"""test_multiplication
"""
# When
with pytest.raises(Exception):
result = multiplication(invalid_numbers)
# Then
assert result
@mark.multiplication
def test_multiplication_invalid_second_number(invalid_second_number):
"""test_multiplication
"""
# When
with pytest.raises(Exception):
result = multiplication(invalid_numbers)
# Then
assert result
Entendo que eu poderia desprezar essas linhas de código no .coveragerc, mas a minha dúvida é exatamente essa: Em caso de Exceptions, é interessante que sejam cobertos por testes, ou faz mais sentido desprezá-las? E em caso de cobrir com testes as Exceptions, como posso fazer isso?
Vou deixar o link do meu projeto, caso seja necessário: https://github.com/macdjoao/tdd-pytest.git
Desde já, agradeço a atenção.