Python 在我的代码上下文中,这个断言错误意味着什么?失败:测试安排(测试模块.单元测试)
我在做一道算术题 程序安排并返回问题,如控制台输出中所示。它是一个接受两个参数的函数:一个列表,其中每个项目都是一个加法或减法问题;另一个可选的第二个参数,如果为true,则在每个问题的破折号下显示每个答案 在我看来,该程序在test_模块文件中的6个测试中有1个失败 这是我运行程序时的控制台输出,错误如下所示:Python 在我的代码上下文中,这个断言错误意味着什么?失败:测试安排(测试模块.单元测试),python,Python,我在做一道算术题 程序安排并返回问题,如控制台输出中所示。它是一个接受两个参数的函数:一个列表,其中每个项目都是一个加法或减法问题;另一个可选的第二个参数,如果为true,则在每个问题的破折号下显示每个答案 在我看来,该程序在test_模块文件中的6个测试中有1个失败 这是我运行程序时的控制台输出,错误如下所示: python main.py 32 3801 50 123 12 + 698 - 2 + 50 - 49
python main.py
32 3801 50 123 12
+ 698 - 2 + 50 - 49 + 3600
----- ------ ---- ----- ------
730 3799 100 74 3612
F.....
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-4/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: ' [68 chars]- ------ ---- -----\n 858 3799 88 172' != ' [68 chars]- ------ ---- -----'
3 3801 45 123
+ 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -
+ ----- ------ ---- ------ 858 3799 88 172 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
----------------------------------------------------------------------
Ran 6 tests in 0.006s
FAILED (failures=1)
my main.py文件的内容:
# This entrypoint file to be used in development. Start by reading README.md
from arithmetic_arranger import arithmetic_arranger
from unittest import main
print(arithmetic_arranger(["32 + 698", "3801 - 2", "50 + 50", "123 - 49", "12 + 3600"], True))
# Run unit tests automatically
main(module='test_module', exit=False)
我的算术_arranger.py文件的内容:
def arithmetic_arranger(problems, results):
if len(problems) > 5:
return 'Error: Too many problems.'
top = []
bottom = []
dashes = []
spaces = ' '
answers = []
# ["32 + 698", "3801 - 2", "50 + 50", "123 - 49", "12 + 3600"]
for problem in problems:
first = ' ' + problem.split()[0]
second = problem.split(' ', 1)[1]
length_first = len(first)
length_second = len(second)
difference_first = length_first - length_second
difference_second = length_second - length_first
width_first = len(problem.split()[0])
width_second = len(problem.split()[2])
if width_first > 4 or width_second > 4:
return "Error: Numbers cannot be more than four digits."
if second[0] != '+' and second[0] != '-':
return "Error: Operator must be '+' or '-'."
if first.lstrip().isdigit() != True or second[2:].isdigit() != True:
return 'Error: Numbers must only contain digits.'
if difference_first > 0:
second = second[0] + ' ' * (difference_first + 1) + second[2:]
elif difference_second > 0:
first = ' ' * difference_second + first
# Optional condition to display answers under problems:
if results:
if second[0] == '+':
answer = int(first) + int(second[2:])
else:
answer = int(first) - int(second[2:])
answer_to_string = str(answer)
length_result = len(answer_to_string)
answer_to_string = ' ' + answer_to_string
length_maximum = max([len(first.lstrip()), len(second[1:].lstrip())])
if length_maximum > length_result:
answer_to_string = ' ' * (length_maximum - length_result) + answer_to_string
elif length_maximum < length_result:
answer_to_string = answer_to_string[1:]
answers.append(answer_to_string)
# Append variables to lists
top.append(first)
bottom.append(second)
dashes.append('-' * len(second))
# Set spacing
spaced_top = spaces.join(top)
spaced_bottom = spaces.join(bottom)
spaced_dashes = spaces.join(dashes)
spaced_answers = spaces.join(answers)
return spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes + '\n' + spaced_answers
import unittest
from arithmetic_arranger import arithmetic_arranger
# the test case
class UnitTests(unittest.TestCase):
def test_arrangement(self):
actual = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"], True)
expected = " 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----"
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
actual = arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"], True)
expected = " 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------"
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]')
def test_too_many_problems(self):
actual = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"], True)
expected = "Error: Too many problems."
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
def test_incorrect_operator(self):
actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"], True)
expected = "Error: Operator must be '+' or '-'."
self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
def test_too_many_digits(self):
actual = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"], True)
expected = "Error: Numbers cannot be more than four digits."
self.assertEqual(actual, expected, '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."')
def test_only_digits(self):
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"], True)
expected = "Error: Numbers must only contain digits."
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
def test_solutions(self):
actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
expected = " 32 1 45 123\n- 698 - 3801 + 43 + 49\n----- ------ ---- -----\n -666 -3800 88 172"
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
if __name__ == "__main__":
unittest.main()