我看到很多问题都在问“如何”用一种特定的语言进行单元测试,但没有人问“什么”、“为什么”和“什么时候”。
是什么? 它对我有什么用? 我为什么要用它? 什么时候用(什么时候不用)? 有哪些常见的陷阱和误解
我看到很多问题都在问“如何”用一种特定的语言进行单元测试,但没有人问“什么”、“为什么”和“什么时候”。
是什么? 它对我有什么用? 我为什么要用它? 什么时候用(什么时候不用)? 有哪些常见的陷阱和误解
当前回答
我想推荐Gerard Meszaros的《xUnit测试模式》一书。它很大,但在单元测试方面是一个很好的资源。这里有一个链接到他的网站,在那里他讨论了单元测试的基础知识。http://xunitpatterns.com/XUnitBasics.html
其他回答
如果你想使用Kent Beck推广的TDD方法开发项目,像NUnit、xUnit或JUnit这样的库是必须的:
你可以阅读《测试驱动开发介绍》(TDD)或Kent Beck的《测试驱动开发:举例说明》。
然后,如果你想确保你的测试覆盖了代码中“好的”部分,你可以使用NCover、JCover、PartCover或其他软件。它们会告诉你代码的覆盖率。取决于你对TDD的熟练程度,你会知道你是否已经很好地练习了它:)
如果给你一堆垃圾,你似乎陷入了一种永久的清理状态,你知道任何新功能或代码的添加都会破坏当前的设置,因为当前的软件就像一个纸牌屋,你会怎么做? 那么我们如何进行单元测试呢?
从小事开始。我刚加入的项目直到几个月前才开始进行单元测试。当覆盖率如此之低时,我们将简单地选择一个没有覆盖率的文件并单击“添加测试”。
现在我们已经达到了40%以上,我们已经成功地摘走了大部分容易摘到的果实。
(最好的部分是,即使在这样低的覆盖率水平上,我们已经遇到了许多代码做错误事情的实例,并且测试发现了它。这是促使人们增加更多测试的巨大动力。)
我想推荐Gerard Meszaros的《xUnit测试模式》一书。它很大,但在单元测试方面是一个很好的资源。这里有一个链接到他的网站,在那里他讨论了单元测试的基础知识。http://xunitpatterns.com/XUnitBasics.html
单元测试是在不需要该代码单元所依赖的基础设施的情况下对代码单元(例如单个函数)进行测试。即在隔离环境中进行测试。
例如,如果您正在测试的函数连接到数据库并执行更新,那么在单元测试中您可能不想执行该更新。如果这是一个积分测试,你会这样做,但在这个例子中,它不是。
因此,单元测试将测试您正在测试的“函数”中所包含的功能,而不会产生数据库更新的副作用。
假设函数从数据库中检索了一些数字,然后执行标准偏差计算。你想测试什么?标准偏差计算正确,还是数据从数据库返回?
在单元测试中,您只想测试标准偏差的计算是否正确。在集成测试中,您希望测试标准偏差计算和数据库检索。
这回答了为什么您应该进行单元测试。
下面的三个视频涵盖了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.
此外,如果你不想进行单元测试,你也不必进行单元测试,但随着你的项目/代码库开始变得越来越大,引入错误的可能性也在增加,这是值得的。