我看到很多问题都在问“如何”用一种特定的语言进行单元测试,但没有人问“什么”、“为什么”和“什么时候”。
是什么? 它对我有什么用? 我为什么要用它? 什么时候用(什么时候不用)? 有哪些常见的陷阱和误解
我看到很多问题都在问“如何”用一种特定的语言进行单元测试,但没有人问“什么”、“为什么”和“什么时候”。
是什么? 它对我有什么用? 我为什么要用它? 什么时候用(什么时候不用)? 有哪些常见的陷阱和误解
当前回答
这回答了为什么您应该进行单元测试。
下面的三个视频涵盖了javascript中的单元测试,但一般原则适用于大多数语言。
单元测试:现在的几分钟将节省几小时后- Eric Mann - https://www.youtube.com/watch?v=_UmmaPe8Bzc
JS单元测试(非常好)- https://www.youtube.com/watch?v=-IYqgx8JxlU
编写可测试的JavaScript - https://www.youtube.com/watch?v=OzjogCFO4Zo
Now I'm just learning about the subject so I may not be 100% correct and there's more to it than what I'm describing here but my basic understanding of unit testing is that you write some test code (which is kept separate from your main code) that calls a function in your main code with input (arguments) that the function requires and the code then checks if it gets back a valid return value. If it does get back a valid value the unit testing framework that you're using to run the tests shows a green light (all good) if the value is invalid you get a red light and you then can fix the problem straight away before you release the new code to production, without testing you may actually not have caught the error.
So you write tests for you current code and create the code so that it passes the test. Months later you or someone else need to modify the function in your main code, because earlier you had already written test code for that function you now run again and the test may fail because the coder introduced a logic error in the function or return something completely different than what that function is supposed to return. Again without the test in place that error might be hard to track down as it can possibly affect other code as well and will go unnoticed.
Also the fact that you have a computer program that runs through your code and tests it instead of you manually doing it in the browser page by page saves time (unit testing for javascript). Let's say that you modify a function that is used by some script on a web page and it works all well and good for its new intended purpose. But, let's also say for arguments sake that there is another function you have somewhere else in your code that depends on that newly modified function for it to operate properly. This dependent function may now stop working because of the changes that you've made to the first function, however without tests in place that are run automatically by your computer you will not notice that there's a problem with that function until it is actually executed and you'll have to manually navigate to a web page that includes the script which executes the dependent function, only then you notice that there's a bug because of the change that you made to the first function.
重申一下,在开发应用程序时运行测试将在编写代码时发现这类问题。如果没有适当的测试,你必须手动检查整个应用程序,即使这样也很难发现错误,你天真地将其发送到生产环境中,一段时间后,好心的用户会给你发送错误报告(这不会像你在测试框架中的错误消息那样好)。
It's quite confusing when you first hear of the subject and you think to yourself, am I not already testing my code? And the code that you've written is working like it is supposed to already, "why do I need another framework?"... Yes you are already testing your code but a computer is better at doing it. You just have to write good enough tests for a function/unit of code once and the rest is taken care of for you by the mighty cpu instead of you having to manually check that all of your code is still working when you make a change to your code.
此外,如果你不想进行单元测试,你也不必进行单元测试,但随着你的项目/代码库开始变得越来越大,引入错误的可能性也在增加,这是值得的。
其他回答
在单元测试和TDD的哲学优势方面,这里有一些关键的“灯泡”观察,这些观察在我试探性地走上TDD启蒙之路的第一步时打动了我(没有原创或一定是新闻)……
TDD does NOT mean writing twice the amount of code. Test code is typically fairly quick and painless to write and is a key part of your design process and critically. TDD helps you to realize when to stop coding! Your tests give you confidence that you've done enough for now and can stop tweaking and move on to the next thing. The tests and the code work together to achieve better code. Your code could be bad / buggy. Your TEST could be bad / buggy. In TDD you are banking on the chances of BOTH being bad / buggy being fairly low. Often its the test that needs fixing but that's still a good outcome. TDD helps with coding constipation. You know that feeling that you have so much to do you barely know where to start? It's Friday afternoon, if you just procrastinate for a couple more hours... TDD allows you to flesh out very quickly what you think you need to do, and gets your coding moving quickly. Also, like lab rats, I think we all respond to that big green light and work harder to see it again! In a similar vein, these designer types can SEE what they're working on. They can wander off for a juice / cigarette / iphone break and return to a monitor that immediately gives them a visual cue as to where they got to. TDD gives us something similar. It's easier to see where we got to when life intervenes... I think it was Fowler who said: "Imperfect tests, run frequently, are much better than perfect tests that are never written at all". I interprete this as giving me permission to write tests where I think they'll be most useful even if the rest of my code coverage is woefully incomplete. TDD helps in all kinds of surprising ways down the line. Good unit tests can help document what something is supposed to do, they can help you migrate code from one project to another and give you an unwarranted feeling of superiority over your non-testing colleagues :)
这篇演讲很好地介绍了测试所需要的所有内容。
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存储库编写钩子,以便在提交更改时自动运行单元测试。
将单元测试保存在与应用程序相同的存储库中是一个很好的实践。
单元测试是关于编写测试应用程序代码的代码。
名称的Unit部分是关于一次测试小单元代码(例如一个方法)的意图。
xUnit是用来帮助测试的——它们是帮助测试的框架。其中一部分是自动测试运行器,它会告诉您哪些测试失败,哪些测试通过。
它们还可以在每个测试之前设置您需要的公共代码,并在所有测试完成后将其删除。
您可以使用一个测试来检查是否抛出了预期的异常,而不必自己编写整个try catch块。
测试驱动开发在某种程度上取代了单元测试这个术语。作为一个老手,我会提到它的更一般的定义。
单元测试还意味着测试一个较大系统中的单个组件。这个组件可以是dll、exe、类库等。它甚至可以是多系统应用程序中的单个系统。因此,单元测试最终是对一个更大系统的单个部分进行测试。
然后,您可以通过测试所有组件如何一起工作来进行集成测试或系统测试。
这回答了为什么您应该进行单元测试。
下面的三个视频涵盖了javascript中的单元测试,但一般原则适用于大多数语言。
单元测试:现在的几分钟将节省几小时后- Eric Mann - https://www.youtube.com/watch?v=_UmmaPe8Bzc
JS单元测试(非常好)- https://www.youtube.com/watch?v=-IYqgx8JxlU
编写可测试的JavaScript - https://www.youtube.com/watch?v=OzjogCFO4Zo
Now I'm just learning about the subject so I may not be 100% correct and there's more to it than what I'm describing here but my basic understanding of unit testing is that you write some test code (which is kept separate from your main code) that calls a function in your main code with input (arguments) that the function requires and the code then checks if it gets back a valid return value. If it does get back a valid value the unit testing framework that you're using to run the tests shows a green light (all good) if the value is invalid you get a red light and you then can fix the problem straight away before you release the new code to production, without testing you may actually not have caught the error.
So you write tests for you current code and create the code so that it passes the test. Months later you or someone else need to modify the function in your main code, because earlier you had already written test code for that function you now run again and the test may fail because the coder introduced a logic error in the function or return something completely different than what that function is supposed to return. Again without the test in place that error might be hard to track down as it can possibly affect other code as well and will go unnoticed.
Also the fact that you have a computer program that runs through your code and tests it instead of you manually doing it in the browser page by page saves time (unit testing for javascript). Let's say that you modify a function that is used by some script on a web page and it works all well and good for its new intended purpose. But, let's also say for arguments sake that there is another function you have somewhere else in your code that depends on that newly modified function for it to operate properly. This dependent function may now stop working because of the changes that you've made to the first function, however without tests in place that are run automatically by your computer you will not notice that there's a problem with that function until it is actually executed and you'll have to manually navigate to a web page that includes the script which executes the dependent function, only then you notice that there's a bug because of the change that you made to the first function.
重申一下,在开发应用程序时运行测试将在编写代码时发现这类问题。如果没有适当的测试,你必须手动检查整个应用程序,即使这样也很难发现错误,你天真地将其发送到生产环境中,一段时间后,好心的用户会给你发送错误报告(这不会像你在测试框架中的错误消息那样好)。
It's quite confusing when you first hear of the subject and you think to yourself, am I not already testing my code? And the code that you've written is working like it is supposed to already, "why do I need another framework?"... Yes you are already testing your code but a computer is better at doing it. You just have to write good enough tests for a function/unit of code once and the rest is taken care of for you by the mighty cpu instead of you having to manually check that all of your code is still working when you make a change to your code.
此外,如果你不想进行单元测试,你也不必进行单元测试,但随着你的项目/代码库开始变得越来越大,引入错误的可能性也在增加,这是值得的。