Clean Code “Unit Tests”

Mike Christianson http://codeaweso.me

The Three Laws of TDD

The Three Laws of TDD First Law - You may not write production code until you have written a failing unit test.

The Three Laws of TDD First Law - You may not write production code until you have written a failing unit test. Second Law - You may not write more of a unit test than is sufficient to fail, and not compiling is failing.

The Three Laws of TDD First Law - You may not write production code until you have written a failing unit test. Second Law - You may not write more of a unit test than is sufficient to fail, and not compiling is failing. Third Law - You may not write more production code than is sufficient to pass the currently failing test.

TDD’s “Fallout 3”

TDD’s “Fallout 3” Every Day - Dozens of tests

TDD’s “Fallout 3” Every Day - Dozens of tests Every Month - Hundreds of tests

TDD’s “Fallout 3” Every Day - Dozens of tests Every Month - Hundreds of tests Every Year - Thousands of tests

TDD’s “Fallout 3” Every Day - Dozens of tests Every Month - Hundreds of tests Every Year - Thousands of tests

That’s a lot of code!

Keeping Tests Clean Test code is just as important as production code. Perhaps more so.

Remember the “broken windows” theory.

Clean Tests

Readability Readability Readability

Clean Tests

Readability

Clarity

Readability

Simplicity

Readability

Density of Expression

Build-Operate-Check

Build up test data Operate on test data Check for expected results

