UnitTest missunderstanding in my project "Arithmetic arranger"

I have problem with UnitTest in my project.
But i can’t understand why this two specific errors show up in terminal:

  • FAIL: test_arrangement (test_module.UnitTests)
  • FAIL: test_solutions (test_module.UnitTests)
    Maybe I need to check for these errors right in code?

My code:

Summary
def arithmetic_arranger(problems, solution=True):
    if len(problems)>4:
        return "Error: Too many problems."

    line1 = ''
    line2 = ''
    line3 = ''
    line4 = ''
    space = '    '

    for item in problems:
        num1, token, num2 = item.split()
        #print(num1, token, num2)
    
        if not token in ['-', '+'] :
            return "Error: Operator must be '+' or '-'."
            
        if num1.isdigit()==False or num2.isdigit()==False :
            return "Error: Numbers must only contain digits." 
        
        if len(num1)>4 or len(num2)>4 :
            return "Error: Numbers cannot be more than four digits."
        
        if token == '+':
            sol = int(num1) + int(num2)
        else :
            sol = int(num1) - int(num2)

        lngt = max(len(num1), len(num2)) + 2
        line1 += num1.rjust(lngt, " ") + space
        line2 += token+num2.rjust(lngt-1, " ") + space
        line3 += str().rjust(lngt, '-') + space
        line4 += str(sol).rjust(lngt, " ") + space

        result = line1 + '\n' + line2 + '\n' + line3

        if solution :
            result += '\n' + line4

    return result

Full details in console:

Summary
   32         1      45      123    
- 698    - 3801    + 43    +  49    
-----    ------    ----    -----    
 -666     -3800      88      172    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: '    [23 chars]  123    \n+ 855    -    2    + 43    +  49   [73 chars]    ' != '    [23 chars]  123\n+ 855    -    2    + 43    +  49\n-----[23 chars]----'
-     3      3801      45      123    
?                                 ----
+     3      3801      45      123
- + 855    -    2    + 43    +  49    
?                                 ----
+ + 855    -    2    + 43    +  49
- -----    ------    ----    -----    
?                                 -----
+ -----    ------    ----    ------   858      3799      88      172     : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.')
AssertionError: '   3[23 chars]  123    \n- 698    - 3801    + 43    +  49   [73 chars]    ' != '   3[23 chars]  123\n- 698    - 3801    + 43    +  49\n-----[57 chars] 172'
-    32         1      45      123    
?                                 ----
+    32         1      45      123
- - 698    - 3801    + 43    +  49    
?                                 ----
+ - 698    - 3801    + 43    +  49
- -----    ------    ----    -----    
?                                 ----
+ -----    ------    ----    -----
-  -666     -3800      88      172    ?                                 ----
+  -666     -3800      88      172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.007s

FAILED (failures=2)

In advance thank’s for helping! :white_heart:

Welcome to the the forums, @boorgoondee.

There are two issues. The easiest is that you are always printing solutions. The spec says setting the second argument to True will print them, not to have the second argument default to True. Second, you are adding extra spaces to your output:

which is what the dashes were trying to say but is hard to tell on a terminal that doesn’t color them correctly, so it’s easy to miss. The ‘-’ line is your output and the ‘+’ line is what is expected. You can see this line has four extra spaces.

1 Like

Thank you for answer!
I’m already running to fix :dash: