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.

Building Maintainable Software-java篇之Automate Tests_第1张图片


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 [email protected].
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


你可能感兴趣的:(Building Maintainable Software-java篇之Automate Tests)