軟體單元測試

軟體單元測試

《軟體單元測試》是2023年電子工業出版社出版的書籍,作者是顧翔

基本介紹

  • 中文名:軟體單元測試
  • 作者:顧翔
  • 出版社:電子工業出版社
  • 出版地:北京
  • 頁數:316 頁
  • 字數:374
  • ISBN:9787121455094
  • 類別:計算機
  • 定價:89 元
作者介紹,內容簡介,圖書目錄,

作者介紹

顧翔,1997年畢業於北京工業大學,中國計算機學會(CFF)會員,上海開源協會個人會員,開源網安專家組成員,國家職業教育研究院軟體測試、安全專家組成員。著有《軟體測試技術實戰:設計、工具及管理》《基於Django的電子商務網站設計》《全棧軟體測試工程師寶典》《通過案例玩轉JMeter(微課版)》圖書。軟體綠色聯盟2018年最佳優秀講師獲得者,先後就職於炎黃新星網路科技有限公司、中興通訊股份有限公司、意法半導體(中國)有限公司和愛立信通信(中國)有限公司,擔任軟體開發工程師、軟體測試工程師,軟體測試經理等職務,積累了豐富的軟體研發測試理論和實踐經驗,精通測試設計、測試管理、安全測試、性能測試、自動化測試、敏捷測試和DevOps測試技術,從2015年起,從事金融、通信、航空、郵政等行業及高校的軟體測試諮詢和培訓服務。

內容簡介

本書對軟體單元測試進行了詳細介紹。第1 章與第2 章介紹軟體單元測試的概念和基礎知識;第3 章到第5 章介紹C 語言、Java 語言和Python 語言的單元測試框架和技巧;第6 章與第7 章介紹代碼覆蓋率工具和代碼語法規範檢查工具;第8 章通過兩個案例詳細介紹TDD。讀者可以根據自己的需求對以上內容進行選擇性閱讀或者全部閱讀。另外,為了鞏固大家的學習效果,每一章結尾都有相應的習題。 本書適合軟體開發工程師、測試工程師、項目經理和大學計算機專業本科高年級學生與研究生閱讀。

圖書目錄

