What are key points to explain Unit Testing

前端 未结 6 2190
一个人的身影
一个人的身影 2021-02-09 19:06

I want to introduce Unit Testing to some colleagues that have no or little experience with Unit Testing. I\'ll start with a presentation of about an hour to explain the concept

6条回答
  •  囚心锁ツ
    2021-02-09 19:32

    Unit tests test small things

    Another thing to remember is that unit tests test small things, "units". So if your test runs against a resource like a live server or a database, most people call that a system or integration test. To unit test just the code that talks to a resource like that, people often use mock objects (often called mocks).

    Unit tests should run fast and be run often

    When unit tests test small things, the tests run fast. That's a good thing. Frequently running unit tests helps you catch problems soon after the occur. The ultimate in frequently running unit tests is having them automated as part of continuous integration.

    Unit tests work best when coverage is high

    People have different views as to whether 100% unit test coverage is desirable. I'm of the belief that high coverage is good, but that there's a point of diminishing return. As a very rough rule of thumb, I would be happy with a code base that had 85% coverage with good unit tests.

    Unit tests aren't a substitute for other types of tests

    As important as unit tests are, other types of testing, like integration tests, acceptance tests, and others can also be considered parts of a well-tested system.

    Unit testing existing code poses special challenges

    If you're looking to add unit tests to existing code, you may want to look at Working Effectively with Legacy Code by Michael Feathers. Code that wasn't designed with testing in mind may have characteristics that make testing difficult and Feathers writes about ways of carefully refactoring code to make it easier to test. And when you're familiar with certain patterns that make testing code difficult, you and your team can write code that tries to avoid/minimize those patterns.

提交回复
热议问题