What are key points to explain Unit Testing

前端 未结 6 2181
一个人的身影
一个人的身影 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.

    0 讨论(0)
  • 2021-02-09 19:35

    You might get some inspiration here too https://stackoverflow.com/questions/581589/introducing-unit-testing-to-a-wary-team/581610#581610

    0 讨论(0)
  • 2021-02-09 19:40

    Unit tests should be FAIR.

    • F Fast
    • A Can be easily Automated
    • I Can be run Independently
    • R Repeatable
    0 讨论(0)
  • 2021-02-09 19:41

    Main points:

    • unit tests help both design (by expressing intent) and regression test (by never going away) code;
    • unit tests are for lazy programmers who don't want to debug their code again;
    • tests have no business of influencing or affecting business logic and functionality, but they do test it fully;
    • unit tests demand the same qualities as regular code: theory and strategy, organization, patterns, smells, refactoring;
    0 讨论(0)
  • 2021-02-09 19:43

    To keep it really short: Unit testing is about two things

    • a tool for verifying intentions
    • a necessary safety net for refactoring

    Obviously, it is a lot more than that, but to me that pretty much the sums it up.

    0 讨论(0)
  • 2021-02-09 19:44

    Remember to point out that Unit Testing is not a silver bullet and shouldn't replace other forms of traditional testing (Functional Tests etc) but should be used in conjunction.

    Unit testing works better in some areas than others, so the only way to have truly comprehensive testing is to combine it with other forms.

    This seems to be one of the biggest criticisms I see of Unit Testing as a lot of people don't seem to 'get' that it shouldn't be replacing other forms of testing in totality.

    0 讨论(0)
提交回复
热议问题