Testing Grails

Experiences from the Field

whoami

Colin Harrington

Principal Consultant
Object Partners, Inc

colin.harrington@objectpartners.com

@ColinHarrington

 Clustered, Load Balanced, Fault tolerant

Distributed applications


Distributed


Test the whole application. 
Virtualize!

Apps, 
Integration points.  
System as a whole.

Testing Perspectives


jUnit
Continuous Integration
Automated Deploy

Fifty Shades of Testing






How much pain do you want?

Spectrum

  • Spectrum of continually running
  • Autotest
  • Running in your IDE?
  • grails test-app unit: package or class
  • grails test-app unit:  (Just a phase)
  • grails test-app
  • Other phases & types

Know Your tools

Built-in stuff


  • Services 
  • Controllers
  • filters
  • domains
  • constraints
  • command objects
  • codecs 
  • GSP
  • UrlMappings
  • WebFlow


Spock!!

GMock
Groovy 


JUnit, jUnit4  Assertions
Spock

Mocking Tools


Continuous Integration


Jenkins


Phases and types

Segregate by package

parallell execution:
http://fbflex.wordpress.com/2011/12/01/a-script-to-run-grails-functional-tests-in-parallel/


Write descriptions in Spock Tests.
Make multiple line assertions
assert something != null
assert something.foo != null
assert something.foo.bar == 'baz' 

JUnit tests
JUnit style assertions.
vs assert statement with Groovy.

Spock implicit assertions
Spock interactions, cardinality.

Build your own tools

Third Party API


Sandbox APIs

BetaMax
Build your own.

APIs


Testing Third party APIs
Learning the API, 
Testing the API, 
Testing your code.
Testing them together 

(-warning- environments go down!)

Know your concepts


  • No IO in Unit tests
  • Unit tests just test your unit! 
  • Mocks/Spies/Stubs



Unit being tested:
Class, Hierarchy, MethodLevel.


Unit being tested:


  • Class
  • Hierarchy
  • Method

TDD 

vs 

Post-Tramatic-Test-Driven-Development






Ideal vs Reality


Bug finding & swatting.  
Write a new failing test confirming the bug to help you reproduce the issue. 

Not always possible/easy with race conditions -then refactor so you don't have these issues.  

Any area of code that is hard to test is prone to error and maintainability woes.  

Fix the bug and watch your tests pass!


New Feature development shouldn't be complete without tests!
  Developers & QA to enforce this. 

Each unit of work should be covered in some level of tests. 
 
IMO should be covered by multiple perspectives or dimensions.  
Unit tested and covered in functional testing.


Optimize developer workflow

Developer workflow


  • IDE support
  • Command line familiarity.
  • GROM
  • Alias for opening tests.
  • Automagically open your HTML reports.

Process

Cut out real time to make this a priority.



Write (some) Tests Fridays


WTF

Testing has to be taught & learned.

Test your Javascript

(or front-end)



karma
testem
mocha
jasmine

http://grails.org/plugin/karma-test-runner

Test production

Load Testing

Jmeter, stick it in prod, see what happens.  

Heard of a guy sticking HTTP Requests in REDIS and mass firing them off to create insane amounts of load.


Security Testing, 
Pen Testing, 
auditing.

Smoke Tests

Basic proof of core services 
configuration

Performance Testing

Performance Testing


Have metrics in place. 


Log aggregation:
 Make the cycle of testing, finding, fixing bugs faster.


Internal Bug tracking systems.  
JIRA does a decent job

VCS

Branching & Tagging matters

Gerrit workflow




*always* resonably stable.


Automate your whole deployments

script it out

eliminate/minimize manual interaction.

Test coverage fallacy

ClassUnderTest.metaClass.methods.each {
	try {
		...it.invokeMethod....
	} catch (Throwable th) {}	
} 

Test Quality



CodeNarc 

Code Coverage

http://gmetrics.sourceforge.net

People


  • Over-the-wall QA

  • Embedded QA - 
    • Building the code as you go.

  • Canary 

  • User QA/UAT

Logging


is

a

Feature

(Test it)


If a tree falls in the forest and no one is around to hear it, did it make a sound?  


If your code runs and left no traces of how it ran, how are you going to debug it?


Use Log Levels.  
More traceability = easier to fix

Thank you



@ColinHarrington
Made with Slides.com