简体   繁体   English

是否有人对正式的单元测试的实用性有度量?

[英]Does anyone have metrics on the utility of formal Unit Testing?

Does anyone have metrics on the utility of formal Unit Testing? 是否有人对正式的单元测试的实用性有度量? I see a lot of attention being paid to unit testing tools and I was curious why? 我看到很多对单元测试工具的关注,我很好奇为什么?

I stopped formal unit testing over 5 or 6 years ago and the net gain in productivity seems quite high. 我在5到6年前就停止了正式的单元测试,生产率的净收益似乎很高。 I stopped unit testing because I noticed that it never caught anything - let alone anything useful. 我停止了单元测试,因为我注意到它从来没有捕获任何东西-更不用说任何有用的东西了。 The type of errors that unit testing detects seem to be preventable by not drinking more than 2 glasses of wine/beer per hour (or 2 joints per hour). 通过每小时不喝超过2杯葡萄酒/啤酒(或每小时不超过2个关节),似乎可以预防单元测试检测到的错误类型。 Also - unit testing seems to create risk by allowing the developer to think that there is some safeguard to catch their mistakes. 另外-单元测试似乎通过允许开发人员认为存在一些可以掩饰其错误的保护措施而带来了风险。

I do test to ensure that the code works as it should, but I do not use any tools. 我进行测试以确保代码能够正常工作,但是我不使用任何工具。 I test based on the changes being made. 我根据所做的更改进行测试。 My production error rate for new code is approximately zero. 新代码的生产错误率大约为零。 My error rate for changes to code is about 2-3 bugs per quarter. 我对代码更改的错误率大约是每季度2-3个错误。 The above measures are based on 4 production applications that I develop/support. 以上措施基于我开发/支持的4种生产应用程序。

I acknowledge your superiority as human being and a coder. 我承认您是人类和编码者的优势。

I, however, am a mere moron, and without Python unittest, I would be lost. 但是,我只是个白痴,没有Python单元测试,我会迷路的。

I cannot refactor without unit tests, it just takes too much thinking. 没有单元测试,我无法重构,这需要太多的思考。

I can barely code without unit tests, it's too hard to be absolutely sure I absolutely understand absolutely every nuance. 没有单元测试,我几乎无法编写代码,很难绝对确定我绝对理解每一个细微差别。

I unit test because I'm an idiot. 我是白痴,所以进行单元测试。 Since you don't make mistakes, you clearly don't need to unit test. 由于您不会犯错误,因此您显然不需要进行单元测试。 I salute you. 我向你们致敬。


Edit. 编辑。 For me, unit tests aren't about metrics or costs. 对我而言,单元测试与指标或成本无关。 I don't need any randomized, controlled experiments to show me the value. 我不需要任何随机的,受控的实验就可以显示出它的价值。 I cannot work without them. 没有他们,我无法工作。 Indeed, I refuse to work without them. 确实,我拒绝没有他们的工作。 In a similar vein, I won't work without a compiler, a text editor, or source code control; 同样,如果没有编译器,文本编辑器或源代码控件,我将无法工作。 I won't work without requirements; 没有要求,我不会工作; I refuse to program without doing design first. 我拒绝不先做设计就编程。

I do not see unit testing as a replacement for traditional testing, but rather as an extra step to ensure correctness of code. 我不认为单元测试可以替代传统测试,而可以作为确保代码正确性的额外步骤。 Some particular areas where I find unit testing useful are: 我发现单元测试有用的一些特定领域是:

  • When refactoring/changing existing code. 重构/更改现有代码时。 Unit tests will verify that at least those cases still work as expected. 单元测试将验证至少那些情况仍然可以按预期进行。 The more tests you have, the more sure you can be that the code changes did not break existing code. 测试次数越多,您就越有可能确定代码更改不会破坏现有代码。
  • When submitting bug reports. 提交错误报告时。 Having a unit test which exposes a bug is a great way of demonstrating the bug AND knowing when it has been fixed. 进行暴露一个错误的单元测试是演示该错误并知道何时修复该错误的好方法。
  • A means of designing interfaces. 一种设计接口的方法。 You have some test code to check the interfaces out with. 您有一些测试代码可以用来检查接口。

Probably a few others I've forgotten about :-P 我可能已经忘记了一些其他问题:-P

PS: How do you know you make no bugs? PS:您如何知道自己没有错误? I don't think that I introduce bugs into code I work on, but that certainly doesn't make it so. 我不认为我会在所处理的代码中引入错误,但事实并非如此。 IMHO, it is naive to think that your code is bug free. 恕我直言,认为您的代码没有错误是天真的。

