解释单元测试的要点是什么 [英] What are key points to explain Unit Testing

查看:30
本文介绍了解释单元测试的要点是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向一些没有或几乎没有单元测试经验的同事介绍单元测试.我将从大约一个小时的演示开始,解释这个概念并给出很多例子.我将跟进结对编程会议和代码审查.

I want to introduce Unit Testing to some colleagues that have no or little experience with Unit Testing. I'll start with a presentation of about an hour to explain the concept and give lots of examples. I'll follow up with pair programming sessions and code reviews.

在介绍中应重点关注哪些要点?

What are the key points that should be focussed on at the intrduction?

推荐答案

单元测试测试小事

另一件要记住的事情是单元测试测试小东西,单元".因此,如果您的测试针对诸如实时服务器或数据库之类的资源运行,大多数人将其称为系统或集成测试.为了对与这样的资源交谈的代码进行单元测试,人们经常使用模拟对象a>(通常称为模拟).

Unit tests test small things

Another thing to remember is that unit tests test small things, "units". So if your test runs against a resource like a live server or a database, most people call that a system or integration test. To unit test just the code that talks to a resource like that, people often use mock objects (often called mocks).

当单元测试测试小事情时,测试运行得很快.这是好事.经常运行单元测试有助于您在问题发生后尽快发现问题.频繁运行的单元测试的最终目的是将它们作为持续集成的一部分进行自动化.

When unit tests test small things, the tests run fast. That's a good thing. Frequently running unit tests helps you catch problems soon after the occur. The ultimate in frequently running unit tests is having them automated as part of continuous integration.

人们对是否需要 100% 的单元测试覆盖率有不同的看法.我相信高覆盖率是好的,但有一个收益递减点.作为一个非常粗略的经验法则,如果代码库的覆盖率达到 85%,并且具有良好的单元测试,我会很高兴.

People have different views as to whether 100% unit test coverage is desirable. I'm of the belief that high coverage is good, but that there's a point of diminishing return. As a very rough rule of thumb, I would be happy with a code base that had 85% coverage with good unit tests.

与单元测试一样重要的是,其他类型的测试,如集成测试、验收测试等,也可以被视为经过良好测试的系统的一部分.

As important as unit tests are, other types of testing, like integration tests, acceptance tests, and others can also be considered parts of a well-tested system.

如果您想向现有代码添加单元测试,您可能需要查看 有效处理遗留代码 作者:Michael Feathers.未考虑测试而设计的代码可能具有使测试变得困难的特征和Feathers 撰写了有关仔细重构代码以使其更易于测试的方法.当您熟悉某些使测试代码变得困难的模式时,您和您的团队就可以编写代码来尝试避免/最小化这些模式.

If you're looking to add unit tests to existing code, you may want to look at Working Effectively with Legacy Code by Michael Feathers. Code that wasn't designed with testing in mind may have characteristics that make testing difficult and Feathers writes about ways of carefully refactoring code to make it easier to test. And when you're familiar with certain patterns that make testing code difficult, you and your team can write code that tries to avoid/minimize those patterns.

这篇关于解释单元测试的要点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