Testing on the Toilet

April 4, 2007

Stubs Speed up Your Unit Tests Michael Feathers defines the qualities of a good unit test as: “they run fast, they help us localize problems.” This can be hard to accomplish when your code accesses a database, hits another server, is time-dependent, etc. By substituting custom objects for some of your module's dependencies, you can thoroughly test your code, increase your coverage, and still run in less than a second. You can even simulate rare scenarios like database failures and test your error handling code. A variety of different terms are used to refer to these “custom objects”. In an effort to clarify the vocabulary, Gerard Meszaros provides the following definitions: ● Test Double is a generic term for any test object that replaces a production object. ● Dummy objects are passed around but not actually used. They are usually fillers for parameter lists. ● Fakes have working implementations, but take some shortcut (e.g., InMemoryDatabase). ● Stubs provide canned answers to calls made during a test. ● Mocks have expectations which form a specification of the calls they do and do not receive. For example, to test a simple method like getIdPrefix() in the IdGetter class: public class IdGetter { // Constructor omitted. public String getIdPrefix() { try { String s = db.selectString("select id from foo"); return s.substring(0, 5); } catch (SQLException e) { return ""; } } }

You could write: db.execute("create table foo (id varchar(40))"); // db created in setUp(). db.execute("insert into foo (id) values ('hello world!')"); IdGetter getter = new IdGetter(db); assertEquals("hello", getter.getIdPrefix());

The test above works but takes a relatively long time to run (network access), can be unreliable (db machine might be down), and makes it hard to test for errors. You can avoid these pitfalls by using stubs: public class StubDbThatReturnsId extends Database { public String selectString(String query) { return "hello world"; } } public class StubDbThatFails extends Database { public String selectString(String query) throws SQLException { throw new SQLException("Fake DB failure"); } } public void testReturnsFirstFiveCharsOfId() throws Exception { IdGetter getter = new IdGetter(new StubDbThatReturnsId()); assertEquals("hello", getter.getIdPrefix()); } public void testReturnsEmptyStringIfIdNotFound() throws Exception { IdGetter getter = new IdGetter(new StubDbThatFails()); assertEquals("", getter.getIdPrefix()); }

More information, feedback, and discussion: 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/).

Stubs Speed up Your Unit Tests

Apr 4, 2007 - Stubs Speed up. Your Unit Tests. Michael Feathers defines the qualities of a good unit test as: “they run fast, they help us localize problems.

152KB Sizes 2 Downloads 199 Views

Recommend Documents

Stubs Speed up Your Unit Tests
Apr 4, 2007 - Michael Feathers defines the qualities of a good unit test as: “they run fast, they help us localize problems.” This can be hard to accomplish when your code accesses a database, hits another server, is time-dependent, etc. By subst

UP TO SPEED
Tornado treasures returned through use of social media (Tullahoma News) ... List of Alabama Tornado Casualties ... Disaster Relief Agencies (list of agencies).

UP TO SPEED
and tornado watches hours in ... After tornadoes left over 300 dead in just a 24 hour period those across the country are now .... Volunteer Call Center is a 24/7.

speed up pdf
Whoops! There was a problem loading more pages. speed up pdf. speed up pdf. Open. Extract. Open with. Sign In. Main menu. Displaying speed up pdf.

speed up - UKZN Student Funding
You should be in your 1st to final year of studying your Bachelors Degree. All studies should be focused on the below disciplines: Mechanical, Industrial or ...

Naming Unit Tests Responsibly Code
Jan 30, 2007 - For example, when it's hard to construct a sentence where the first word is the class under test, it suggests the test may be in the wrong place. And classes that are hard to describe in general often need to be broken down into smalle

pc speed up pro serial.pdf
Try one of the apps below to open or edit this item. pc speed up pro serial.pdf. pc speed up pro serial.pdf. Open. Extract. Open with. Sign In. Main menu.

speed your internet.pdf
Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. speed your internet.pdf. speed your internet.pdf. Open. Extract.

Discovering Math APIs by Mining Unit Tests
Department of Computer Science and Automation, Indian Institute of Science. {anirudh s,pandita.omesh ... top-most pseudo-code snippet to implement the entire expression was correct in 93% of the cases. .... 4 hadoop.apache.org. 5 respectively, acs.lb

Powerful unit root tests free of nuisance parameters
Feb 23, 2015 - Of course, the limit depends on the true parameter value c entering through the data by Assumption 1, and ... e−cs − e−c. 2c2 ecs +. 1 c2. , δ0 = −. 3 − 2c + e−2c − 4e−c. 2c3 ..... 3See Tanaka (1996, Ch. 6) for further

Discovering Math APIs by Mining Unit Tests - (SEAL), IISc Bangalore
... unit test mining approach. The semantics of APIs to be migrated can be specified in math notation, to obtain matching APIs from other libraries using MathFinder. Acknowledgements We thank the volunteers of the user study, and the mem- bers of the

Discovering Math APIs by Mining Unit Tests
Abstract. In today's API-rich world, programmer productivity depends heavily on the programmer's ability to discover the required APIs. In this paper, we present a technique and tool, called MathFinder, to discover. APIs for mathematical computations

Solid Type System Runtime Checks and Unit Tests - GitHub
insufficient type information! Return type should be something like ... type is. // ProteinFail \/ Future[List[FacebookLike]]. Unwrap? ... case \/(result) => s"Dude, eat proteins, or you won't do like me: $result" .... Thank you! goo.gl/U0WYAB · PDF.

17 Ways to Optimize and Speed Up WordPress Sites.docx.pdf ...
17 Ways to Optimize and Speed Up WordPress Sites.docx.pdf. 17 Ways to Optimize and Speed Up WordPress Sites.docx.pdf. Open. Extract. Open with. Sign In.

Can Quantum Communication Speed Up Distributed ...
Research at the Centre for Quantum ..... talk to the server. ..... the server model as Carol, David, and the real server, while we call the nonlocal ...... For any α ≥ 1, an α-approximate solution of P on weighted network (N,ω) is a feasible sol

Speed-up techniques for solving large-scale biobjective ...
Jan 23, 2009 - Computers & Operations Research 37 (2010) 521--533. Contents lists available at ... Of course, these techniques have to be properly ..... ing cost with vi. The size of the list is limited to a reasonable size. All the cities are then .

Combining genes and memes to speed up evolution
Department of computer and information science. N-7491 ... the maximum number of memes that an agent can ac- quire as ... If on one side, learning can guide.

Speed-up Techniques for Solving Large-scale bTSP ...
Multiobjective combinatorial optimization, tsp, local search, speed-up techniques. 1. INTRODUCTION. Considering more than one objective in combinatorial op-.