Unit Test Conventions

2018-05-01
  1. A testing unit should focus on one tiny bit of functionality and prove it correct.

    • 单元测试应该关注某一个函数功能细节,并且验证函数功能呢正常。
  2. Each test unit must be fully independent. Each test must be able to run alone, and also within the test suite, regardless of the order that they are called. The implication of this rule is that each test must be loaded with a fresh dataset and may have to do some cleanup afterwards. This is usually handled by setUp() and tearDown() methods.

    • 每一个测试单元必须要是完全独立的。每个测试需要能单独运行,并且,无论在测试套件内以任何顺序执行,都应该运行正常。这意味着每一个测试必须要再入一个新的数据集并且在测试完成吼要做一些清除(回滚)。通常,通过 setUp() 和 tearDown() 方法来完成。
  3. Try hard to make tests that run fast. If one single test needs more than a few milliseconds to run, development will be slowed down or the tests will not be run as often as is desirable. In some cases, tests can’t be fast because they need a complex data structure to work on, and this data structure must be loaded every time the test runs. Keep these heavier tests in a separate test suite that is run by some scheduled task, and run all other tests as often as needed.

    • 尽力让测试运行得快一些。如果某个测试需要需要花费很长的时间,会影响开发的速度,或者不被经常运行。有时候,测试由于复杂的数据结构,而且这个数据结构必须每次都被载入并测试运行,导致无法快速执行。保留好这个重量级的测试在另一个测试套件中,然后以另外的执行计划运行。尽量多地运行其他的测试。
  4. Learn your tools and learn how to run a single test or a test case. Then, when developing a function inside a module, run this function’s tests frequently, ideally automatically when you save the code.

    • 学习如何单独运行一个测试或者测试用例。在开发一个模块里的函数时,多运行这个函数的测试。最好是能在你保存代码的时候自动测试。
  5. Always run the full test suite before a coding session, and run it again after. This will give you more confidence that you did not break anything in the rest of the code.

    • 最好在每个编程阶段开始和结束的时候运行完整的测试套件,这样做能够帮助你不损坏代码的其他部分。
  6. It is a good idea to implement a hook that runs all tests before pushing code to a shared repository.

    • 实现一个hook,在将代码推送到公共分支之前,来运行所有测试是个不错的主意。
  7. If you are in the middle of a development session and have to interrupt your work, it is a good idea to write a broken unit test about what you want to develop next. When coming back to work, you will have a pointer to where you were and get back on track faster.

    • 如果开发到一半时不得不中断工作,可以写一个不完全测试来检验已实现的的部分。回到工作时,你就能清楚地知道自己已经做到哪里,要如何快速地继续开始。
  8. The first step when you are debugging your code is to write a new test pinpointing the bug. While it is not always possible to do, those bug catching tests are among the most valuable pieces of code in your project.

    • debug的第一步是写一个新的测试来定位bug。尽管这样并不总是可行的,这些bug定位代码将是你项目中最有价值的代码。
  9. Use long and descriptive names for testing functions. The style guide here is slightly different than that of running code, where short names are often preferred. The reason is testing functions are never called explicitly. square() or even sqr() is ok in running code, but in testing code you would have names such as test_square_of_number_2(), test_square_negative_number(). These function names are displayed when a test fails, and should be as descriptive as possible.

    • 尽量用长一点的、自描述的测试函数名。测试代码和偏好短小名称、经常运行的代码不同。测试函数不是总被精确地调用。Square() 或者 sqr() 再运行的时候没有问题,但是在测试的时候,你最好使用诸如 test_square_of_number_2(), test_square_negative_number() 这样的名称。当测试失败的时候,这些函数名称会被显示出来,因此他们最好能是描述性的。
    • 长函数名虽然会使代码看起来比较厚重,但是因为这不是业务代码而是带有一点点文档性质的东西,一点小小的平衡是值得的。这和第十条是相互补充的。代码的逻辑应该足够清晰、简洁,函数名称则倾向于拥有较强的描述性,清楚地表达意图。
  10. When something goes wrong or has to be changed, and if your code has a good set of tests, you or other maintainers will rely largely on the testing suite to fix the problem or modify a given behavior. Therefore the testing code will be read as much as or even more than the running code. A unit test whose purpose is unclear is not very helpful in this case.

    • 如果某些代码出错或者被修改,而这些代码有很好的测试集,你和其他的维护人员可以依赖这些测试套件来修补这些问题或者修改某些行为(程序性的)。因此,测试代码会比运实现代码被更频繁地查看。单元测试如果目的不清晰,在这种情况就没有什么卵用。
  11. Another use of the testing code is as an introduction to new developers. When someone will have to work on the code base, running and reading the related testing code is often the best thing that they can do to start. They will or should discover the hot spots, where most difficulties arise, and the corner cases. If they have to add some functionality, the first step should be to add a test to ensure that the new functionality is not already a working path that has not been plugged into the interface.

    • 测试代码还能用来指导新来的开发人员。查看、运行相关测试代码通常是开始一个项目最好的办法。他们能通过这个了解通常在什么地方会遇到麻烦,还有了解到边界情况。如果开发人员要添加一些功能,第一步要做的就是添加一些测试来确保新的功能尚未被添加到接口中。

girl

..-. . -.