python - 这个 AssertionError 在我的代码上下文中是什么意思?失败:test_arrangement (test_module.UnitTests)
问题描述
我正在研究算术编曲问题。
程序在控制台输出中排列并返回问题,如下所示。它是一个接受两个参数的函数:一个列表,其中每个项目是一个加法或减法问题,以及一个可选的第二个参数,如果为真,则在每个问题下的破折号下方显示每个答案。
对我来说,该程序似乎在 test_module 文件中的 6 个测试中失败了 1 个。
这是我运行程序时的控制台输出,错误如下所示:
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)
我的 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)
我的 algorithm_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
我的 test_module.py 文件的内容:
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()
解决方案
推荐阅读
- python - 如何在 Python 中删除列表中的重复元素
- python - 如何将接收到的(承载)访问令牌传递给生成的 REST 客户端,以调用安全的 API-Gateway 端点
- python - Python - datetime.time() 比较将 AttributeError 作为“str”对象返回
- reactjs - 如何使用 Recoil 在 React 组件之外操作全局状态?
- python - 获取 xgboost 线性模型的系数
- r - 如何融化具有多个组和空后缀的data.frame
- angular - Angular - 如何在按钮中添加金额?
- javascript - 如何根据参数插入键并在对象 es6 中更新其值
- html - 为什么会出现我网站上的图像和文字?
- powershell - 使用 Powershell 在现有节之间添加新节