(Regarding unit testing, if you know your code may contain bugs - and I would say most code does - wouldn't you want to use every possible means to catch them?) (关于单元测试,如果您知道您的代码可能包含错误-我会说大多数代码都包含-您是否不想使用一切可能的方法来捕获它们?)

Here is some White Paper about Unit Test that might help you: 这是一些有关单元测试的白皮书,可能会对您有所帮助:

But, Martin Fowler put it, the anecdotal evidence in support of unit tests and TDD is overwhelming, but you cannot measure productivity. 但是, 马丁·福勒Martin Fowler)认为,支持单元测试和TDD的轶事证据是压倒性的,但您无法衡量生产率。

Unit testing is good because you can change a part and know if somewhere else it has modified something. 单元测试很好,因为您可以更改零件并知道它是否在其他地方进行了修改。 Some people are "in love" with Unit Testing and should calm theirselve. 有些人“爱上了”单元测试,应该让自己平静下来。 I believe in Unit Testing but people who try to covert everything are AS dangerous of people who do not unit test. 我相信单元测试,但是那些试图隐瞒一切的人对于没有单元测试的人来说是危险的。

I don't have any metrics to point at, but I think the rise in popularity is because the rest of us have had experience that's the opposite of yours. 我没有任何指标要指出,但是我认为受欢迎程度的上升是因为我们其他人的经验与您相反。 :) :)

Here is a thread that has some research about the TDD approach Research on TDD 这是一个有关TDD方法的研究线索

Is there hard evidence of the ROI of unit testing? 是否有确凿的证据证明单元测试的投资回报率?

With unit tests, I can fix bugs in production code and install the new version within the hour the bug was found and I can be sure that the new version isn't worse than what we had before - because the tests tell me so. 通过单元测试,我可以修复生产代码中的错误,并在发现错误的一小时内安装新版本,并且可以确定新版本不会比我们以前的版本差-因为测试可以告诉我。 It might be better, though. 不过可能会更好。

They give me a lower watermark below which the quality of my code can never sink. 它们给了我较低的水印,低于此水印我的代码质量永远不会下降。 They allow me to keep track of the bigger picture and have the tests find the small mistakes that I tend to make. 它们使我能够跟踪大局,并让测试发现我容易犯的小错误。 They also allow me to develop in a very relaxed style. 它们也使我以轻松的方式发展。

Since I test, I tend to deliver on time, my code quality has improved a lot and the result usually works as expected. 自测试以来,我倾向于按时交付,我的代码质量得到了很大改善,并且结果通常可以按预期工作。 Also, I'm much faster since I can cut corners which would be too dangerous to try if I didn't have the tests. 此外,我的速度也快得多,因为我可以偷工减料,如果没有测试,尝试将非常危险。

That said, I also don't have any hard numbers nor do I know any source despite the fact that I'm doing unit test and TDD for years. 就是说,尽管我多年来一直在进行单元测试和TDD,但我也没有任何硬性数字,也不知道任何消息来源。 My love for tests is based on pure word of mouth and personal experience. 我对测试的热爱是基于纯粹的口口相传和个人经验。

I've found that unit testing helps me when adding new functionality. 我发现添加新功能时,单元测试可以帮助我。 In this scenario I used to worry that what I was adding was going to break something in some remote part of the application. 在这种情况下,我曾经担心添加的内容会破坏应用程序某些远程部分的内容。 But with appropriate unit tests I know whether or not I've broken something the moment I run the tests. 但是通过适当的单元测试,我知道在运行测试的那一刻是否已经损坏了某些东西。

Here's an interesting discussion on the utility of unit tests. 这是关于单元测试实用性的有趣讨论

If you don't like unit tests, another concept you might want to look into is Design By Contract. 如果您不喜欢单元测试,则可能要考虑的另一个概念是“ 按合同设计”。 Which basically asserts that if certain input conditions are met then there will be a guaranteed output according to the contract. 它基本上断言,如果满足某些输入条件,则根据合同将有保证的输出。

I'm a development manager. 我是开发经理。 For my organization, setting up and migrating to nhibernate involved some setup costs and added to our development time. 对于我的组织而言,设置和迁移到Nhibernate涉及一些设置成本,并增加了我们的开发时间。 Some of the developers liked it, some thought it was a waste of time. 有些开发人员喜欢它,有些认为这是浪费时间。

There hasn't been a noticeable change in error rates, but perhaps it's too early to tell. 错误率没有明显变化,但现在还为时过早。

From my perspective, I think it helps junior developers who aren't sure of their work, but for the senior developers, it seems to slow them down - it's one more thing to keep updated. 从我的角度来看,我认为它可以帮助不确定其工作的初级开发人员,但是对于高级开发人员而言,它似乎使他们的速度变慢了-保持更新是一回事。 I'm not sure if we'll continue using this, revert back to our old ways (ad hoc unit testing), or let developers make a personal choice. 我不确定我们是否会继续使用它,恢复到以前的方式(临时单元测试),还是让开发人员做出个人选择。

