单元测试还是功能测试?
我最近听说了功能测试而不是单元测试。
我知道单元测试从最原子的形式测试给定代码片段的每种可能性。但是功能测试呢?
在我看来,这听起来像是只测试代码是否有效,但它与单元测试一样可靠吗?
有人告诉我,对于这个问题有两种观点。有些人更喜欢单元测试,其他人更喜欢功能测试。
有没有好的资源、链接、书籍、任何参考资料或你们中的一个人可以解释和启发我在这个主题上的道路?
谢谢!
I have recently heard of Functional Testing over Unit Testing.
I understand that Unit Testing tests each of the possibilities of a given piece of code from its most atomic form. But what about Functional Testing?
This sounds to me like only testing if the code works, but is it as reliable as Unit Testing?
I've been told there was two school of thoughts for the matter. Certains would prefer Unit Testing, others Functional Testing.
Is there any good resources, links, books, any references or one of you all who can explain and elighten my path on the subject?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
单元测试与功能测试不是异或,而是与。单元测试是关于单独测试单元,而功能测试是关于集成测试整体(所有单元是否正常工作?)。
两者都是良好软件工程实践的必要组成部分。
Unit testing versus functional testing is not an
xor
, but rather anand
. Unit testing is about testing units in isolation while functional testing is about testing the whole in integration (do all the units works together properly?).Both are necessary components of good software engineering practices.
杰森的回答是正确的。不同类型的测试有不同的目的,并且可以分层以获得最佳结果(良好的设计、满足规范、减少缺陷)。
这些类别之间有一些重叠;例如,单元测试可以指定行为。
还有其他的;大多数人都想了解更多内容,请参阅软件测试。
人们忽略的一点是,单元测试是独立测试代码片段。例如,好的单元测试不会访问数据库。这有两个优点:它使测试运行得更快,因此您可以更频繁地运行它们,并且它迫使您编写松散耦合的类(更好的设计)。
您请求资源;我推荐 Roy Osherove 的书The Art of Unit Testing with Examples in .NET。虽然没有一本书是完美的,但这本书给出了许多关于编写良好测试的优秀指导。
编辑:对于针对现有软件编写测试,没有什么比 Michael Feathers 的书 有效地使用遗留代码。
Jason's answer is correct. Different types of tests have different purposes, and can be layered for best results (good design, meeting specifications, reduced defects).
There is some overlap between these categories; unit tests can specify behavior, for instance.
And there are others; for more than most people care to know, see Software Testing.
One point people missed is that unit testing is testing pieces of code in isolation. Good unit tests don't hit the database, for instance. This has two advantages: it makes the tests run fast so you'll run them more often, and it forces you to write loosely coupled classes (better design).
You asked for resources; I recommend Roy Osherove's book The Art of Unit Testing with Examples in .NET. While no book is perfect, this one gives many excellent pointers on writing good tests.
EDIT: And for writing tests against existing software, nothing beats Michael Feathers' book Working Effectively with Legacy Code.
单元测试测试您的代码单元(方法等),以确保它们按照您的期望进行。
功能测试测试您的系统设计,以确保各部分正确交互。如果您编写一个接受 int 并返回字符串的命令并对其进行充分测试,则可以确定它有效。但如果您没有进行系统测试,您可能永远不会注意到代码的其余部分认为它可以接受 null,但实际上却不能。
两种类型的测试都很重要。
编辑:要添加与 gbjbaanb 所说的略有不同的视图:
Unit testing tests your code units (methods, etc) to make sure they do what you expect them to.
Functional testing tests your system design to make sure the pieces interact correctly. If you write a command that takes and int and returns a string and test it fully, you can be sure it works. But if you don't have system tests, you may never notice that the rest of the code thinks it can accept a null but it can't.
Both types of testing are important.
edit: To add a slightly different view to what gbjbaanb said:
以上所有内容都很有用,但它们并不相互排斥。你应该做其中的大部分,但你在每个部分上花费的时间取决于你从它们中得到的结果,仅此而已。如果您的代码过于模块化而无法轻松进行单元测试,那么请将精力花在功能测试上。如果您正在编写一个小型组件库,请花时间对它们进行单元测试,如果您正在编写军用导弹的控制系统,您绝对应该对它们进行现场验收测试(因为即使失败时爆炸也很有趣:))
All the above are useful but they're not mutually exclusive. You should be doing most of them but the amount of time you spend on each part depends on the results you get from them, that's all. If your code is too modular to be easily unit tested, then spend your efforts on the functional tests. If you're writing a library of small components, spend your time on unit testing them, and if you're writing control systems for military missiles you should definitely be site acceptance testing them (as explosions even when it fails is fun :) )
功能测试,也称为系统测试,旨在测试整个系统,验证功能需求很满意。
单元测试旨在测试“单元”,即构建系统的函数或方法孤立。有时称为开发人员测试。事后进行单元测试可能会很困难,这就是为什么 TDD 在代码之前编写测试。
这些是互补,因为这些单元可以独立工作,而不是集成在一起时,或者它们可以通过单元测试,但不能满足所有产品要求。
Functional testing, also called System testing, aims at testing the complete system, and verifying the functional requirements are satisfied.
Unit testing aims at testing the "units", i.e. the functions or methods the system is build from in isolation. It's sometimes called Developer testing. Unit testing can be hard after the fact, that's why TDD writes the test before the code.
Those are complementary as the units can work independently and not when integrated all together, or they can pass the unit tests, and not fulfill all the product requirements.
单元测试和功能测试有两种不同的结果。
单元测试验证一小段代码是否按预期工作。它通常由开发人员完成,以确保代码正确运行。它们通常也由测试框架自动化。
功能测试通过程序中的特定路径来验证功能是否按预期工作。它们通常由软件上的人员执行,以确保程序按照用户预期的方式运行。因此,它的级别更高,因此可以同时测试多个单元。
我认为两者都很重要。但是,如果您的资源有限,并且必须选择/选择技术,我认为这取决于您创建的产品,但对于我所做的(人类通过某些按钮使用的汽车控制产品)功能测试是最重要的。它检查并确保当用户获得产品时,它会执行其应该执行的操作。这并不意味着我们应该选择退出单元测试,但如果到了紧要关头,功能性才是最重要的,以确保良好的用户体验并让产品上市。
比如说,如果您生产数据库引擎(或其他不一定面向用户的产品),那么单元测试可能是您真正应该做的。
Unit Testing and Functional Testing have two different results.
Unit Testing verifies that a small piece of code works as expected. It is usually done by the developer to ensure that the code works correctly. They are usually automated by a testing-framework as well.
Functional Testing verifies that a feature works as expected by going through a certain pathway through the program. They are usually executed by a person on the software ensuring that the program will work they way it is supposed to for the user. It, as such, is higher level, and thus tests several units at once.
I think both are important. If you have limited resources, though, and have to pick/choose techniques, and I think it depends on the products you create, but for what I do (automotive control products used by humans through some buttons) functional tests are most important. It checks, and ensures, that when the user gets the product, it does what it is supposed to do. This doesn't mean we should opt out of unit testing, but if push-comes-to-shove, functional is the most important to ensure great user experience and getting the product out the door.
If you produce, say, a database engine (or some other product that isn't necessarily user-facing), unit testing may be what you really ought to do.
单元测试测试一段代码,并向程序员确认另一段代码正在执行其预期的操作。在测试驱动开发中,首先编写单元测试并观察到失败,然后编写代码使测试通过。程序员对单元测试感兴趣。单元测试执行起来很快。
功能测试测试您的黑盒需求并演示一项用户功能是否已到位。例如,如果我按下红色大按钮,铃声就会开始响起。功能测试甚至可能不是测试代码。也许存在一个机械过程,导致按下按钮后铃声响起。客户对功能测试感兴趣,因为他们确认高级流程是否以他们理解的方式工作。他们执行起来往往很慢。
A Unit Test tests a piece of Code and confirms for a programmer that another piece of code is doing what it is supposed to. In Test Driven Development, the unit test is written first and observed to fail, before the code is written causing the test to pass. Programmers are interested in Unit Tests. Unit Test are quick to execute.
A Function Test tests your black box requirement and demonstrates that a piece of user functionality is in place. For example, if I press the big red button, the bell begins to ring. The functional test mightn't even be testing code. Perhaps there is a mechanical process that cause the bell to ring having pressed the button. Customers are interested in Functional Tests as they confirm that a high level process if working in a manner that they understand. They are often slow to execute.
在大多数开发工作中,两者都占有一席之地。
单元测试是为了测试小单元的代码,看看它们是否按预期工作。
功能测试是为了测试系统的整体功能是否符合预期。
它们处于不同的级别,并且都应该使用。
There is a place for both in most development work.
Unit testing is there to test small units of code, to see that they work as expected.
Functional testing is there to test that the overall functionality of the system is as expected.
They are at different levels and both should be used.