開始使用免費開始

產生報告的函式

你已經學到函式可以回傳值,但它們也可以直接印出結果。

你的開發團隊需要定期取得測試執行時間的報告。與其每次手動計算這些指標,不如建立一個名為 test_report 的自訂函式,直接列印格式化的摘要而不回傳任何值。

我們已為你提供一個 test_durations 清單,內容為以秒為單位的測試執行時間。

本練習屬於課程

Intermediate Python for Developers

檢視課程

練習說明

  • 在函式定義中加入參數 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
____(____)
編輯並執行程式碼