Test Driven Development
with %UnitTest

Marc Stober

Software Engineer, Partners HealthCare

Presentation to NEMUG, November 13, 2012

Who am I?

I WANT YOU TO DO TDD WITH %UnitTest
Image source: Wikipedia

unit test /YOO-nit test/ noun

  1. Small scale tests
  2. Testing that uses JUnit-like (xUnit) tools (any scale)
  3. Test Driven Development

A short history...

Test Driven Development (TDD)

Test First

First thing you do is write a test that fails.

It's not a way of testing: It's a way of (thinking about) development.

red/green/refactor

Small steps, incremental cycle

Re-run. Work clean. Keep fast!

Continuous, automated regression testing.

Pros

Cons

  • Test in language you code in
  • Automated regression tests
  • Not a replacement for SQA
  • Helps design complex logic
  • Encourages short, functional (no side-effects) methods of maintainable, reusable, testable code
  • Easier to refactor
  • Hard to retrofit existing logic
  • Some things are hard to test: UI, dependencies (mocks/DI)
  • Saves time overall
  • Takes more time
"Anyone who continues to think that TDD slows you down is living in the stone age...it's like sterile procedure for surgeons." -- "Uncle" Bob Martin*
  • Keeps you disciplined
  • Makes you dogmatic
  • Programmers taking responsibility for quality of work
  • Programmers taking responsibility for quality of work J
*https://sites.google.com/site/unclebobconsultingllc/home/articles/echoes-from-the-stone-age

Pro Tips

Don't break the build!

Code Coverage: is 100% enough, or too much?

DRY doesn’t apply so much. Beck: "every test should tell a story"

Usually don't move to Production

Gamification?

 

Writing a test

Structure of a test: AAA Pattern

* Arrange (set up)

* Act (run)

* Assert

Test suite/case (class)

Test (method)

http://c2.com/cgi/wiki?ArrangeActAssert

My extensions for Cache unit testing

Adds Run method (no import/export!)

Wildcard

Summary

Color!

 

Demonstration Time!

Requirements

IsEmpty(string) should return true for empty string, spaces, $c(0).

IsEmptyDOB(id) should return true if pt identified by id has empty DOB. (time permitting)

Dependencies

Extensions [Download]:

Testing.TestCase

Testing.Manager

Steps

Thank you! Questions?

Slides and code:

http://www.marcstober.com/blog/2012/11/13/nemug-presentation-on-test-driven-development/

Contact:

marcstober@gmail.com

@marcstober

Further reading

http://www.martinfowler.com/bliki/Xunit.html

http://xprogramming.com/software/ (list of xUnit frameworks)

http://blog.jphpsf.com/2012/09/30/OMG-test-driven-development-actually-works/

http://www.drdobbs.com/testing/unit-testing-is-there-really-any-debate/240007176#

Powered by deck.js. Fonts from The League of Moveable Type.

/

#