`
xucons
  • 浏览: 85384 次
  • 性别: 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).pdf 挺好的一本介绍UT的书

    [ebook] addison wesley - effective software testing.pdf

    Chapter 1. Requirements Phase ... Unit Testing Chapter 7. Automated Testing Tools Chapter 8. Automated Testing: Selected Best PracticesChapter 9. Nonfunctional Testing ..... Item 41: Do Not Make

    有效的软件测试[英]

    Item 28: Structure the Development Approach to Support Effective Unit Testing Item 29: Develop Unit Tests in Parallel or Before the Implementation Item 30: Make Unit-Test Execution Part of the Build ...

    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 ...

    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 ...

    测试大包.zip

    Java测试驱动开发,JUnit.in.Action.2nd,测试驱动开发的艺术,有效单元测试(Effective unit testing)

    spring3 and junit4

    spring framework 3.2.8,包含完整版库文件及api,junit 4库文件及api,还有pdf资料Effective Unit Testing

    Mochito Tutorials Point

    Mockito is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock...

    软件测试: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 ...

    Developer.Testing.Building.Quality.into.Software

    Establish well-balanced and effective testing strategies suitable for agile teams Table of Contents Chapter 1 Developer Testing Chapter 2 Testing Objectives, Styles, and Roles Chapter 3 The Testing ...

    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. ...

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

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

    Python Testing Cookbook.pdf

    Over 70 simple but incredibly effective recipes for taking control of automated testing using powerful Python testing tools Learn to write tests at every level using a variety of Python testing tools...

    有效软件测试的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, ...

    Testing.and.Securing.Android.Studio.Applications

    Create effective test cases, unit tests, and functional tests to ensure your Android applications function correctly Optimize the performance of your app by debugging and using high-quality code Who ...

    The.Way.of.the.Web.Tester.A.Beginners.Guide.to.Automating.Tests

    Follow the testing pyramid and level up your skills in user interface testing, integration testing, and unit testing. Your new skills will free you up to do other, more important things while letting...

    Reengineering.NET.0321821459.epub

    Using innovative Test Doubling to make unit testing even more effective Applying Dependency Inversion to break tight coupling and promote easier development and testing Leveraging source control, ...

    Mockito Cookbook - Packt

    Mockito is a testing framework that allows you to easily create test doubles (mocks) ... This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on.

    Pro WPF and Silverlight MVVM - Effective Application Development with MVVM

    Chapter 7: Unit Testing ........................145 Chapter 8: Data Access Layer ...................163 Chapter 9: Application Support .................185 Chapter 10: Sample Application ................

Global site tag (gtag.js) - Google Analytics