Build-Operate-Check public void testGetDataAsXML() throws Exception { makePageWithContent(“TestPageOne”, “test page”); submitRequest(“TestPageOne”, “type:data”); assertResponseIsXML(); assertResponseContains(“test page”, “
Build-Operate-Check public void testGetDataAsXML() throws Exception { makePageWithContent(“TestPageOne”, “test page”); submitRequest(“TestPageOne”, “type:data”); assertResponseIsXML(); assertResponseContains(“test page”, “
Any other “clean Code” practices?

Which do you prefer? @Test public void turnOnLoTempAlarmAtThreshold() { hw.setTemp(WAY_TOO_COLD); controller.tic(); assertTrue(hw.heaterState()); assertTrue(hw.blowerState()); assertFalse(hw.coolerState()); assertFalse(hw.hiTempAlarm()); assertTrue(hw.loTempAlarm()); } @Test public void turnOnLoTempAlarmAtThreshold() { wayTooCold(); assertEquals(“HBchL”, hw.getState()); }

Dual Standards

Memory or CPU efficiency usually matter less within a test. But, always, cleanliness remains.

One Assert per Test It’s more what you'd call a "guideline" than actual rule. Strategies for removing duplicate code with the goal of following the “rule” can complicate tests. Template Method pattern Separate class, use @Before

Single Concept per Test

An actual rule, not a guideline A continuation of prior “clean code” concepts Violation causes confusion and delay for the reader

Single Concept per Test @Test public void test() throws ParseException { GroupValidator validator = new GroupValidator(LOCATION, "WIII"); LinkedHashMap groups = ReportParser.parse("METAR WIII 172145Z AUTO VRB02KT 9999 +SHRA +FC FZDZ +TSRAGR " + " +BKN030 FEW075 SCT100 OVC250 11/06 Q1021", validator); assertEquals("172145Z", groups.get(TIMESTAMP)); assertEquals("AUTO", groups.get(AUTO_COR)); assertEquals("VRB02KT", groups.get(WIND)); assertEquals("9999", groups.get(VISIBILITY)); assertEquals("+SHRA +FC FZDZ +TSRAGR", groups.get(WEATHER)); assertEquals("BKN030 FEW075 SCT100 OVC250", groups.get(SKY_CLOUD)); assertEquals("11/06", groups.get(TEMP_DEWPOINT)); assertEquals("Q1021", groups.get(PRESSURE)); assertEquals("METAR AUTO", groups.get(FULL_TYPE)); }

Single Concept per Test @Test public void test() throws ParseException { GroupValidator validator = new GroupValidator(LOCATION, "WIII"); LinkedHashMap groups = ReportParser.parse("METAR WIII 172145Z AUTO VRB02KT 9999 +SHRA +FC FZDZ +TSRAGR " + " +BKN030 FEW075 SCT100 OVC250 11/06 Q1021", validator); assertEquals("172145Z", groups.get(TIMESTAMP)); assertEquals("AUTO", groups.get(AUTO_COR)); assertEquals("VRB02KT", groups.get(WIND)); assertEquals("9999", groups.get(VISIBILITY)); assertEquals("+SHRA +FC FZDZ +TSRAGR", groups.get(WEATHER)); assertEquals("BKN030 FEW075 SCT100 OVC250", groups.get(SKY_CLOUD)); assertEquals("11/06", groups.get(TEMP_DEWPOINT)); assertEquals("Q1021", groups.get(PRESSURE)); assertEquals("METAR AUTO", groups.get(FULL_TYPE)); }

Thoughts? Ideas? Is it “Clean”?

F.I.R.S.T Fast - Tests should be fast and run quickly. Independent - Test should not depend on each other and be able to run in any order. Repeatable - Test should be repeatable in any environment. Self Validating - Tests should have a boolean output: pass or fail. Timely - The tests need to be written in a timely fashion, that is, just before production code.

Clean Code “Unit Tests” - GitHub

It's more what you'd call a "guideline" ... LinkedHashMap<Group,String> groups = ... assertEquals("+SHRA +FC FZDZ +TSRAGR", groups.get(WEATHER));.

9MB Sizes 10 Downloads 125 Views

Recommend Documents

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

Clean Code “Error Handling” - GitHub
It's your job. Do it. Error handling isn't a curse, or a bother. Things can and do go wrong. Thus, error handling is an essential part of programming. And, since it ...

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.

Perl Post Install Tests - GitHub
Apr 10, 2013 - CPAN/Perl community has usually been good at preserving backwards compatibility ... and installed. ▻ Writing a best practices guide for tests ...

Code Library - GitHub
Sep 13, 2013 - Contents. 1 Data Structure. 1. 1.1 atlantis . .... 4.25 Second-best MST . ...... prime is good. 10 static const int MAXX=47111; // bigger than. √ c. 11.

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

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.

robert martin clean code pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. robert martin ...

The Pirate Copyright Code - GitHub
law should be in the information age not the way it has to be to satisfy the last milleniums ... subsequent exploitation regulations for the case the economic rights expired with the author still ..... dicial proceedings or public security; c. use fo

101 Ruby Code Factoids - GitHub
You can add the current directory to your load path with: .... Apple.chew. # => "munch munch" def Apple.cut. "chop chop" end. Apple.cut ..... 61) Method#owner.

Old school code audit? - GitHub
Awesome week! * Hack anything you want. * Just boring code audit? No shit--->. * Telco sec shit are looks awesome. Step-1: Software defined radio-->listen to ...

Time in jsUnit Tests Code
Mar 29, 2007 - function testUpdatesStatusMessageOverFourSeconds() {. Clock.reset(); // Clear any existing timeout functions on the event queue. var status = ...

OpenDaylight Performance Stress Tests Report v1.0 - GitHub
Jun 29, 2015 - For our evaluation we have used NSTAT [1], an open source en- vironment ... rameters), online performance and statistics monitoring, and.

Unit - 4 Code Optimization.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Unit - 4 Code ...

Open Source Code Serving Endangered Languages - GitHub
ten called low-resource, under-resourced, or minority lan- guages) ... Our list is updatable more ... favorites on other social media sites, and, generally, a good.

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

Marching Up and Down the Code - GitHub
CONTENTS. 0 Starting with Python's IDLE. 1. 0.0 Introduction . ..... Knowing how to program a computer is a great skill to have, even if you are not a ..... need to take the value of age from the present year, 2015, and then add on 100. ..... Draw a

HDL-BUS control and operate code - GitHub
Operate code. Function. Targets address. Additional data format(every 9 data) ..... x value of volume(79 small-----0 big). Return #Zz,ON,SRC1,VOL38.