软件工程与测试 Computer Science

9 个科学问题 · 计算机 Computer Science

本页目录

依赖注入与 Mock 对象如何提升代码的可测试性?

How do dependency injection and mock objects improve code testability?

依赖注入把组件所依赖的外部服务通过参数传入,而不是在内部硬编码创建,从而使测试时能用轻量的 Mock 替身替换数据库或网络等重依赖。这样单元可以在隔离环境下被快速、确定性地验证,而不必启动整个系统。
Dependency injection supplies a component's external services through parameters rather than hardcoding their creation, so tests can swap in lightweight mock doubles for heavy dependencies like databases or networks. The unit can then be verified quickly and deterministically in isolation without booting the whole system.
难度 专业 · 适合 高中(15-18) CC BY 4.0 · 已核验 · Verified 2026-11-02
来源 · Source

S. Freeman, N. Pryce《测试驱动的面向对象软件开发》(GOOS) 第3章 (人民邮电出版社) ; 另见: S. Freeman, N. Pryce, Growing Object-Oriented Software, Guided by Tests, Addison-Wesley, 2009

S. Freeman, N. Pryce, Growing Object-Oriented Software, Guided by Tests, Addison-Wesley, 2009 ; see also: S. Freeman, N. Pryce《测试驱动的面向对象软件开发》(GOOS) 第3章 (人民邮电出版社)

#依赖注入 #Mock #可测试性

测试金字塔为什么建议底层单元测试远多于高层端到端测试?

Why does the test pyramid recommend far more low-level unit tests than high-level end-to-end tests?

单元测试运行快、隔离性强、定位问题直接,因此数量可以很大且成本很低;端到端测试涉及多组件与真实环境,运行慢、易碎且调试困难。把多数投入放在底层单元、少量放在高层端到端,能在保证覆盖的同时控制维护成本。
Unit tests run fast, are well isolated, and pinpoint failures directly, so they can be numerous and cheap, whereas end-to-end tests span many components and real environments, making them slow, brittle, and hard to debug. Investing mostly in low-level units and few high-level tests keeps coverage high while controlling maintenance cost.
难度 专业 · 适合 高中(15-18) CC BY 4.0 · 已核验 · Verified 2026-10-04
来源 · Source

Cohn《Succeeding with Agile》第13章 (Prentice Hall), 2009

M. Cohn, Succeeding with Agile: Software Development Using Scrum, Chapter 13, Prentice Hall, 2009

#测试金字塔 #单元测试 #端到端

敏捷开发中的「迭代」概念对交付有什么好处?

What benefit does the iteration concept in agile development bring to delivery?

敏捷把开发切分为固定长度(如两周)的迭代,每个迭代都交付可运行的增量并获取反馈。这样能尽早暴露需求偏差,逐步逼近用户真正需要的功能,而不是等到末期才一次性交付。
Agile breaks development into fixed-length iterations, such as two weeks, each delivering a working increment and gathering feedback. This surfaces requirement mismatches early and gradually converges on what users truly need, rather than delivering everything only at the end.
难度 进阶 · 适合 大众 CC BY 4.0 · 已核验 · Verified 2026-11-02
来源 · Source

Beck《解析极限编程》(Addison-Wesley)

K. Beck, Extreme Programming Explained: Embrace Change, Addison-Wesley

#敏捷 #迭代 #交付

版本控制系统(如 Git)对团队协作有什么核心价值?

What core value does a version control system such as Git bring to team collaboration?

Git 记录每一次代码变更的完整历史,让多人可以并行开发并通过分支与合并协调修改。它还能在出错时回溯到任意历史版本,避免改动丢失,是协作与可维护性的基础。
Git records the full history of every code change, letting many people develop in parallel and coordinate edits through branches and merges. It also lets you revert to any past version when something goes wrong, preventing lost work and underpinning collaboration and maintainability.
难度 进阶 · 适合 大众 CC BY 4.0 · 已核验 · Verified 2026-11-06
来源 · Source

Chacon《Pro Git》第1章 (Apress)

S. Chacon, B. Straub, Pro Git, 2nd ed., Chapter 1, Apress

#Git #版本控制 #分支

单元测试在软件开发中主要解决什么问题?

What problem does unit testing mainly solve in software development?

单元测试针对最小可测试单元(如单个函数或类)编写自动化用例,验证其在各种输入下的行为是否符合预期。它能在开发早期快速捕获逻辑错误,并为后续重构提供安全网。
Unit testing writes automated cases for the smallest testable unit, such as a single function or class, verifying its behavior under various inputs. It catches logic errors early in development and provides a safety net for later refactoring.
难度 进阶 · 适合 初中(12-15) CC BY 4.0 · 已核验 · Verified 2026-11-02
来源 · Source

Beck《测试驱动开发》(Addison-Wesley)

K. Beck, Test-Driven Development: By Example, Addison-Wesley

#单元测试 #测试 #重构

变异测试(mutation testing)的基本原理是什么?为什么它衡量的是测试套件的质量而非被测程序本身的质量?

What is the basic idea of mutation testing, and why does it measure the quality of the test suite rather than the quality of the program itself?

