你如何判断你的单元测试是正确的? [英] How do you tell that your unit tests are correct?

查看:28
本文介绍了你如何判断你的单元测试是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我职业生涯的各个阶段,我只做过少量的单元测试.每当我再次开始深入研究时,如何证明我的测试是正确的总是困扰着我.我怎么知道我的单元测试中没有错误?通常我最终会运行应用程序,证明它有效,然后使用单元测试作为一种回归测试.推荐的方法是什么和/或您对这个问题采取的方法是什么?

I've only done minor unit testing at various points in my career. Whenever I start diving into it again, it always troubles me how to prove that my tests are correct. How can I tell that there isn't a bug in my unit test? Usually I end up running the app, proving it works, then using the unit test as a sort of regression test. What is the recommended approach and/or what is the approach you take to this problem?

我还意识到您可以编写易于理解的小型、细粒度的单元测试.但是,如果您假设小而细化的代码是完美无瑕的,那么您就可以编写小而细化的程序,而无需进行单元测试.

I also realize that you could write small, granular unit tests that would be easy to understand. However, if you assume that small, granular code is flawless and bulletproof, you could just write small, granular programs and not need unit testing.

Edit2:对于单元测试是为了确保您的更改不会破坏任何内容"和只有当测试与代码具有完全相同的缺陷时才会发生这种情况"的论点,如果测试过度拟合怎么办?有可能通过一个糟糕的测试来通过好的和坏的代码.我的主要问题是单元测试有什么好处,因为如果你的测试有缺陷,你就不能真正提高你对代码的信心,不能真正证明你的重构有效,也不能真正证明你符合规范?

For the arguments "unit testing is for making sure your changes don't break anything" and "this will only happen if the test has the exact same flaw as the code", what if the test overfits? It's possible to pass both good and bad code with a bad test. My main question is what good is unit testing since if your tests can be flawed you can't really improve your confidence in your code, can't really prove your refactoring worked, and can't really prove that you met the specification?

推荐答案

单元测试应该表达您正在测试的任何内容的契约".它或多或少是放入代码的单元的规范.因此,根据规范,单元测试是否正确"应该或多或少显而易见.

The unit test should express the "contract" of whatever you are testing. It's more or less the specification of the unit put into code. As such, given the specs, it should be more or less obvious whether the unit tests are "correct".

但我不会太担心单元测试的正确性".它们是软件的一部分,因此,它们也很可能是不正确的.单元测试的重点 - 从我的观点来看 - 是它们确保您的软件的合同"不会意外被破坏.这就是单元测试如此有价值的原因:您可以在软件中进行挖掘,重构某些部分,更改其他部分的算法,并且您的单元测试会告诉您是否破坏了任何东西.即使不正确单元测试也会告诉你这一点.

But I would not worry too much about the "correctness" of the unit tests. They are part of the software, and as such, they could well be incorrect as well. The point of unit tests - from my POV - is that they ensure the "contract" of your software is not broken by accident. That is what makes unit tests so valuable: You can dig around in the software, refactor some parts, change the algorithms in others, and your unit tests will tell you if you broke anything. Even incorrect unit tests will tell you that.

如果您的单元测试中存在错误,您会发现 - 因为单元测试失败,而被测试的代码却是正确的.那么,修复单元测试.没什么大不了的.

If there is a bug in your unit tests, you will find out - because the unit test fails while the tested code turns out to be correct. Well then, fix the unit test. No big deal.

这篇关于你如何判断你的单元测试是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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