Python 如何在单元测试中更正此断言错误?
我在做一道算术题 程序安排并返回问题,如控制台输出中所示。它是一个接受两个参数的函数:一个列表,其中每个项目都是一个加法或减法问题;另一个可选的第二个参数,如果为true,则在每个问题的破折号下显示每个答案 在我看来,该程序在test_模块文件中的6个测试中有1个失败 这是我运行程序时的控制台输出,错误如下所示:Python 如何在单元测试中更正此断言错误?,python,unit-testing,assertion,Python,Unit Testing,Assertion,我在做一道算术题 程序安排并返回问题,如控制台输出中所示。它是一个接受两个参数的函数:一个列表,其中每个项目都是一个加法或减法问题;另一个可选的第二个参数,如果为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: ' [36 chars] - 2 + 43 + 49\n----- ------ ---- -----\n' != ' [36 chars] - 2 + 43 + 49\n----- ------ ---- -----'
3 3801 45 123
+ 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -
+ ----- ------ ---- ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=1)
以下是我的main.py文件的内容:
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)
以下是我的算术排列程序文件的内容:
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
如果不进行太多的研究,看起来就像是在算术排列函数的末尾添加了一个换行符,即使结果不应打印:
return spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes + '\n' + spaced_answers
测试用例中不存在该换行符。一切正常,因此我在这里发布算术排列程序文件中的最终代码:
def arithmetic_arranger(problems, results = False):
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]
difference_first = len(first) - len(second)
difference_second = len(second) - len(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)
if results:
arranged_problems = spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes + '\n' + spaced_answers
else:
arranged_problems = spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes
return arranged_problems
def算术排列程序(问题,结果=错误):
如果len(问题)>5:
返回“错误:问题太多。”
top=[]
底部=[]
破折号=[]
空格=“”
答案=[]
# ["32 + 698", "3801 - 2", "50 + 50", "123 - 49", "12 + 3600"]
问题中的问题:
first=''+problem.split()[0]
秒=问题。分割(“”,1)[1]
差分_first=len(第一)-len(第二)
差秒=长(秒)-长(一)
宽度\u first=len(problem.split()[0])
宽度\u秒=len(problem.split()[2])
如果第一宽度>4或第二宽度>4:
return“错误:数字不能超过四位数。”
如果第二个[0]!='+'第二个[0]!='-':
return“错误:运算符必须为“+”或“-”
如果第一个.lstrip().isdigit()!=真或秒[2:]isdigit()!=正确:
return“错误:数字只能包含数字。”
如果差异_首先>0:
秒=秒[0]+“”*(差分_first+1)+秒[2:]
elif差分_秒>0:
第一名=''*差分_第二名+第一名
#显示问题下答案的可选条件:
如果结果是:
如果第二个[0]=='+':
答案=整数(第一)+整数(第二[2:])
其他:
答案=int(第一个)-int(第二个[2:])
答案到字符串=str(答案)
长度结果=len(回答字符串)
answer_to_string=''+answer_to_string
长度_max=max([len(first.lstrip()),len(second[1:][.lstrip())]))
如果长度\最大值>长度\结果:
答案对字符串=“”*(长度最大值-长度结果)+答案对字符串
elif长度_最大值<长度_结果:
answer_to_string=answer_to_string[1:]
answers.append(answer\u到\u字符串)
#将变量附加到列表
top.append(第一个)
bottom.append(第二个)
破折号。追加('-'*len(秒))
#设置间距
间隔的顶部=空格。连接(顶部)
间隔底部=空格。连接(底部)
间隔破折号=空格。连接(破折号)
间隔的答案=空格。连接(答案)
如果结果是:
排列的问题=间隔的顶部+'\n'+间隔的底部+'\n'+间隔的破折号+'\n'+间隔的答案
其他:
排列的问题=间隔的顶部+'\n'+间隔的底部+'\n'+间隔的破折号
退货问题
更改代码中的单个参数并不能保证发布一个几乎相同的单独问题。您可以编辑原始帖子,向问题添加评论,或向答案添加评论,就像您所做的那样,然后留出时间进行后续操作。
def arithmetic_arranger(problems, results = False):
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]
difference_first = len(first) - len(second)
difference_second = len(second) - len(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)
if results:
arranged_problems = spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes + '\n' + spaced_answers
else:
arranged_problems = spaced_top + '\n' + spaced_bottom + '\n' + spaced_dashes
return arranged_problems