我看到很多问题都在问“如何”用一种特定的语言进行单元测试,但没有人问“什么”、“为什么”和“什么时候”。
是什么? 它对我有什么用? 我为什么要用它? 什么时候用(什么时候不用)? 有哪些常见的陷阱和误解
我看到很多问题都在问“如何”用一种特定的语言进行单元测试,但没有人问“什么”、“为什么”和“什么时候”。
是什么? 它对我有什么用? 我为什么要用它? 什么时候用(什么时候不用)? 有哪些常见的陷阱和误解
当前回答
This is my take on it. I would say unit testing is the practice of writing software tests to verify that your real software does what it is meant to. This started with jUnit in the Java world and has become a best practice in PHP as well with SimpleTest and phpUnit. It's a core practice of Extreme Programming and helps you to be sure that your software still works as intended after editing. If you have sufficient test coverage, you can do major refactoring, bug fixing or add features rapidly with much less fear of introducing other problems.
当所有单元测试都能自动运行时,这是最有效的。
单元测试通常与OO开发相关联。基本思想是创建一个脚本,为你的代码设置环境,然后练习它;您编写断言,指定您应该接收的预期输出,然后使用如上所述的框架执行您的测试脚本。
框架将针对您的代码运行所有测试,然后报告每个测试的成功或失败。phpUnit默认情况下从Linux命令行运行,尽管有HTTP接口可用于它。SimpleTest本质上是基于web的,在我看来,它更容易启动和运行。结合xDebug, phpUnit可以为您提供代码覆盖率的自动统计数据,有些人认为这非常有用。
有些团队从他们的subversion存储库编写钩子,以便在提交更改时自动运行单元测试。
将单元测试保存在与应用程序相同的存储库中是一个很好的实践。
其他回答
首先,无论是谈论单元测试还是其他类型的自动化测试(集成、加载、UI测试等),与您所建议的关键区别在于它是自动化的、可重复的,并且不需要消耗任何人力资源(=没有人需要执行测试,它们通常按下按钮就可以运行)。
单元测试与“只是打开一个新项目并测试这个特定的代码”的主要区别在于它是自动化的,因此是可重复的。
如果您手动测试代码,它可能会使您相信代码在当前状态下工作得很完美。但一周后,当你对它做了轻微的修改时呢?您是否愿意在代码发生任何更改时再次手工重新测试?很可能不会:-(
但是,如果您可以在任何时间运行您的测试,只需单击一下,以完全相同的方式,在几秒钟内,那么一旦有什么东西坏了,它们就会立即显示给您。如果您还将单元测试集成到您的自动化构建过程中,即使在看似完全无关的更改破坏了代码库中遥远部分的某些情况下,它们也会提醒您错误——当您甚至不会想到需要重新测试特定的功能时。
这是单元测试相对于手工测试的主要优势。但等等,还有更多:
unit tests shorten the development feedback loop dramatically: with a separate testing department it may take weeks for you to know that there is a bug in your code, by which time you have already forgotten much of the context, thus it may take you hours to find and fix the bug; OTOH with unit tests, the feedback cycle is measured in seconds, and the bug fix process is typically along the lines of an "oh sh*t, I forgot to check for that condition here" :-) unit tests effectively document (your understanding of) the behaviour of your code unit testing forces you to reevaluate your design choices, which results in simpler, cleaner design
反过来,单元测试框架使您更容易编写和运行测试。
我不反对丹的观点(尽管不回答可能是更好的选择)……但是……
单元测试是编写代码来测试系统的行为和功能的过程。
显然,测试可以提高代码的质量,但这只是单元测试的表面好处。真正的好处是:
Make it easier to change the technical implementation while making sure you don't change the behavior (refactoring). Properly unit tested code can be aggressively refactored/cleaned up with little chance of breaking anything without noticing it. Give developers confidence when adding behavior or making fixes. Document your code Indicate areas of your code that are tightly coupled. It's hard to unit test code that's tightly coupled Provide a means to use your API and look for difficulties early on Indicates methods and classes that aren't very cohesive
你应该进行单元测试,因为向客户交付可维护的高质量产品符合你的利益。
我建议您将它用于任何系统,或系统的一部分,以模拟真实世界的行为。换句话说,它特别适合于企业开发。我不会将它用于一次性/实用程序。我不会用它来测试系统中有问题的部分(UI是一个常见的例子,但并不总是这样)
最大的陷阱是开发人员测试太大的单元,或者他们认为一个方法是一个单元。如果您不理解控制反转,这一点尤其正确——在这种情况下,您的单元测试总是会变成端到端集成测试。单元测试应该测试单个行为——大多数方法都有许多行为。
最大的误解是程序员不应该测试。只有糟糕或懒惰的程序员才会相信这一点。给你盖屋顶的人不应该测试吗?更换心脏瓣膜的医生不应该检查新瓣膜吗?只有程序员才能测试他的代码是否按照他的意图去做(QA可以测试边缘情况——当代码被告知要做程序员不打算做的事情时,它的行为如何,而客户可以进行验收测试——代码是否按照客户的要求去做)
I think the point that you don't understand is that unit testing frameworks like NUnit (and the like) will help you in automating small to medium-sized tests. Usually you can run the tests in a GUI (that's the case with NUnit, for instance) by simply clicking a button and then - hopefully - see the progress bar stay green. If it turns red, the framework shows you which test failed and what exactly went wrong. In a normal unit test, you often use assertions, e.g. Assert.AreEqual(expectedValue, actualValue, "some description") - so if the two values are unequal you will see an error saying "some description: expected <expectedValue> but was <actualValue>".
总之,单元测试将使测试更快,对开发人员来说更舒服。您可以在提交新代码之前运行所有的单元测试,这样就不会中断同一项目中其他开发人员的构建过程。
使用Testivus。你需要知道的都在那里:)