# 作業一 ## 作業概述 - 本次作業完成 Line coverage 的測試 - 包含 - 14 行程式碼 - 4 個 test case - 2 個缺失的設計 ## 程式結構分析 ### 測試特別結構分析 - 3 到 6 行為迴圈 - 計算學生成績 - 7 到 14 行為條件判斷 - 學生成績的等級判定 ### 程式碼 ```python # 計算學生成績等級: def process_student_data(scores): # 初始化變數 total = 0 # 第1行 count = 0 # 第2行 # 迴圈 - 計算學生平均成績 while count < len(scores): # 第3行 total += scores[count] # 第4行 count += 1 # 第5行 average = total / len(scores) # 第6行 # 條件判斷 - 學生成績的等級判定 if average >= 90: # 第7行 return "A" # 第8行 elif average >= 80: # 第9行 return "B" # 第10行 elif average >= 70: # 第11行 return "C" # 第12行 else: # 第13行 return "F" # 第14行 ``` ## 測試案例 | Test Case 1 | Result | | -------- | -------- | | Input values | scores=[90, 95] | | Expected result | A | | Test program's result | A | | Criteria analysis | 1~6, 7, 8 | | Test Case 2 | Result | | -------- | -------- | | Input values | scores=[80, 82] | | Expected result | B | | Test program's result | B | | Criteria analysis | 1~6, 7, 9, 10 | | Test Case 3 | Result | | -------- | -------- | | Input values | scores=[75, 77] | | Expected result | C | | Test program's result | C | | Criteria analysis | 1~6, 7, 9, 11, 12 | | Test Case 4 | Result | | -------- | -------- | | Input values | scores=[20, 30] | | Expected result | F | | Test program's result | F | | Criteria analysis | 1~6, 7, 9, 11, 13, 14 | ## 測試案例詳述 - 測試案例一 - 測試成績為 A 的狀況 - 測試:1~6, 7, 8 行 - 測試案例二 - 測試成績為 B 的狀況 - 測試:1~6, 7, 9, 10 行 - 測試案例三 - 測試成績為 C 的狀況 - 測試:1~6, 7, 9, 11, 12 行 - 測試案例四 - 測試成績為 F 的狀況 - 測試:1~6, 7, 9, 11, 13, 14 行 - 總結 - 四個測試案例 - Line Coverage 100% ## 測試工具展示 - 測試案例一 ```python def test_case_1_grade_A(self): # 測試案例 1 - A 等級 result = process_student_data([90, 95]) print("預期結果為 A,實際結果為:", result) assert result == "A" ``` ![image](https://hackmd.io/_uploads/HyyLNfkl-g.png) - 測試案例二 ```python def test_case_2_grade_B(self): # 測試案例 2 - B 等級 result = process_student_data([80, 82]) print("預期結果為 B,實際結果為:", result) assert result == "B" ``` ![image](https://hackmd.io/_uploads/HkuUNMyg-x.png) - 測試案例三 ```python def test_case_3_grade_C(self): # 測試案例 3 - C 等級 result = process_student_data([75, 77]) print("預期結果為 C,實際結果為:", result) assert result == "C" ``` ![image](https://hackmd.io/_uploads/SylPVf1xbe.png) - 測試案例四 ```python def test_case_4_grade_F(self): # 測試案例 4 - F 等級 result = process_student_data([20, 30]) print("預期結果為 F,實際結果為:", result) assert result == "F" ``` ![image](https://hackmd.io/_uploads/rJsPEMklZx.png) - 測試結果 ``` python3 -m pytest collected 4 items === 4 passed in 0.01s === ``` ![image](https://hackmd.io/_uploads/Sk3dNfJgbx.png) - coverage 查看 ![image](https://hackmd.io/_uploads/Bk7vd2yJWg.png) ## 缺失的檢測與查看 ### 缺失設計(一) - 刪除 Test Case 3 ### 缺失結果(一) ``` collected 3 items === 3 passed in 0.02s === ``` ![image](https://hackmd.io/_uploads/HJ1ct3kkbe.png) ![image](https://hackmd.io/_uploads/rJc2Y31y-l.png) ### 缺失設計(二) - 修改 Test Case 1 測試的輸出為 '優' ### 缺失結果(二) ``` === FAILURES === ___ TestCalculator.test_case_1_grade_A ___ self = <complex_calculator_test.TestCalculator object at 0x1056b8a50> def test_case_1_grade_A(self): # 測試案例 1 - A 等級 result = process_student_data([90, 95]) > assert result == "優" E AssertionError: assert 'A' == '優' E E - 優 E + A complex_calculator_test.py:8: AssertionError === short test summary info === FAILED complex_calculator_test.py::TestCalculator::test_case_1_grade_A - AssertionError: assert 'A' == '優' === 1 failed, 3 passed in 0.03s === ``` ![image](https://hackmd.io/_uploads/ry0920yJ-x.png)