第1 章 軟體單元測試簡介 ····································································.1
1.1 樁對象和測試驅動函式 ······························································.1
1.2 測試驅動開發 ·········································································.2
1.3 軟體測試應該貫徹始終 ······························································.2
1.4 軟體測試金字塔·······································································.3
1.5 單元測試在傳統開發模式中的地位 ···············································.4
1.6 單元測試在敏捷開發模式中的地位 ···············································.5
1.7 精準測試 ···············································································.5
1.8 單元測試和白盒測試 ·································································.6
1.9 單元測試的FIRST 原則和AIR 原則 ··············································.7
1.10 習題 ····················································································.7
第2 章 軟體單元測試基礎知識 ······························································.8
2.1 動態自動化單元測試 ·································································.8
2.1.1 被測程式 ·······································································.8
2.1.2 語句覆蓋 ·······································································.8
2.1.3 分支覆蓋 ······································································.11
2.1.4 條件覆蓋 ·······································································13
2.1.5 條件/分支覆蓋 ································································15
2.1.6 MC/DC 覆蓋 ··································································15
2.1.7 路徑覆蓋 ·······································································17
2.1.8 幾種覆蓋率的強弱關係 ·····················································18
2.1.9 控制流覆蓋 ····································································18
2.2 靜態自動化單元測試:代碼掃描 ··················································19
2.3 手工單元測試:代碼調試 ···························································20
2.4 手工單元測試:代碼評審 ···························································20
2.5 單元測試中的問題 ····································································20
2.5.1 功能層面的問題·····························································.21
2.5.2 性能層面的問題·····························································.22
2.5.3 安全層面的問題·····························································.22
2.6 習題 ····················································································.23
第3 章 C 語言動態自動化單元測試框架 ·················································.24
3.1 在Windows 下安裝C 語言運行環境 ············································.24
3.1.1 安裝配置MinGW ···························································.24
3.1.2 安裝配置MSYS2 ···························································.25
3.1.3 安裝配置IDE ································································.25
3.2 安裝編譯CUnit ······································································.31
3.2.1 在Windows 下安裝CUnit ·················································.31
3.2.2 在Linux 下安裝CUnit ·····················································.31
3.2.3 創建被測檔案和測試檔案 ·················································.32
3.2.4 在Windows 下運行測試檔案 ·············································.34
3.2.5 在Linux 下運行測試檔案 ·················································.34
3.2.6 運行結果 ·····································································.35
3.3 查看測試報告 ········································································.35
3.4 CUnit 介紹 ············································································.36
3.4.1 CUnit 的四種運行模式 ·····················································.36
3.4.2 CUnit 頭檔案 ································································.38
3.4.3 CUnit 支持的斷言 ··························································.39
3.4.4 CUnit 架構 ···································································.40
3.4.5 CUnit 的基本測試步驟 ·····················································.40
3.5 案例 ····················································································.41
3.5.1 指針操作 ·····································································.41
3.5.2 返回結構體 ··································································.43
3.5.3 檔案的讀寫操作·····························································.44
3.6 習題 ····················································································.46
第4 章 Java 語言動態自動化單元測試框架 ·············································.47
4.1 在Eclipse 中創建Maven 項目 ····················································.47
4.2 在Eclipse 中配置JUnit 和TestNG 運行環境 ···································.50
4.2.1 配置JUnit 運行環境 ························································.50
4.2.2 配置TestNG 運行環境 ·····················································.53
4.3 JUnit 4 ··················································································55
4.3.1 JUnit 4 的測試代碼 ··························································55
4.3.2 與JUnit 4 相關的API ·······················································58
4.3.3 JUnit 4 的裝飾器 ·····························································58
4.3.4 JUnit 4 的斷言 ································································59
4.3.5 逾時測試 ·······································································59
4.3.6 JUnit 4 參數化測試 ··························································60
4.3.7 測試異常 ·······································································61
4.3.8 批量運行 ·······································································63
4.3.9 利用Ant 運行 ·································································63
4.3.10 利用Maven 運行 ····························································67
4.3.11 配合Allure 生成漂亮的JUnit 4 測試報告 ······························70
4.4 JUnit 5 ··················································································82
4.4.1 JUnit 5 的測試代碼 ··························································82
4.4.2 與JUnit 5 相關的API ·······················································83
4.4.3 JUnit 5 的裝飾器 ·····························································84
4.4.4 JUnit 5 的斷言 ································································86
4.4.5 JUnit 5 的依賴注入 ··························································88
4.4.6 傳遞自定義參數 ······························································90
4.4.7 JUnit 5 參數化測試 ··························································91
4.4.8 內嵌測試類 ····································································94
4.4.9 重複測試 ·······································································96
4.4.10 動態測試 ·····································································97
4.4.11 分組斷言assertAll ··························································98
4.4.12 批量測試 ·····································································98
4.4.13 利用Maven 運行 ····························································99
4.4.14 配合Allure 生成漂亮的JUnit 5 測試報告 ···························.101
4.5 TestNG ···············································································.105
4.5.1 TestNG 的使用和運行 ····················································.105
4.5.2 testng.xml 檔案與Suite 測試 ············································.107
4.5.3 與TestNG 相關的API ····················································.109
4.5.4 TestNG 的裝飾器 ··························································.110
4.5.5 TestNG 的斷言 ·····························································.113
4.5.6 異常測試 ····································································.113
4.5.7 忽略測試 ····································································.114
4.5.8 逾時測試 ···································································.114
4.5.9 分組測試 ···································································.114
4.5.10 依賴測試 ··································································.116
4.5.11 TestNG 參數化測試 ·····················································.119
4.5.12 TestNG 報告 ······························································.121
4.5.13 利用Maven 運行 ························································.121
4.5.14 配合Allure 生成漂亮的TestNG 測試報告 ··························.123
4.6 測試替身 ············································································.127
4.6.1 樁對象 ······································································.128
4.6.2 偽造對象 ···································································.128
4.6.3 間諜對象 ···································································.131
4.6.4 模擬對象 ···································································.132
4.7 利用EvoSuite 自動生成測試用例 ··············································.155
4.7.1 在Eclipse 中運行 ·························································.155
4.7.2 在命令行中運行···························································.156
4.8 變異測試 ············································································.157
4.8.1 變異測試引出 ·····························································.157
4.8.2 變異測試簡介 ·····························································.158
4.8.3 PITest 在Eclipse 中的安裝和使用 ·····································.160
4.8.4 PITest 測試報告 ···························································.162
4.8.5 修改測試數據 ·····························································.163
4.9 在Jenkins 中配置JUnit 4、JUnit 5、TestNG 和Allure ·····················.164
4.9.1 Jenkins 安裝和基本配置 ·················································.164
4.9.2 JUnit 在Jenkins 中的配置 ···············································.168
4.9.3 TestNG 在Jenkins 中的配置 ············································.170
4.9.4 Allure JUnit 在Jenkins 中的配置 ·······································.172
4.10 習題 ················································································.174
第5 章 Python 語言動態自動化單元測試框架 ········································.176
5.1 unittest ···············································································.176
5.1.1 計算器案例 ································································.176
5.1.2 unittest 的裝飾器 ··························································.178
5.1.3 unittest 的斷言 ·····························································.179
5.1.4 通過parameterized.expand 實現參數化 ·······························.179
5.1.5 測試異常 ···································································.180
5.1.6 批量運行生成報告 ························································.181
5.2 Pytest ·················································································.182
5.2.1 Pytest 安裝 ··································································.182
5.2.2 案例··········································································.183
5.2.3 Pytest 的裝飾器 ····························································.185
5.2.4 Pytest 常用命令行選項 ···················································.185
5.2.5 Pytest 實現並發測試 ······················································.186
5.2.6 Pytest 特有的參數化功能 ················································.187
5.2.7 配合Allure 生成漂亮的Pytest 測試報告 ·····························.190
5.2.8 在Jenkins 中配置Allure Pytest ·········································.195
5.3 Python 的模擬對象 ································································.197
5.3.1 產品代碼 ····································································.197
5.3.2 通過unittest 使用模擬對象 ··············································.197
5.3.3 通過Pytest 使用模擬對象 ···············································.198
5.4 變異測試工具mutpy ······························································.199
5.4.1 mutpy 的安裝 ······························································.199
5.4.2 mutpy 的使用 ······························································.199
5.5 習題 ··················································································.201
第6 章 代碼覆蓋率工具 ····································································.202
6.1 C 語言覆蓋率工具gcov 和lcov ·················································.202
6.1.1 lcov 與gcov 的安裝和運行 ··············································.202
6.1.2 lcov 報告 ····································································.205
6.1.3 lcov 在Jenkins 中的套用 ·················································.205
6.2 Java 語言覆蓋率工具JaCoCo ····················································.207
6.2.1 JaCoCo 在Eclipse 下的套用 ·············································.207
6.2.2 JaCoCo 在Maven 下的套用 ·············································.210
6.2.3 JaCoCo 在Jenkins 中的套用 ············································.211
6.3 Python 語言覆蓋率工具Coverage 和pytest-cov ······························.213
6.3.1 Coverage ····································································.213
6.3.2 pytest-cov ···································································.215
6.3.3 Python 語言覆蓋率工具在Jenkins 中的套用 ·························.216
6.4 習題 ··················································································.217
第7 章 代碼語法規範檢查工具 ···························································.218
7.1 Java 語言靜態分析工具PMD ····················································.218
7.1.1 PMD 在Eclipse 下的套用 ················································.218
7.1.2 配置PMD 的Maven 檔案 ···············································.220
7.1.3 在Jenkins 中配置PMD ··················································.222
7.2 Python 語言靜態分析工具flake8 和pylint ····································.225
7.2.1 flake8 ········································································.225
7.2.2 pylint ········································································.226
7.2.3 flack8 和pylint 在Jenkins 中的套用 ···································.227
7.3 多代碼語法規範檢查平台SonarQube··········································.229
7.3.1 安裝JDK ···································································.229
7.3.2 SonarQube 支持的資料庫 ················································.231
7.3.3 SonarQube ··································································.236
7.3.4 安裝sonar-scanner ························································.242
7.3.5 SonarQube 的配置及套用 ················································.244
7.3.6 在Jenkins 中配置SonarQube ···········································.254
7.4 習題 ··················································································.258
第8 章 TDD 案例 ············································································.259
8.1 斐波那契數列 ······································································.259
8.1.1 初始化 ······································································.259
8.1.2 第一次需求變更···························································.261
8.1.3 第二次需求變更···························································.263
8.1.4 第三次需求變更···························································.266
8.1.5 第四次需求變更···························································.268
8.2 完善計算器產品代碼 ·····························································.271
8.3 利用Jenkins 分析TDD 代碼 ····················································.292
8.4 習題 ··················································································.297
附錄A 在寫作過程中發現開源軟體中的Bug ··········································.298
參考文獻 ························································································.301

熱門詞條

聯絡我們