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

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.

159KB Sizes 1 Downloads 258 Views

Recommend Documents

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

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

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

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

Extracting expertise to facilitate exploratory search ... - Semantic Scholar
Jan 1, 2009 - (IR) methods of expert identification with a computational cognitive model to test ... data-mining techniques to predict the usefulness of different.

Which dissimilarity is to be used when extracting ...
being increasingly used in social sciences for the study of life-course pro- cesses. The methodology .... exhaustively among all input vectors and the algorithm is of batch type. ..... Lecture Notes in Computer Science 5629. Springer 2009, ISBN ...

Extracting expertise to facilitate exploratory search ... - Semantic Scholar
Jan 1, 2009 - data-mining techniques to predict the usefulness of different presentations of ... area of HCI or cognitive sciences, definition of expertise is often.

Extracting Contextual Evaluativity
(§4) strongly support a compositional approach to contextual evaluativity inference. A simple com- ... Note that for this application, we may simplify the compositional picture and treat functors as .... 2http://developer.yahoo.com/search/.