生成报告的函数
您已经学习了函数可以返回值,但函数也可以直接打印结果。
您的开发团队需要定期查看测试执行时间的报告。与其每次手动计算这些指标,不如编写一个名为 test_report 的自定义函数,用于打印格式化摘要且不返回任何值。
我们已为您提供一个以秒为单位的测试执行时间列表 test_durations。
本练习是课程的一部分
Python 中级:面向开发者
练习说明
- 在函数定义中补全参数,将
durations作为参数名。 - 使用内置函数对
durations求和,计算总测试时间。 - 调用该函数并传入列表
test_durations,生成最近测试的报告。
交互式实操练习
通过完成这段示例代码来试试这个练习。
test_durations = [245.50, 189.99, 312.75, 156.20, 428.90, 201.35, 167.80]
# Complete the function
def test_report(____):
num_tests = len(durations)
# Calculate total test time
total_time = ____(____)
print("=== Test Report ===")
print("Total Tests: ", num_tests)
print("Total Execution Time (s): ", total_time)
# Generate the report for recent test runs
____(____)