/
testfunction.py
37 lines (36 loc) · 2.78 KB
/
testfunction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import unittest
import json
def run_suite(test_class, chapter_index):
suite = unittest.TestLoader().loadTestsFromTestCase(test_class)
runner = unittest.TextTestRunner(verbosity=2)
test_results = runner.run(suite)
number_of_failures = len(test_results.failures)
number_of_errors = len(test_results.errors)
number_of_test_runs = test_results.testsRun
number_of_successes = number_of_test_runs - (number_of_failures + number_of_errors)
with open("/home/jovyan/exercise_index.json", "r") as f:
exercise_index = json.load(f)
chapter_name = exercise_index[chapter_index]["chapter_name"]
number_of_total_questions = 0
number_of_completed_questions = 0
for i in range(len(exercise_index)):
number_of_total_questions += exercise_index[i]["number_of_exercises"]
if i < chapter_index:
number_of_completed_questions += exercise_index[i]["number_of_exercises"]
number_of_completed_questions += number_of_successes
chapter_percentage = number_of_successes * 100 / number_of_test_runs
overall_percentage = number_of_completed_questions * 100 / number_of_total_questions
print("你在「{}」章節的練習題完成率為 ... {:.2f}% ({}/{})".format(chapter_name, chapter_percentage, number_of_successes, number_of_test_runs))
print("整體課程練習題的累計完成率為 ... {:.2f}% ({}/{})".format(overall_percentage, number_of_completed_questions, number_of_total_questions))
if chapter_percentage == 100 and chapter_index < 19:
print("表現得很好,你已經完成「{}」所有習題,我們繼續往下個章節:「{}」前進!".format(exercise_index[chapter_index]["chapter_name"], exercise_index[chapter_index + 1]["chapter_name"]))
if chapter_index == 4:
print("太棒了,你已經完成「Python 的 50+ 練習」的第一部分:Python 程式設計的基礎觀念,接下來還有三個部分等你來挑戰!")
elif chapter_index == 8:
print("表現得非常好,你已經完成「Python 的 50+ 練習」的第二部分:Python 程式設計的進階觀念,接著讓我們邁向資料科學!")
elif chapter_index == 12:
print("太令人佩服,你已經完成「Python 的 50+ 練習」的第三部分:Python 資料科學的基礎,距離完課只剩下最後一哩路!")
elif chapter_percentage == 100 and chapter_index == 19:
print("恭喜完課,你已經完成「Python 的 50+ 練習」所有習題,能夠堅持到底完成所有的教學影片與練習題真是非常了不起!後面已經沒有練習題了,你現在是一位擅長寫程式處理資料的分析師!")
elif chapter_percentage >= 50:
print("你已經完成「{}」章節一半以上的練習,繼續加油!".format(chapter_name))