關(guān)于我們
書單推薦
新書推薦
|
軟件單元測試 讀者對象:本書適合軟件開發(fā)工程師、測試工程師、項目經(jīng)理和大學(xué)計算機(jī)專業(yè)本科高年級學(xué)生與研究生閱讀。
本書對軟件單元測試進(jìn)行了詳細(xì)介紹。第1 章與第2 章介紹軟件單元測試的概念和基礎(chǔ)知識;第3 章到第5 章介紹C 語言、Java 語言和Python 語言的單元測試框架和技巧;第6 章與第7 章介紹代碼覆蓋率工具和代碼語法規(guī)范檢查工具;第8 章通過兩個案例詳細(xì)介紹TDD。讀者可以根據(jù)自己的需求對以上內(nèi)容進(jìn)行選擇性閱讀或者全部閱讀。另外,為了鞏固大家的學(xué)習(xí)效果,每一章結(jié)尾都有相應(yīng)的習(xí)題。 本書適合軟件開發(fā)工程師、測試工程師、項目經(jīng)理和大學(xué)計算機(jī)專業(yè)本科高年級學(xué)生與研究生閱讀。
顧翔,1997年畢業(yè)于北京工業(yè)大學(xué),中國計算機(jī)學(xué)會(CFF)會員,上海開源協(xié)會個人會員,開源網(wǎng)安專家組成員,國家職業(yè)教育研究院軟件測試、安全專家組成員。著有《軟件測試技術(shù)實戰(zhàn):設(shè)計、工具及管理》《基于Django的電子商務(wù)網(wǎng)站設(shè)計》《全棧軟件測試工程師寶典》《通過案例玩轉(zhuǎn)JMeter(微課版)》圖書。軟件綠色聯(lián)盟2018年最佳優(yōu)秀講師獲得者,先后就職于炎黃新星網(wǎng)絡(luò)科技有限公司、中興通訊股份有限公司、意法半導(dǎo)體(中國)有限公司和愛立信通信(中國)有限公司,擔(dān)任軟件開發(fā)工程師、軟件測試工程師,軟件測試經(jīng)理等職務(wù),積累了豐富的軟件研發(fā)測試?yán)碚摵蛯嵺`經(jīng)驗,精通測試設(shè)計、測試管理、安全測試、性能測試、自動化測試、敏捷測試和DevOps測試技術(shù),從2015年起,從事金融、通信、航空、郵政等行業(yè)及高校的軟件測試咨詢和培訓(xùn)服務(wù)。
目 錄
第1 章 軟件單元測試簡介 ····································································.1 1.1 樁對象和測試驅(qū)動函數(shù) ······························································.1 1.2 測試驅(qū)動開發(fā) ·········································································.2 1.3 軟件測試應(yīng)該貫徹始終 ······························································.2 1.4 軟件測試金字塔·······································································.3 1.5 單元測試在傳統(tǒng)開發(fā)模式中的地位 ···············································.4 1.6 單元測試在敏捷開發(fā)模式中的地位 ···············································.5 1.7 精準(zhǔn)測試 ···············································································.5 1.8 單元測試和白盒測試 ·································································.6 1.9 單元測試的FIRST 原則和AIR 原則 ··············································.7 1.10 習(xí)題 ····················································································.7 第2 章 軟件單元測試基礎(chǔ)知識 ······························································.8 2.1 動態(tài)自動化單元測試 ·································································.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 幾種覆蓋率的強(qiáng)弱關(guān)系 ·····················································18 2.1.9 控制流覆蓋 ····································································18 2.2 靜態(tài)自動化單元測試:代碼掃描 ··················································19 2.3 手工單元測試:代碼調(diào)試 ···························································20 2.4 手工單元測試:代碼評審 ···························································20 2.5 單元測試中的問題 ····································································20 2.5.1 功能層面的問題·····························································.21 2.5.2 性能層面的問題·····························································.22 2.5.3 安全層面的問題·····························································.22 2.6 習(xí)題 ····················································································.23 第3 章 C 語言動態(tài)自動化單元測試框架 ·················································.24 3.1 在Windows 下安裝C 語言運(yùn)行環(huán)境 ············································.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 創(chuàng)建被測文件和測試文件 ·················································.32 3.2.4 在Windows 下運(yùn)行測試文件 ·············································.34 3.2.5 在Linux 下運(yùn)行測試文件 ·················································.34 3.2.6 運(yùn)行結(jié)果 ·····································································.35 3.3 查看測試報告 ········································································.35 3.4 CUnit 介紹 ············································································.36 3.4.1 CUnit 的四種運(yùn)行模式 ·····················································.36 3.4.2 CUnit 頭文件 ································································.38 3.4.3 CUnit 支持的斷言 ··························································.39 3.4.4 CUnit 架構(gòu) ···································································.40 3.4.5 CUnit 的基本測試步驟 ·····················································.40 3.5 案例 ····················································································.41 3.5.1 指針操作 ·····································································.41 3.5.2 返回結(jié)構(gòu)體 ··································································.43 3.5.3 文件的讀寫操作·····························································.44 3.6 習(xí)題 ····················································································.46 第4 章 Java 語言動態(tài)自動化單元測試框架 ·············································.47 4.1 在Eclipse 中創(chuàng)建Maven 項目 ····················································.47 4.2 在Eclipse 中配置JUnit 和TestNG 運(yùn)行環(huán)境 ···································.50 4.2.1 配置JUnit 運(yùn)行環(huán)境 ························································.50 4.2.2 配置TestNG 運(yùn)行環(huán)境 ·····················································.53 4.3 JUnit 4 ··················································································55 4.3.1 JUnit 4 的測試代碼 ··························································55 4.3.2 與JUnit 4 相關(guān)的API ·······················································58 4.3.3 JUnit 4 的裝飾器 ·····························································58 4.3.4 JUnit 4 的斷言 ································································59 4.3.5 超時測試 ·······································································59 4.3.6 JUnit 4 參數(shù)化測試 ··························································60 4.3.7 測試異常 ·······································································61 4.3.8 批量運(yùn)行 ·······································································63 4.3.9 利用Ant 運(yùn)行 ·································································63 4.3.10 利用Maven 運(yùn)行 ····························································67 4.3.11 配合Allure 生成漂亮的JUnit 4 測試報告 ······························70 4.4 JUnit 5 ··················································································82 4.4.1 JUnit 5 的測試代碼 ··························································82 4.4.2 與JUnit 5 相關(guān)的API ·······················································83 4.4.3 JUnit 5 的裝飾器 ·····························································84 4.4.4 JUnit 5 的斷言 ································································86 4.4.5 JUnit 5 的依賴注入 ··························································88 4.4.6 傳遞自定義參數(shù) ······························································90 4.4.7 JUnit 5 參數(shù)化測試 ··························································91 4.4.8 內(nèi)嵌測試類 ····································································94 4.4.9 重復(fù)測試 ·······································································96 4.4.10 動態(tài)測試 ·····································································97 4.4.11 分組斷言assertAll ··························································98 4.4.12 批量測試 ·····································································98 4.4.13 利用Maven 運(yùn)行 ····························································99 4.4.14 配合Allure 生成漂亮的JUnit 5 測試報告 ···························.101 4.5 TestNG ···············································································.105 4.5.1 TestNG 的使用和運(yùn)行 ····················································.105 4.5.2 testng.xml 文件與Suite 測試 ············································.107 4.5.3 與TestNG 相關(guān)的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 參數(shù)化測試 ·····················································.119 4.5.12 TestNG 報告 ······························································.121 4.5.13 利用Maven 運(yùn)行 ························································.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 中運(yùn)行 ·························································.155 4.7.2 在命令行中運(yùn)行···························································.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 修改測試數(shù)據(jù) ·····························································.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 習(xí)題 ················································································.174 第5 章 Python 語言動態(tài)自動化單元測試框架 ········································.176 5.1 unittest ···············································································.176 5.1.1 計算器案例 ································································.176 5.1.2 unittest 的裝飾器 ··························································.178 5.1.3 unittest 的斷言 ·····························································.179 5.1.4 通過parameterized.expand 實現(xiàn)參數(shù)化 ·······························.179 5.1.5 測試異常 ···································································.180 5.1.6 批量運(yùn)行生成報告 ························································.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 實現(xiàn)并發(fā)測試 ······················································.186 5.2.6 Pytest 特有的參數(shù)化功能 ················································.187 5.2.7 配合Allure 生成漂亮的Pytest 測試報告 ·····························.190 5.2.8 在Jenkins 中配置Allure Pytest ·········································.195 5.3 Python 的模擬對象 ································································.197 5.3.1 產(chǎn)品代碼 ····································································.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 習(xí)題 ··················································································.201 第6 章 代碼覆蓋率工具 ····································································.202 6.1 C 語言覆蓋率工具gcov 和lcov ·················································.202 6.1.1 lcov 與gcov 的安裝和運(yùn)行 ··············································.202 6.1.2 lcov 報告 ····································································.205 6.1.3 lcov 在Jenkins 中的應(yīng)用 ·················································.205 6.2 Java 語言覆蓋率工具JaCoCo ····················································.207 6.2.1 JaCoCo 在Eclipse 下的應(yīng)用 ·············································.207 6.2.2 JaCoCo 在Maven 下的應(yīng)用 ·············································.210 6.2.3 JaCoCo 在Jenkins 中的應(yīng)用 ············································.211 6.3 Python 語言覆蓋率工具Coverage 和pytest-cov ······························.213 6.3.1 Coverage ····································································.213 6.3.2 pytest-cov ···································································.215 6.3.3 Python 語言覆蓋率工具在Jenkins 中的應(yīng)用 ·························.216 6.4 習(xí)題 ··················································································.217 第7 章 代碼語法規(guī)范檢查工具 ···························································.218 7.1 Java 語言靜態(tài)分析工具PMD ····················································.218 7.1.1 PMD 在Eclipse 下的應(yīng)用 ················································.218 7.1.2 配置PMD 的Maven 文件 ···············································.220 7.1.3 在Jenkins 中配置PMD ··················································.222 7.2 Python 語言靜態(tài)分析工具flake8 和pylint ····································.225 7.2.1 flake8 ········································································.225 7.2.2 pylint ········································································.226 7.2.3 flack8 和pylint 在Jenkins 中的應(yīng)用 ···································.227 7.3 多代碼語法規(guī)范檢查平臺SonarQube··········································.229 7.3.1 安裝JDK ···································································.229 7.3.2 SonarQube 支持的數(shù)據(jù)庫 ················································.231 7.3.3 SonarQube ··································································.236 7.3.4 安裝sonar-scanner ························································.242 7.3.5 SonarQube 的配置及應(yīng)用 ················································.244 7.3.6 在Jenkins 中配置SonarQube ···········································.254 7.4 習(xí)題 ··················································································.258 第8 章 TDD 案例 ············································································.259 8.1 斐波那契數(shù)列 ······································································.259 8.1.1 初始化 ······································································.259 8.1.2 第一次需求變更···························································.261 8.1.3 第二次需求變更···························································.263 8.1.4 第三次需求變更···························································.266 8.1.5 第四次需求變更···························································.268 8.2 完善計算器產(chǎn)品代碼 ·····························································.271 8.3 利用Jenkins 分析TDD 代碼 ····················································.292 8.4 習(xí)題 ··················································································.297 附錄A 在寫作過程中發(fā)現(xiàn)開源軟件中的Bug ··········································.298 參考文獻(xiàn) ························································································.301
你還可能感興趣
我要評論
|