Unit testing gives you some certainty that you code meets your expectations. Unit testing is not a holy grail, it is simply one of the tools you can use to attain a good level of accuracy and modularity with your code.
The unit test is your expectation. In other words, your test will call or invoke the piece of code that is under test, and then measure the results (return values, etc) against what you expected to happen (or be returned). If the results don't match what you expected, then you have either got a faulty test (your expectation was wrong), or the code under test is not operating as you thought.
Unit testing is especially useful when it comes time for refactoring or maintaining your code. You can change code, run the unit tests, and check to see if your changes have broken the tests.
To keep it brief, unit tests do have other benefits as well. Normally you should be able to run them in an automated way, so you can have them run if you do a nightly build, then you can check the results in the morning. This is especially useful in larger teams when multiple people can be changing or adding code. Constructing your code in a way that is suitable for unit testing also leads to more of a decoupled approach when building your app, this reduces code smell and helps with the future maintainability and extensibility of it.
Edit:
With testing did_earn, do I hard code in test cases. For example, would I manually pass user5 as a param (user5 has logged in 400 times) and make sure that they do not get the badge, but user6 (500 logins) does get the badge?
Yes, that is exactly what you will do. This means that if you change the internal implementation of did_earn then your tests can tell you immediately whether it still meets your expectation (that it only returns true from 500 logins onwards). If you are testing code that is not strongly typed (like a scripting language) then you should also consider testing for what happens if you pass a string to the function instead of an integer, etc.
While it may not apply to you at the moment, you should also check out the concept of mocking, IoC and Dependency Injection, which are all techniques used heavily in unit testing.
发布评论
评论(1)
单元测试可以让您确定您的代码是否满足您的期望。单元测试不是圣杯,它只是您可以用来使代码达到良好的准确性和模块化程度的工具之一。
单元测试是您的期望。换句话说,您的测试将调用或调用正在测试的代码段,然后根据您预期发生(或返回)的结果来衡量结果(返回值等)。如果结果与您的期望不符,那么您要么得到了错误的测试(您的期望是错误的),要么被测试的代码没有按照您的想法运行。
当需要重构或维护代码时,单元测试特别有用。您可以更改代码,运行单元测试,并检查您的更改是否破坏了测试。
简而言之,单元测试还有其他好处。通常,您应该能够以自动方式运行它们,因此如果您进行夜间构建,则可以让它们运行,然后您可以在早上检查结果。当多人可以更改或添加代码时,这在大型团队中特别有用。
以适合单元测试的方式构建代码还会在构建应用程序时带来更多的解耦方法,这会减少代码气味并有助于其未来的可维护性和可扩展性。
编辑:
是的,这正是您要做的。这意味着,如果您更改
did_earn
的内部实现,那么您的测试可以立即告诉您它是否仍然满足您的期望(从 500 次登录开始,它仅返回 true)。如果您正在测试非强类型的代码(例如脚本语言),那么您还应该考虑测试如果将字符串而不是整数传递给函数会发生什么情况等。虽然它目前可能不适用于您,您还应该查看 mocking 的概念,IoC 和 依赖注入,这些都是单元测试中大量使用的技术。
Unit testing gives you some certainty that you code meets your expectations. Unit testing is not a holy grail, it is simply one of the tools you can use to attain a good level of accuracy and modularity with your code.
The unit test is your expectation. In other words, your test will call or invoke the piece of code that is under test, and then measure the results (return values, etc) against what you expected to happen (or be returned). If the results don't match what you expected, then you have either got a faulty test (your expectation was wrong), or the code under test is not operating as you thought.
Unit testing is especially useful when it comes time for refactoring or maintaining your code. You can change code, run the unit tests, and check to see if your changes have broken the tests.
To keep it brief, unit tests do have other benefits as well. Normally you should be able to run them in an automated way, so you can have them run if you do a nightly build, then you can check the results in the morning. This is especially useful in larger teams when multiple people can be changing or adding code.
Constructing your code in a way that is suitable for unit testing also leads to more of a decoupled approach when building your app, this reduces code smell and helps with the future maintainability and extensibility of it.
Edit:
Yes, that is exactly what you will do. This means that if you change the internal implementation of
did_earn
then your tests can tell you immediately whether it still meets your expectation (that it only returns true from 500 logins onwards). If you are testing code that is not strongly typed (like a scripting language) then you should also consider testing for what happens if you pass a string to the function instead of an integer, etc.While it may not apply to you at the moment, you should also check out the concept of mocking, IoC and Dependency Injection, which are all techniques used heavily in unit testing.