There are several tools that measure the code coverage with unit tests. 有几种工具可用于测试单元测试的代码覆盖率。 They are an essential part together with unit test to ensure the code is not only tested, but completly tested. 它们与单元测试一起是必不可少的部分,以确保不仅对代码进行了测试,而且对它们进行了完整的测试。

Everything else is just pure magic ;) 其他一切都只是纯魔术;)

If you want to refactor code, by definition you need some way of telling if the changes broke the code. 如果要重构代码,按照定义,您需要某种方法来判断更改是否破坏了代码。 Lacking divine insight, I find that unit testing is a pretty good tool, but ymmv. 缺乏神圣的见解,我发现单元测试是一个非常好的工具,但是非常好。

I've specifically had a lot of gain using Test Driven Development (TDD) with C++ on a huge monolithic server application. 我特别在大型的单片服务器应用程序上使用带有C ++的测试驱动开发(TDD)获得了很多收益。

When I'm working on an area of code, I first ensure that that area is covered with tests before I change or write new code. 在处理代码区域时,在更改或编写新代码之前,我首先要确保该区域已被测试覆盖。

In this use case I have huge gains in productivity. 在这种用例中,我在生产率方面有巨大的收获。

  • To build and run my test: 10 seconds. 建立并运行我的测试:10秒。
  • To build, run and manually test the full server: min 5 minutes. 要构建,运行并手动测试整个服务器:最少5分钟。

This means I'm able to iterate an area of code very quickly and only test fully when I need to. 这意味着我能够非常快速地迭代一段代码,并且仅在需要时才进行全面测试。

In addition to this I have also utilised integration testing which take longer to build and run, but only exercise the specific piece of functionality I am interested in. 除此之外,我还利用了集成测试,该测试花费了较长的构建和运行时间,但仅使用了我感兴趣的特定功能。

I have some sympathy for what you're saying. 我对你的意思表示同情。 My experience is similar in that my new bugs are seldom caught by unit tests. 我的经验相似,因为我的新错误很少被单元测试捕获。 If at all, the unit tests are modified after the bug has been found to ensure that it doesn't reappear. 如果有的话,在发现错误之后,将对单元测试进行修改,以确保它不会再次出现。

Where unit tests have helped me is in the design of my (Java) classes. 单元测试对我有帮助的地方是我的(Java)类的设计。 I have often refactored the classes to make them testable (removal of singletons for example) which, I think, has improved the overall design. 我经常重构类以使其可测试(例如删除单例),我认为这改善了总体设计。 For me, that's reason enough to use them. 对我来说,这足以使用它们。

In my experience Unit Testing helped me with the following stuff : 以我的经验,单元测试帮助我完成了以下工作:

  • Now I can give all of my focus to the code block / function / class that I'm writing without worrying about anything else, because I know If I do something stupid or cause a side effect my tests will tell me 现在,我可以将所有精力放在正在编写的代码块/函数/类上,而不必担心其他任何事情,因为我知道如果我做一些愚蠢的操作或引起副作用,我的测试就会告诉我
  • I can refactor stuff by knowing that I'm not breaking stuff 我可以通过知道自己没有破坏东西来重构东西
  • I'm sure that my app is working as expected 我确定我的应用程序可以正常运行
  • Before a release I don't check every single functionality manually to confirm that app still works, 在发布之前,我不会手动检查每一项功能以确认该应用程序仍能正常运行,
  • I'm sure my application is always stable in some level 我确定我的应用程序一定程度上一直稳定

However this is a bit related with me since I'm really bad about "managing multiple stuff at a time" and "focusing". 但是,这与我有点关系,因为我对“一次管理多个内容”和“集中精力”确实很不好。 Therefore Unit Testing works for me and literally save my day so many times, where I introduced a new feature and it broke some old functionality. 因此,单元测试为我工作,实际上节省了我很多时间,在此我引入了一项新功能,但它破坏了一些旧功能。

If this is not the case for you just don't use it. 如果不是这种情况,那就不要使用它。 If you are still having the same outcome, performance and quality with the same amount of bugs then that means you don't need unit testing. 如果您仍然具有相同的结果,性能和质量以及相同数量的错误,那么这意味着您不需要单元测试。 Or you need to revise your unit testing methodologies. 或者您需要修改单元测试方法。

PS Based on your bug rate and what you've said you sound like a genius anyway (assuming these are medium or big projects), so I'd say don't use Unit Tests, it looks like you are doing fine. PS根据您的错误率以及您所说的听起来还是个天才(假设这些是中型或大型项目),因此我要说不要使用单元测试,看来您做得不错。 But for the rest of the world who are not genius such as me I strongly recommend Unit Test because it worked for me. 但是对于像我这样的天才以外的世界其他地区,我强烈推荐单元测试,因为它对我有用。