变异测试向代码注入微小缺陷——如把 > 改成 >=、加号改成减号——生成“变异体”。若测试套件对某变异体仍全部通过,说明它未被“杀死”,暴露出断言或输入覆盖不足。变异得分即被杀死变异体的比例,得分越高说明测试越能区分正确与错误的实现。因每个变异体都要重跑测试,工具常靠增量分析和抽样控制开销。
Mutation testing injects many small deliberate defects into the code—changing > to >=, swapping + for -, flipping boolean conditions—to create mutants. If the suite still passes against a mutant, that mutant survives, revealing missing assertions or weak inputs. The mutation score is the fraction of mutants killed; a higher score means the tests can tell correct implementations apart from subtly broken ones. Because every mutant reruns the suite, tools limit cost with incremental analysis and mutant sampling.
难度 进阶 · 适合 初中(12-15) CC BY 4.0 · 已核验 · Verified 2026-11-02
来源 · Source

R. A. DeMillo、R. J. Lipton、F. G. Sayward,《Hints on Test Data Selection: Help for the Practicing Programmer》,IEEE Computer,1978年第11卷第4期。

R. A. DeMillo, R. J. Lipton, F. G. Sayward, "Hints on Test Data Selection: Help for the Practicing Programmer," IEEE Computer, vol. 11, no. 4, 1978.

#变异测试 #测试充分性 #变异得分

一个语句覆盖率达到 100% 的测试套件能否证明程序没有缺陷?为什么覆盖率高低不能直接等同于测试质量的好坏?

Can a test suite with 100% statement coverage prove that a program is defect-free? Why cannot the coverage percentage be equated directly with test quality?

不能。覆盖率只回答哪些代码被执行过,并不关心断言是否严格核对了结果——恒真断言同样能把指标刷满。缺失的功能根本不会生成对应代码,自然无从覆盖;错误的输入组合即便经过某一行也可能放过缺陷。变异测试向程序注入微小缺陷并检验用例能否将其杀死,以此衡量断言的真实甄别力,可作为覆盖率的补充。覆盖率适合用来发现盲区,而不是当成考核目标。
No. Coverage only records which code has been executed and says nothing about whether assertions rigorously checked the results - assertions that always pass inflate the metric just as easily. Missing features generate no code to cover at all, and a wrong input combination can run through a line yet still let a bug escape. Mutation testing injects small defects and checks whether the suite kills them, measuring genuine assertion strength as a complement to coverage. Treat coverage as a tool for finding blind spots, not a target number.
难度 进阶 · 适合 大众 CC BY 4.0 · 已核验 · Verified 2026-11-04
来源 · Source

Al Blwi, Ayad, Khaireddine, Marsit, Mili,《Semantic Coverage: Measuring Test Suite Effectiveness》,Proceedings of the 18th International Conference on Software Technologies,2023 (DOI: 10.5220/0012063900003538)

Al Blwi, Ayad, Khaireddine, Marsit, Mili, 'Semantic Coverage: Measuring Test Suite Effectiveness', Proceedings of the 18th International Conference on Software Technologies, 2023, DOI: 10.5220/0012063900003538

#软件测试 #代码覆盖率 #变异测试

回归测试是为了防范什么风险?

What risk is regression testing meant to guard against?

回归测试在修改或新增功能后重新运行已有测试用例,确认旧功能没有被新改动破坏。它能捕捉「改一处坏一片」的副作用,是持续维护质量的常用手段。
Regression testing reruns existing test cases after a change or new feature to confirm that old functionality was not broken by the new code. It catches the fix-one-thing-break-another side effects and is a standard way to maintain quality continuously.
难度 进阶 · 适合 初中(12-15) CC BY 4.0 · 已核验 · Verified 2026-11-04
来源 · Source

IEEE,《IEEE Standard for Software and System Test Documentation》,IEEE Std 829-2008,2008 (DOI: 10.1109/ieeestd.2008.4578383)

IEEE, 'IEEE Standard for Software and System Test Documentation', IEEE Std 829-2008, 2008, DOI: 10.1109/ieeestd.2008.4578383

#回归测试 #质量保证 #测试

代码评审(Code Review)在团队开发中主要带来哪些收益?

What are the main benefits of code review in team development?

代码评审让其他成员在合并前检查改动,能发现单人所忽略的逻辑缺陷、安全漏洞和风格问题。它同时促进知识在团队内共享,使多人熟悉同一模块,降低人员单点依赖。
Code review has other members inspect changes before merging, catching logic bugs, security holes, and style issues that the author missed. It also spreads knowledge across the team so several people understand each module, reducing single-person dependency.
难度 进阶 · 适合 初中(12-15) CC BY 4.0 · 来源待核验 · Source pending review
来源 · Source

出处备注:著录含书名,但缺版次 / 出版社 / 年份中的一部分,需编辑补全后才能做独立比对。

McConnell《代码大全》第21章 (电子工业出版社)

S. McConnell, Code Complete, 2nd ed., Chapter 21 (Collaborative Construction), Microsoft Press

#代码评审 #质量 #协作