官术网_书友最值得收藏!

Understanding TDD

Now that we have seen what unit tests are and how they can help in development, we are going to learn about TDD.

In 1996, Kent Beck introduced a new software development methodology called Extreme Programming. The word Extreme indicates that the concepts behind Extreme Programming are totally different from the concepts used in software development back then. For many people, these concepts sound extreme even today.

The methodology is based on 12 rules or practices. One of the rules states that developers have to write unit tests and all parts of the software have to be thoroughly tested. All tests have to pass before the software (or a new feature) can be released to customers. The tests should be written before the production code that they test.

This so-called test-first programming led to TDD. As the name suggests, in TDD, tests drive the development. This means that the developer writes code only because there is a test that fails. The tests dictate whether the code has to be written, and they also provide a measure when a feature is "done"--it is done when all tests for this feature pass.

Robert C. Martin (known as Uncle Bob) has come up with three simple rules for TDD:

  • You are not allowed to write any production code unless it is to pass a failing unit test
  • You are not allowed to write any more of a unit test that is sufficient to fail, and compilation failures are failures
  • You are not allowed to write any more production code that is sufficient to pass the one failing unit test

These rules sound kind of silly because when you start with a feature that uses a new class or method that is not declared yet, the test will fail immediately, and you have to add some code just to be able to finish writing the test. But by following these rules, you will only write code that is actually needed to implement the features. And you will only write test code that is needed as well. All the code you write will either end up being part of the final product or it will be a part of your test suite.

Because of the focus on just one feature at a time, you will have a working piece of software almost all the time. So, when your boss enters your office and asks you for a demonstration of the current status of the project, you are only a few minutes away from a presentable (that is, compiling), and a thoroughly tested piece of software.

主站蜘蛛池模板: 高碑店市| 华池县| 清涧县| 鄂尔多斯市| 麻栗坡县| 榕江县| 镶黄旗| 泸定县| 嘉荫县| 剑阁县| 平陆县| 治多县| 高密市| 柯坪县| 敦化市| 曲周县| 涪陵区| 华坪县| 克东县| 重庆市| 屯留县| 曲水县| 定边县| 文安县| 宣武区| 抚远县| 长子县| 洪洞县| 鞍山市| 绥棱县| 怀来县| 定州市| 怀柔区| 湟源县| 清原| 宿松县| 栾城县| 平谷区| 思南县| 长垣县| 响水县|