`
xucons
  • 浏览: 86426 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Effective Unit Testing

    博客分类:
  • Java
阅读更多

By Tim Burns Careful programmers test early and test often. Unit testing. Isn't that some annoying requirement that we're going to ignore? What if I told you we are going to start with unit testing? You ask, "But how can I test code that hasn't been written?" I reply, "The unit test will define the code you are going to write." Many developers get very nervous when you mention unit tests. I suspect they have this vision of a grand table with every single method listed, along with the expected results and pass/fail date. I'll leave that kind of unit testing to folks trying to get FDA approval. It's important, but not relevant in most programming projects. The unit test will motivate the code that you write. In a sense, it is a little design document that says, "What will this bit of code do?" Or, in the language of object oriented programming, "What will these clusters of objects do?" The crucial issue in constructing a unit test is scope. If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions. Certainly, interactions of objects are the crux of any object oriented design. Likewise, if the scope is too broad, then there is a high chance that not every component of the new code will get tested. The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy. I like to start with a feature that is described in the requirements document and work down until a method doesn't need a unit test. That way, tests are grouped according to major feature and should include as many unit tests that deal with the feature in question as are necessary. How do you know that a method doesn't need a unit test? First, can it be tested by inspection? If the code is simple enough that the developer can just look at it and verify its correctness then it is simple enough to not require a unit test. The developer should know when this is the case. Unit tests will most likely be defined at the method level, so the art is to define the unit test on the methods that cannot be checked by inspection. Usually this is the case when the method involves a cluster of objects. Unit tests that isolate clusters of objects for testing are doubly useful, because they test for failures, and they also identify those segments of code that are related. People who revisit the code will use the unit tests to discover which objects are related, or which objects form a cluster. Hence: Unit tests isolate clusters of objects for future developers. Another good litmus test is to look at the code and see if it throws an error or catches an error. If error handling is performed in a method, then that method can break. Generally, any method that can break is a good candidate for having a unit test, because it may break at some time, and then the unit test will be there to help you fix it. The danger of not implementing a unit test on every method is that the coverage may be incomplete. Just because we don't test every method explicitly doesn't mean that methods can get away with not being tested. The programmer should know that their unit testing is complete when the unit tests cover at the very least the functional requirements of all the code. The careful programmer will know that their unit testing is complete when they have verified that their unit tests cover every cluster of objects that form their application.

分享到:
评论

相关推荐

    Effective.Unit.Testing(2013.2)

    《Effective Unit Testing(2013.2)》是一本专门为Java开发者量身定做的单元测试指南,由Lasse Koskela撰写,属于Manning Publications Co.出版的一系列计算机书籍。这本书在介绍单元测试(UT)方面获得了积极评价...

    Test-Driven Java Development(PACKT,2015)

    Master effective unit testing in isolation from the rest of your code Design simple and easily maintainable codes by implementing different techniques Use mocking frameworks and techniques to easily ...

    Effective software testing

    #### 六、单元测试(Unit Testing) 1. **结构化开发方式支持有效的单元测试**:在开发过程中采用合适的方法论,如敏捷开发,有助于提高单元测试的质量。 2. **并行或先于实现开发单元测试**:在代码实现之前或...

    Effective Software Testing

    #### 六、单元测试(Unit Testing) **6.1 发展策略支持单元测试** - **策略**:采用适合的开发模式以利于高效执行单元测试。 - **模式**:敏捷开发、持续集成等。 **6.2 并行或前置单元测试开发** - **时机**:...

    computer books

    《Effective Unit Testing: A Guide for Java Developers》这本书强调了良好的单元测试对于提高软件质量和减少后期维护成本的重要性。书中提到“the promise of good tests”,即好的单元测试能够带来的益处,包括但...

    spring3 and junit4

    本文将深入探讨这两个关键组件,并结合《Effective Unit Testing》一书中的精华,为开发者提供全面的理论和实践指导。 1. Spring3.2.8框架详解: - **依赖注入**:Spring的核心特性之一,通过反转控制,实现了对象...

    Test-Driven Java Development - Second Edition.pdf

    Master effective unit testing in isolation from the rest of your code Design simple and easily maintainable code by implementing different techniques Use mocking frameworks and techniques to easily ...

    有效的软件测试[英]

    **6.1 构建支持有效单元测试的开发方法(Structure the Development Approach to Support Effective Unit Testing)** - **关键点**:确保开发过程支持单元测试。 - **重要性**:良好的开发实践可以简化单元测试的...

    测试大包.zip

    《Effective Unit Testing》这本书则可能专注于如何编写有效的单元测试,提供了一系列最佳实践和策略。这可能包括如何编写可读性强、易于维护的测试,如何编写针对边界条件和异常情况的测试,以及如何利用Mocking和...

    软件测试英文书软件测试英文书

    构建支持有效单元测试的开发方法(Structure the Development Approach to Support Effective Unit Testing)** - 在软件开发过程中采用有利于单元测试的方法论。 - 如敏捷开发、持续集成等。 - 这有助于提高单元...

    XDoclet in Action

    - **Testing Best Practices**: Tips on effective unit testing strategies using mock objects. #### Chapter 11: XDoclet and Portlets Finally, this chapter discusses the use of XDoclet in generating code...

    软件测试:wiley,.the.art.of.software.testing.(2004)

    pragmatic unit testing in c#_edited with nunit (ebook) wiley - testing web security-assessing the security of web sites and applications - 2002 (steven splaine) artech house - testing and quality ...

    Python Testing with pytest: Simple, Rapid, Effective, and Scalable

    ### Python Testing with pytest: Simple, Rapid, Effective, and Scalable #### 一、概述 《Python Testing with pytest: Simple, Rapid, Effective, and Scalable》是一本由Brian Okken撰写的书籍,旨在帮助读者...

    Effective Software Test Automation

    You can use this tool to generate test scripts for continuous unit testing, integration testing, and regression testing. Software defects are common and cause economic losses from time to time. ...

    有效软件测试的50条建议

    The fifty lessons provided here focus on the key aspects of software testing: test planning, design, documentation, execution, managing the testing team, unit testing, automated testing, ...

Global site tag (gtag.js) - Google Analytics