Testing on the Toilet

June 13, 2007

Extracting Methods to Simplify Testing When a method is long and complex, it is harder to test. You can make it easier by extracting methods: finding pieces of code in existing, complex methods (or functions) that can be replaced with method calls (or function calls). Consider the following complicated method: def GetTestResults(self): # Check if results have been cached. results = cache.get('test_results', None) if results is None: # No results in the cache, so check the database. results = db.FetchResults(SQL_SELECT_TEST_RESULTS) # Count passing and failing tests. num_passing = len([r for r in results if r['outcome'] == 'pass']) num_failing = len(results) - num_passing return num_passing, num_failing

This method is difficult to test because it not only relies on a database, but also on a cache. In addition, it performs some post processing of the retrieved results. The first hint that this method could use refactoring is the abundance of comments. Extracting sections of code into well-named methods reduces the original method's complexity. When complexity is reduced, comments often become unnecessary. For example, consider the following: def GetTestResults(self): results = self._GetTestResultsFromCache() if results is None: results = self._GetTestResultsFromDatabase() return self._CountPassFail(results) def _GetTestResultsFromCache(self): return cache.get('test_results', None) def _GetTestResultsFromDatabase(self): return db.FetchResults(SQL_SELECT_TEST_RESULTS) def _CountPassFail(self, results): num_passing = len([r for r in results if r['outcome'] == 'pass']) num_failing = len(results) – num_passing return num_passing, num_failing

Now, tests can focus on each individual piece of the original method by testing each extracted method. This has the added benefit of making the code more readable and easier to maintain. (Note: Method extraction can be done for you automatically in Python by the open-source refactoring browser BicycleRepairMan, and in Java by several IDEs, including IntelliJ IDEA and Eclipse.)

More information, discussion, and archives: http://googletesting.blogspot.com Copyright © 2007 Google, Inc. Licensed under a Creative Commons Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

Extracting Methods to Simplify Testing Code

Jun 13, 2007 - BicycleRepairMan, and in Java by several IDEs, including IntelliJ IDEA and Eclipse.) More information, discussion, and archives:.

159KB Sizes 2 Downloads 248 Views

Recommend Documents

Extracting Methods to Simplify Testing
Jun 13, 2007 - When a method is long and complex, it is harder to test. ... can be done for you automatically in Python by the open-source refactoring browser.

Methods for Automatically Extracting Bioacoustic ...
more target sources from the remainder of the sig- nal (the background). The target sources may be a single .... For each possible energy threshold, the black area to the right of .... alternative way of describing a probability distribution.

Methods for Automatically Extracting Bioacoustic ...
In particular, the number of signal categories is determined automatically. ..... 'guess', by the user, of the number of distinct types of call. However, our initial experiments with humpback ..... view”, AT&T Bell Laboratories Technical Journal 63

Testing Against Interfaces Code
Jul 24, 2008 - Page 1 ... inadequacy, you took the time to build your own planetary death ray, a veritable ... build rule that includes java/junit as a dependency:.

FORTEST: Formal Methods and Testing
requires the application of a battery of such techniques. Two of the most ...... ization of existing testing criteria [54] to help eliminate mis- understanding and in the ...

Summary Models & Implementation Methods Testing ...
Seth Simms, Margot Maxwell, Sara Johnson, Julian Rrushi. Summary. Models & Implementation Methods. Testing & Results ... I/O Manager. Hardware. HID.

Evaluation of Sensory Substitution to Simplify the ...
summarized in Table I, and based on these data mean value and standard deviation are ... Prior to the test, a logarithmic transformation was applied to meet ... Matey, “A large haptic device for aircraft engine maintainability,” IEEE. Computer ..

grab partners with android pay to simplify the ... Developers
THE CHALLENGE. Cash transactions for ride hailing. In 2012, a few friends were enjoying some tea together and started ranting about how difficult it was to get ...

LUXE TEAMS UP WITH ANDROID PAY TO SIMPLIFY THE USER ...
LUXE TEAMS UP WITH ANDROID PAY. TO SIMPLIFY THE USER EXPERIENCE. To learn more about simple payment on your Android app,.

Evaluation of Sensory Substitution to Simplify the ...
In the sensory substitution field, [9] analyzed the effects of substituting direct haptic .... Apart from the analysis of the statistical data, user comments while ...

CoGenTe: A Tool for Code Generator Testing
Sep 24, 2010 - rating various coverage criteria over semantics. This enables ... tactic aspects of a translation, but its complex semantic as- pects, too. Syntactic ...

Behaviour Directed Testing of Auto-code Generators
[19] G. C. Necula. Translation validation for an optimizing com- piler. In PLDI, pages 83–94, 2000. [20] V. Paxson. flex - A fast scanner generator., 2.5 edition,.

Be an MVP of GUI Testing Code
Feb 5, 2009 - MVP is very similar to MVC (Model-View-Controller). In MVC, the presentation logic is shared by Controller and. View,as shown in the diagram ...

Standard Test Methods for Tension Testing of Metallic Materials.pdf
Astm E 8M 04 - Standard Test Methods for Tension Testing of Metallic Materials.pdf. Astm E 8M 04 - Standard Test Methods for Tension Testing of Metallic ...

The appropriateness of statistical methods for testing ...
P4. P6. Table Aa. P8. Structure. P8. P6. P4. P10 a The indices denote the level of organizational performance. P2. P2. Low. High. High. Low. Contingency. P8.

Testing Speaking: Methods, Techniques and Tips Tim ...
Tim Dalby (Jeonju University) ... Formative assessment, such as this, can allow a teacher to modify the course ... To have good construct validity, there needs ... rater training so that all examiners are clear in the use of the marking criteria.

Standard Test Methods for Tension Testing of Metallic Materials.pdf
Astm E 8M 04 - Standard Test Methods for Tension Testing of Metallic Materials.pdf. Astm E 8M 04 - Standard Test Methods for Tension Testing of Metallic ...