Let's make this testsuite run faster!
View Slide
David GageotFreelancedeveloper@dgageotjavabien.net
Why make a test suite run faster?
“ I firmly believe it can makeyour product better. Faster totest leads to faster to write,deploy and use ”Anonymous bearded freelance developper
You test early and often. Right?
Continuous integration?
Continuous testing?
Continuous deployment?
Testing a lot can lead to waiting a lot
How wants his coding room to look like
Test suite duration can be contained
Jenkins slaves to the rescue
Jenkins slaves to the rescue. not.
Why complicated when it can be simple?
I’ll share a few easy tricks
How can we accelerate the tests?TheCheaterThe Lazy
TheCheater
Buy a faster machine. Tests are cpu/
Thank youQ/A
BUT single threaded tests get slower02550751002007 2008 2009 2010
Use all the cores with Maven3mvn -T1 clean install : 5:05smvn -T4 clean install : 3:10sBeware oftests with sideeffects
It’s not just me
Use all the cores with JUnit/TestNGBeware oftests with sideeffects
That’s it for cheatersNo silver bullet
The Lazy
Delete useless testsIt’s so simple, we don’t do it...
Even better, delete dead code
Even better, delete dead codeLeads to deleting even more useless tests
Work in a sandboxIn-memory database H2 behaves much likeMySqlAs simple aschanging anxml fileThe database is slow
It’s Not only SQLIf going for NoSQL, choose a server thatcan run in-processeg. VoldemortThe network is slow
In-memory SMTP ServerSubEtha SMTP
Abstract the filesystemApache VFS (Virtual File System)Spring Resource...
In-memory everythingAs a bonus, testswill run smoother
Of course all of these are easierto setup at the beginning of aproject
The
5 minutes a day can make adifference
Don’t test business rules in integrationUnit tests are often a better place
Not the same scale of speed10sec.01sec
Action #1 - Break the longer integrationInto one faster integration test and a lot of small
Action #2 - Mock the slowest layersMocks arenot just forunit testseg. with Spring and Mockito spies
Action #3 - Don’t test through theSelenium is often overkill«But my application iscomplex!»«My users want complexfeatures,My users want Ajax»«I need to test browsercompatibility!»
Really?
Complexity has a costThat you pay each time tests run
Test through the browser the strictUnit test Javascript
Action #4 - Sometimes be old schoolwrite (easier to test) server-side code
One more thing™Action #0 - Simplify and optimize your codeTests will run fasterKeep that onesecret...