Hello Community,
I’m stuck at the arithmetic formatter project as part of the scientific Python Course.
As i can say the output of the formated arithmetic string looks “okay” (MyOutput) but some test failed. I don’t get the meaning of the Assert-Error, or rather don’t have enough experience with the pytest module to derive next steps from the assert. What does the [==] stands for in the assert statement? Obviously not, that the actual is the same value as the expected??
Can anyone help me with a hint?
Thank you and have a nice day!
arithmetic_arranger.py
import re
def arithmetic_arranger(problems, print_result = False):
##VALIDATION
#len check
if len(problems) <= 5:
pass
else:
return("Error: Too many problems.")
#collection
result = []
first = []
last = []
operator = []
#splitting expression with regex
for problem in problems:
problem_spl = (re.split("\s",problem))
#checking for operator [+-]
if re.match("[+-]",problem_spl[1]):
pass
else:
return("Error: Operator must be '+' or '-'.")
#checking for digits
if problem_spl[0].isdigit() and problem_spl[2].isdigit():
pass
else:
return("Error: Numbers must only contain digits.")
#checking for len of digits
if len(problem_spl[0]) <= 4 and len(problem_spl[2]) <= 4:
pass
else:
return("Error: Numbers cannot be more than four digits.")
#collecting results
result.append(str((eval(problem))))
#collecting first number
first.append(problem_spl[0])
#collecting last number
last.append(problem_spl[2])
#collecting operator
operator.append(problem_spl[1])
#ARRANGING
#calculate needed space
betw_space = 4
problem_space = []
i = 0
for problem in problems:
if int(result[i]) >= 0:
problem_space.append(max(len(first[i]),len(last[i]),len(result[i])) + 2)
else:
problem_space.append(max(len(first[i]),len(last[i]),len(result[i])) + 1)
i +=1
#formatting string for return
f = ""
s = ""
l = ""
r = ""
i = 0
for problem in problems:
fl = first[i].rjust(problem_space[i])
sl = operator[i] + last[i].rjust(problem_space[i]-1)
ll = "-"*problem_space[i]
rl = result[i].rjust(problem_space[i])
if i != len(problems):
f += fl + " "*betw_space
s += sl + " "*betw_space
l += ll + " "*betw_space
r += rl + " "*betw_space
else:
f += fl
s += sl
l += ll
r += rl
i +=1
if print_result:
strng = f + "\n" + s +"\n" + l + "\n" + r
else:
strng = f + "\n" + s +"\n" + l
return strng
main.py
# This entrypoint file to be used in development. Start by reading README.md
from pytest import main
from arithmetic_arranger import arithmetic_arranger
print(arithmetic_arranger(['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True))
# Run unit tests automatically
main()
test_module.py
import pytest
from arithmetic_arranger import arithmetic_arranger
test_cases = [
pytest.param(
[['3801 - 2', '123 + 49']],
' 3801 123\n'
'- 2 + 49\n'
'------ -----',
'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]',
id='test_two_problems_arrangement1'),
pytest.param(
[['1 + 2', '1 - 9380']],
' 1 1\n'
'+ 2 - 9380\n'
'--- ------',
'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]',
id='test_two_problems_arrangement2'),
pytest.param(
[['3 + 855', '3801 - 2', '45 + 43', '123 + 49']],
' 3 3801 45 123\n'
'+ 855 - 2 + 43 + 49\n'
'----- ------ ---- -----',
'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]',
id='test_four_problems_arrangement'),
pytest.param(
[['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']],
' 11 3801 1 123 1\n'
'+ 4 - 2999 + 2 + 49 - 9380\n'
'---- ------ --- ----- ------',
'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]',
id='test_five_problems_arrangement'),
pytest.param(
[['44 + 815', '909 - 2', '45 + 43', '123 + 49',
'888 + 40', '653 + 87']],
'Error: Too many problems.',
'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."',
id='test_too_many_problems'),
pytest.param(
[['3 / 855', '3801 - 2', '45 + 43', '123 + 49']],
"Error: Operator must be '+' or '-'.",
'''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''',
id='test_incorrect_operator'),
pytest.param(
[['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']],
'Error: Numbers cannot be more than four digits.',
'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."',
id='test_too_many_digits'),
pytest.param(
[['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49']],
'Error: Numbers must only contain digits.',
'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."',
id='test_only_digits'),
pytest.param(
[['3 + 855', '988 + 40'], True],
' 3 988\n'
'+ 855 + 40\n'
'----- -----\n'
' 858 1028',
'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.',
id='test_two_problems_with_solutions'),
pytest.param(
[['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True],
' 32 1 45 123 988\n'
'- 698 - 3801 + 43 + 49 + 40\n'
'----- ------ ---- ----- -----\n'
' -666 -3800 88 172 1028',
'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.',
id='test_five_problems_with_solutions'),
]
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
assert actual == expected_output, fail_message
test_module OUTPUT
` ============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items
test_module.py FFFF....FF [100%]
=================================== FAILURES ===================================
________________ test_template[test_two_problems_arrangement1] _________________
arguments = [['3801 - 2', '123 + 49']]
expected_output = ' 3801 123\n- 2 + 49\n------ -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E assert ' 3801 ... ----- ' == ' 3801 ...---- -----'
E - 3801 123
E + 3801 123
E ? ++++
E - - 2 + 49
E + - 2 + 49
E ? ++++
E - ------ -----...
E
E ...Full output truncated (3 lines hidden), use '-vv' to show
test_module.py:77: AssertionError
________________ test_template[test_two_problems_arrangement2] _________________
arguments = [['1 + 2', '1 - 9380']]
expected_output = ' 1 1\n+ 2 - 9380\n--- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]
E assert ' 1 ... ------ ' == ' 1 ...--- ------'
E - 1 1
E + 1 1
E ? ++++
E - + 2 - 9380
E + + 2 - 9380
E ? ++++
E - --- ------...
E
E ...Full output truncated (3 lines hidden), use '-vv' to show
test_module.py:77: AssertionError
________________ test_template[test_four_problems_arrangement] _________________
arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = ' 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E assert ' 3 3... ----- ' == ' 3 3...---- -----'
E - 3 3801 45 123
E + 3 3801 45 123
E ? ++++
E - + 855 - 2 + 43 + 49
E + + 855 - 2 + 43 + 49
E ? ++++
E - ----- ------ ---- -----...
E
E ...Full output truncated (3 lines hidden), use '-vv' to show
test_module.py:77: AssertionError
________________ test_template[test_five_problems_arrangement] _________________
arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = ' 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E assert ' 11 38... ------ ' == ' 11 38...--- ------'
E - 11 3801 1 123 1
E + 11 3801 1 123 1
E ? ++++
E - + 4 - 2999 + 2 + 49 - 9380
E + + 4 - 2999 + 2 + 49 - 9380
E ? ++++
E - ---- ------ --- ----- ------...
E
E ...Full output truncated (3 lines hidden), use '-vv' to show
test_module.py:77: AssertionError
_______________ test_template[test_two_problems_with_solutions] ________________
arguments = [['3 + 855', '988 + 40'], True]
expected_output = ' 3 988\n+ 855 + 40\n----- -----\n 858 1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.
E assert ' 3 ... 1028 ' == ' 3 9... 858 1028'
E - 3 988
E + 3 988
E ? + ++++
E - + 855 + 40
E + + 855 + 40
E ? + ++++
E - ----- -----...
E
E ...Full output truncated (6 lines hidden), use '-vv' to show
test_module.py:77: AssertionError
_______________ test_template[test_five_problems_with_solutions] _______________
arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = ' 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E assert ' 32 ... 1028 ' == ' 32 ... 172 1028'
E - 32 1 45 123 988
E + 32 1 45 123 988
E ? + ++++
E - - 698 - 3801 + 43 + 49 + 40
E + - 698 - 3801 + 43 + 49 + 40
E ? + ++++
E - ----- ------ ---- ----- -----...
E
E ...Full output truncated (6 lines hidden), use '-vv' to show
test_module.py:77: AssertionError
=========================== short test summary info ============================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - Assert...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Assert...
FAILED test_module.py::test_template[test_four_problems_arrangement] - Assert...
FAILED test_module.py::test_template[test_five_problems_arrangement] - Assert...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass...
========================= 6 failed, 4 passed in 0.18s ==========================
`
MyOutput
32 1 45 123 988
- 698 - 3801 + 43 + 49 + 40
----- ------ ---- ----- ------
-666 -3800 88 172 1028