Building Maintainable Software-java篇之Automate Tests

Building Maintainable Software-java篇之Automate Tests


Keep the bar green to keep the code clean.
—The jUnit motto


Guideline:

Automate tests for your codebase.
• Do this by writing automated tests using a test framework.
• This improves maintainability because automated testing makes development predictable and less risky.

Motivation

Automated Testing Makes Testing Repeatable

Just like other programs and scripts, automated tests are executed in exactly the same
way every time they are run. This makes testing repeatable: if a certain test executes at
two different points in time yet gives different answers, it cannot be that the test execution itself was faulty. One can conclude that something has changed in the system
that has caused the different outcome. With manual tests, there is always the possibility that tests are not performed consistently or that human errors are made.

Automated Testing Makes Development Efficient

Automated tests can be executed with much less effort than manual tests. The effort
they require is negligible and can be repeated as often as you see fit. They are also
faster than manual code review. You should also test as early in the development process as possible, to limit the effort it takes to fix problems.


Automated Testing Makes Code Predictable



Technical tests can be automated to a high degree. Take unit tests and integration
tests: they test the technical inner workings of code and the cohesion/integration of
that code. Without being sure of the inner workings of your system, you might get
the right results by accident. It is a bit like driving a car: you might arrive at an
intended destination by following the wrong directions, but when you want to go to
another destination, you are uncertain whether the new directions are reliable and
will actually take you there.
A common advantage of automated testing is identifying when regression is occur‐
ring. Without a batch of automated unit tests, development quickly turns into a game
of whack-a-mole: you implement a change in one piece of code, and while you are
working on the next change in another piece of code, you realize you have introduced
a bug with your previous change. Automated tests allow you to double-check your
entire codebase effortlessly before turning to the next change. And since the automa‐
ted unit tests follow predefined paths, you can be sure that if you have fixed a bug, it
does not pop up on a second run.


Thus, running automated tests provides certainty about how the code works. There‐
fore, the predictability of automated tests also makes the quality of developed code
more predictable.


Tests Document the Code That Is Tested

The script or program code of a test contains assertions about the expected
behavior of the system under test. 


Writing Tests Make You Write Better Code

Writing tests helps you to write testable code. As a side effect, this leads to code consisting of units that are shorter, are simpler, have fewer parameters, and are more loosely coupled (as the guidelines in the previous chapters advise). 


How to Apply the Guideline



How you automate tests differs by the types of tests you want to automate. Test types
differ in what is tested, by whom, and why, as detailed in Table 10-1. They are ordered
from top to bottom based on the scope of the tests. For example, a unit test has the
unit as scope, while an end-to-end test, a regression test, and an acceptance test are
on the system level.




Table 10-1 shows that a regression test is a unit test, an integration test, or an end-toend test that has been created when a bug was fixed. Acceptance tests are end-to-end tests executed by end user representatives.
Different types of testing call for different automation frameworks. For unit testing,
several well-known Java frameworks are available, such as jUnit. For automated
end-to-end testing, you need a framework that can mimic user input and capture
output. A well-known framework that does just that for web development is Sele‐
nium. For integration testing, it all depends on the environment in which you are
working and the quality characteristics you are testing. SoapUI is a framework for
integration tests that focuses on web services and messaging middleware. Apache
jMeter is a framework for testing the performance of Java applications under heavy
workloads.
Choosing a test framework needs to be done at the team level. Writing integration
tests is a specialized skill—but unit testing is for each and every individual developer.
That is why the rest of this chapter focuses on writing unit tests using the most wellknown framework for Java: jUnit.


Getting Started with jUnit Tests



General Principles for Writing Good Unit Tests



When writing tests, it is important to keep in mind the following general principles:


Test both normal and special cases


Maintain tests just like nontest (production) code


Write tests that are isolated: their outcomes should reŠect only the behavior of the subject being tested




Measure Coverage to Determine Whether There Are Enough Tests



读书笔记:

Building Maintainable Software: Ten Guidelines for Future-Proof Code
by Joost Visser
Copyright © 2016 Software Improvement Group, B.V. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Acquisitions Editor: Rachel Roumeliotis
Editor: Nan Barber
Production Editor: Matthew Hacker
Copyeditor: Rachel Monaghan
Proofreader: Marta Justak
Indexer: WordCo Indexing Services, Inc.
Interior Designer: David Futato
Cover Designer: Randy Comer
Illustrator: Rebecca Demarest
February 2016: First Edition
Revision History for the First Edition
2016-01-25: First Release
See  http://shop.oreilly.com/product/0636920049159.do


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值