编写测试套件的指南

发布于 2024-09-28 10:50:01 字数 1431 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

婴鹅 2024-10-05 10:50:01

这是一个非常广泛的问题。对于单元测试和测试驱动开发 (TDD),此来自 Microsoft - 如果不适用,您可以忽略特定于 Visual Studio 的建议。

如果您正在寻找有关系统或性能测试的指导,我会澄清您的问题。 Boost.Test 文档

有几个单元测试最好
在我们结束之前审查的做法。
首先,TDD 是无价的
实践。在所有的发展中
可用的方法论,TDD 是
可能是最会的
从根本上改善许多人的发展
未来几年的投资是
最小。任何 QA 工程师都会告诉你
开发者无法编写成功
软件未经相应测试。
对于 TDD,实践是这样写
在编写之前进行这些测试
实施,理想情况下,写作
测试以便它们可以作为一部分运行
无人值守的构建脚本。它
需要纪律来开始这个习惯,
但一旦建立,编码
如果没有 TDD 方法,感觉就像
开车不系安全带。

对于测试本身来说有
一些额外的原则将
帮助成功测试:

避免在之间创建依赖关系
测试,使得测试需要在
特定顺序。每次测试应
自治。

使用测试初始化
验证测试清理的代码
执行成功并重新运行
在执行测试之前进行清理,如果
没有运行。

先写测试
编写任何生产代码
实施。

创建一个测试类
对应于每个类中的
生产代码。这简化了
测试组织并使其易于
选择每个测试的放置位置。

使用
Visual Studio 生成初始
测试项目。这将显着
减少所需的步骤数
手动设置测试项目并
将其与生产相关联
项目。

避免创建其他机器
依赖测试,例如测试
依赖于特定目录
路径。

创建模拟对象来测试
接口。模拟对象是
在测试项目中实施
验证 API 是否匹配
所需的功能。

验证一下
之前所有测试都成功运行
继续创建新测试。那
确保修复代码的方式
打破它后立即。

最大化
可以运行的测试数量
无人值守。绝对确定
没有合理的无人值守
在完全依赖之前测试解决方案
关于手动测试。

This is a very broad question. For unit testing and Test Driven Development (TDD), there is some useful (if somewhat platidinous in parts) guidance on this from Microsoft - you can overlook the Visual Studio-specific advice, if it does not apply.

If you are looking for guidance on system or performance testing, I would clarify your question. There is a decent broader rationale in the docs for Boost.Test.

There are several unit testing best
practices to review before we close.
Firstly, TDD is an invaluable
practice. Of all the development
methodologies available, TDD is
probably the one that will most
radically improve development for many
years to come and the investment is
minimal. Any QA engineer will tell you
that developers can't write successful
software without corresponding tests.
With TDD, the practice is to write
those tests before even writing the
implementation and ideally, writing
the tests so that they can run as part
of an unattended build script. It
takes discipline to begin this habit,
but once it is established, coding
without the TDD approach feels like
driving without a seatbelt.

For the tests themselves there are
some additional principals that will
help with successful testing:

Avoid creating dependencies between
tests such that tests need to run in a
particular order. Each test should be
autonomous.

Use test initialization
code to verify that test cleanup
executed successfully and re-run the
cleanup before executing a test if it
did not run.

Write tests before
writing the any production code
implementation.

Create one test class
corresponding to each class within the
production code. This simplifies the
test organization and makes it easy to
choose where to places each test.

Use
Visual Studio to generate the initial
test project. This will significantly
reduce the number of steps needed when
manually setting up a test project and
associating it to the production
project.

Avoid creating other machine
dependent tests such as tests
dependent on a particular directory
path.

Create mock objects to test
interfaces. Mock objects are
implemented within a test project to
verify that the API matches the
required functionality.

Verify that
all tests run successfully before
moving on to creating a new test. That
way you ensure that you fix code
immediately upon breaking it.

Maximize
the number of tests that can be run
unattended. Make absolutely certain
that there is no reasonable unattended
testing solution before relying solely
on manual testing.

酒儿 2024-10-05 10:50:01

TDD 无疑是一组最佳实践。在改造测试时,我的目标是代码覆盖率和边界条件覆盖率两件事。基本上,您应该选择函数的输入,以便 A) 所有代码路径都经过测试,如果所有代码路径的所有排列都经过测试,效果会更好(有时可能会出现大量情况,如果路径差异表面上不同,则实际上没有必要)并且B) 如果您的代码具有 if x > ,则测试所有边界条件(这些是导致代码路径选择变化的条件)。在其中的 5 中,您使用 x = 5 和 x = 6 进行测试以获得边界的两侧。

TDD is certainly one set of bests practices. When retrofitting tests, I aim for two things code coverage and boundary condition coverage. Basically you should pick inputs to functions such that A) All code paths are tested and better if all permutations of all code paths are tested(sometimes that can be a large number of cases and not really be necessary if path differences are superficially different) and B) That all boundary conditions(those are conditions that cause variation in code path selection) are tested if your code has an if x > 5 in it you test with x = 5, and x = 6 to get both sides of the boundary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文