# Arithmetic arranger test_module?

Hello,

I have the code that outputs the expected answer and format but when I ran it on replit it showed 7 tests failed and only 3 passed… I am not sure what’s wrong… Does anyone know what causes it failed?

Thank you.

My code:

``````import re

arranged_problems = ''
space = ' '
line = '-'
newline = '\n'
operands0 = list()
operands1 = list()
operators = list()
for problem in problems:

if len(problems) > 5:
return 'Error: Too many problems.'

if re.search("[*/]", problem):
return "Error: Operator must be '+' or '-'."

if re.search("[^\d\s+-]", problem):
return "Error: Number must only contain digits."

if len(problem.split()[0]) > 4 or len(problem.split()[2]) > 4:
return "Error: Numbers cannot be more than four digits."

operands0.append(problem.split()[0])
operators.append(problem.split()[1])
operands1.append(problem.split()[2])

# the 1st row
for operand0, operand1 in zip(operands0, operands1):
width = max(len(operand0), len(operand1))
if operand0 != operands0[-1]:
arranged_problems += 2*space + operand0.rjust(width) + 4*space
else:
arranged_problems += 2*space + operand0.rjust(width)

arranged_problems += newline

# the 2nd row
for operand0, operand1, operator in zip(operands0, operands1, operators):
# width_diff = max(len(operand0), len(operand1)) - len(operand1)
# arranged_problems = arranged_problems + operator + space + width_diff*space + operand1 + 4*space
width = max(len(operand0), len(operand1))
if operand0 != operands0[-1]:
arranged_problems = arranged_problems + operator + space + operand1.rjust(width) + 4*space
else:
arranged_problems = arranged_problems + operator + space + operand1.rjust(width)

arranged_problems += newline

# the 3rd row
for operand0, operand1 in zip(operands0, operands1):
width = max(len(operand0), len(operand1))
arranged_problems = arranged_problems + 2*line + width*line + 4*space

arranged_problems += newline

# Perform calculation and store in list sols
sols = list()
for operand0, operand1, operator in zip(operands0, operands1, operators):
if (operator == '+'):
sols.append(int(operand0) + int(operand1))
else:
sols.append(int(operand0) - int(operand1))

# the 4th row, solution row
for operand0, operand1, sol in zip(operands0, operands1, sols):
if len(str(sol)) > max(len(operand0), len(operand1)):
arranged_problems = arranged_problems + space + str(sol) + 4*space
else:
arranged_problems = arranged_problems + 2*space + str(sol) + 4*space

return arranged_problems
``````

Output from replit tests:

`````` python main.py -vv
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
================================ test session starts =================================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /opt/virtualenvs/python3/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items

test_module.py::test_template[test_two_problems_arrangement1] FAILED           [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED           [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED           [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED           [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED                   [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED                  [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED                     [ 70%]
test_module.py::test_template[test_only_digits] FAILED                         [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED         [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED        [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      123\n-    2    +  49\n------    -----    ' == '  3801      123\n-    2    +  49\n------    -----'
E             3801      123
E           -    2    +  49
E         - ------    -----
E         + ------    -----
E         ?                ++++

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\n+ 2- 9380\n---    ------    ' == '  1         1\n+ 2    - 9380\n---    ------'
E         -   1         1
E         ?    ----
E         +   1     1
E         - + 2    - 9380
E         ?    ----
E         + + 2- 9380
E         - ---    ------
E         + ---    ------
E         ?              ++++

test_module.py:77: AssertionError
``````

This (denoted by `^`) is telling you that you have 4 extra spaces after the dashes for the equals bar. It’s happening in the bit of code commented `the 3rd row`. It looks like you would have the same issue on your solutions row as well. The extra spaces would cause all the arrangement tests to fail, even if the others passed.

I believe the required error message for the failing digits test requires `Numbers` and not `Number`.

1 Like

Thank you very much for pointing out what was causing failing from test module.
I finally got through after fixing this extra spaces, which I had no idea with.
You have sharp eyes. I did not even notice that I missed ‘s’ for `Numbers` in one of those Error message.
Thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.