Dunno about you, but I've just checked in two fixes for two coredumps created by changes in existing code that my unit tests caught. Dunno关于您,但是我刚刚检查了两个修复程序,以解决由于单元测试捕获的现有代码更改而创建的两个核心转储。 And I just had a major production issue that would have been caught by a unit test if I had more trust in its results (our unittests are a bit more on a functional side than I'd like to admit). 如果我对结果有更多的信任,我只是遇到了一个重大的生产问题,而单元测试可能会抓住这个问题(我们的单元测试在功能方面比我想承认的要多得多)。

It seems to me that formal Unit Testing with the popular testing tools is pretty much like US airport security. 在我看来,使用流行的测试工具进行的正式单元测试与美国机场安全非常相似。

  • It provides the illusion of security 它提供了安全的错觉
  • It makes people 'feel' good 它使人们“感觉”良好
  • It's very inconvenient (extremely inconvenient if you got the wrong color skin) 这非常不方便(如果您弄错了肤色,则非常不便)
  • People will angrily wave their fist at you if you criticise it... 如果您批评它,人们会生气地向您挥动拳头。
  • These same people will be left befuddled when their process fails them and they will jump on the next band wagon... 当他们的过程失败时,这些相同的人将被迷惑,他们将跳上下一辆旅行车...

I think people have different perspectives on software. 我认为人们对软件有不同的看法。 In my opinion, software is a means of making money (hopefully by providing increased revenues or saving money). 在我看来,软件是一种赚钱的手段(希望通过增加收入或节省金钱)。 I've seen the posts for TDD which is the closest I see as a scientific way to answer the question, but the methodology lacks scientific rigor. 我看过有关TDD的帖子,这是我认为是最科学的回答问题的方法,但该方法缺乏科学严谨性。 None of the articles specified had a baseline or fairly contrasted alternative method. 指定的文章都没有基准或相当对比的替代方法。

I guess, the fans of formal unit testing will continue to feel secure in their ways. 我想,正式单元测试的拥护者将继续以自己的方式感到安全。 I will continue to write my spec on a scrap of paper and put in the bowl at the feet of my statue of St. Anthony and say a prayer. 我将继续将规格写在纸片上,放在我的圣安东尼雕像脚下的碗里,并祈祷。 Who is to say which way is more effective, but my way sure feels good... Maybe I'll write a white paper about it. 谁说哪种方法更有效,但是我的方法肯定不错……也许我会写一份白皮书。

Remember the rise in popularity of 70's and 80's haircuts and clothes... that didn't work out so well for those of us who lived in those decades. 请记住,70年代和80年代的理发和衣服的流行度上升……对于那些生活在那几十年中的我们来说,这并不是一件好事。

Formal unit testing takes considerable work and effort to maintain. 正式的单元测试需要大量的工作和维护工作。 I'd guess that it takes 20-50% of the time it takes to actually develop the software. 我猜想,实际开发软件需要20-50%的时间。 What I'm asking is for the known price of adding 20-50% overhead to every development effort, is the gain noteworthy and/or proveable. 我要问的是每项开发工作都要增加20-50%开销的已知价格,这是值得注意的和/或可证明的。

By not doing formal unit testing, you are forcing the developer to think through the appropriate things to test. 通过不进行正式的单元测试,您将迫使开发人员考虑进行适当的测试。 The developer takes more ownership of the product. 开发人员获得了产品的更多所有权。

Formal Unit testing sounds like snake oil juice... Everyone and his brother say it is good, useful, cool, etc., but there has not been a randomized controlled trial to prove that it actually saves time or money. 正式的单元测试听起来像蛇油汁……每个人和他的兄弟都说它很好,有用,很酷,等等,但是还没有一项随机对照试验来证明它确实节省了时间或金钱。 All the responses thus far are subjective testimonies. 到目前为止,所有答复均为主观证词。

What I'd like to know is if there is a software manager who can demonstrate higher productivity (or even higher quality) after the introduction of unit testing. 我想知道的是,在引入单元测试后,是否有一个软件经理可以证明更高的生产率(甚至更高的质量)。

lol - the facetious rant by S.Lott is the highest ranked response... Given this forum is anonymous (for me anyway), your respect is not what I'm seeking. 大声笑-S.Lott的恶作剧是获得最高评价的响应...鉴于该论坛是匿名的(无论如何对我来说),您的尊敬不是我想要的。 I'd consider myself barely above mediocre. 我认为自己勉强能做到平庸。 I've worked with exceptional developers... those guys generally don't even do basic tests of their code - they just know it will work. 我曾与杰出的开发人员一起工作过……这些家伙通常甚至不对其代码进行基本测试-他们只知道它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM