关于我们
书单推荐
新书推荐
|
软件单元测试 读者对象:本书适合软件开发工程师、测试工程师、项目经理和大学计算机专业本科高年级学生与研究生阅读。
本书对软件单元测试进行了详细介绍。第1 章与第2 章介绍软件单元测试的概念和基础知识;第3 章到第5 章介绍C 语言、Java 语言和Python 语言的单元测试框架和技巧;第6 章与第7 章介绍代码覆盖率工具和代码语法规范检查工具;第8 章通过两个案例详细介绍TDD。读者可以根据自己的需求对以上内容进行选择性阅读或者全部阅读。另外,为了巩固大家的学习效果,每一章结尾都有相应的习题。 本书适合软件开发工程师、测试工程师、项目经理和大学计算机专业本科高年级学生与研究生阅读。
顾翔,1997年毕业于北京工业大学,中国计算机学会(CFF)会员,上海开源协会个人会员,开源网安专家组成员,国家职业教育研究院软件测试、安全专家组成员。著有《软件测试技术实战:设计、工具及管理》《基于Django的电子商务网站设计》《全栈软件测试工程师宝典》《通过案例玩转JMeter(微课版)》图书。软件绿色联盟2018年最佳优秀讲师获得者,先后就职于炎黄新星网络科技有限公司、中兴通讯股份有限公司、意法半导体(中国)有限公司和爱立信通信(中国)有限公司,担任软件开发工程师、软件测试工程师,软件测试经理等职务,积累了丰富的软件研发测试理论和实践经验,精通测试设计、测试管理、安全测试、性能测试、自动化测试、敏捷测试和DevOps测试技术,从2015年起,从事金融、通信、航空、邮政等行业及高校的软件测试咨询和培训服务。
目 录
第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
你还可能感兴趣
我要评论
|