Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Started With Unit Testing

Getting Started With Unit Testing

How I got started with unit testing, a brief introduction to what unit testing is, and tips for getting started.

Alison Barrett

November 30, 2013
Tweet

More Decks by Alison Barrett

Other Decks in Programming

Transcript

  1. G E T T I N G S TA R

    T E D W I T H 
 U N I T T E S T I N G A L I S O N B A R R E T T ’ S
  2. A L I S O N B A R R

    E T T • Code Wrangler at Automattic • @alisothegeek • http://alisothegeek.com
  3. S O M E T I M E I N

    2 0 1 1 Unit testing? What’s that? I’ll go find out.
  4. Unit testing waw waw waw waw waw waw waw waw

    waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw waw…
  5. S O M E T I M E I N

    2 0 1 2 Oh yeah, unit testing. I should learn that sometime.
  6. L AT E R I N 2 0 1 2

    Oh so that’s what unit tests are…
  7. L AT E R I N 2 0 1 2

    OMG I AM SCARED PLZ REMOVE UNIT TESTS FROM BRAIN
  8. R AT I O N A L I Z AT

    I O N Only crazy-smart devs use it. I’m not smart enough.
  9. R AT I O N A L I Z AT

    I O N It’s a whole new framework. No time to learn it.
  10. R AT I O N A L I Z AT

    I O N I bet it’s hard to set up.
  11. R AT I O N A L I Z AT

    I O N It’s too hard.
  12. J U N E 2 0 1 3 I guess

    I’m learning unit testing today.
  13. J U N E 2 0 1 3 OMG THIS

    IS SO EASY AND FUN AND AMAZING
  14. J U N E 2 0 1 3 UNIT TEST

    ALL THE THINGS
  15. A L S O I N J U N E

    I need a new topic for WCSF… Unit testing!
  16. E X P E R T A D V O

    C AT E E X P E R T AT A D V O C AT I N G
  17. W H AT I S U N I T T

    E S T I N G ?
  18. In computer programming, unit testing is a method by which

    individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application. In procedural programming, a unit could be an entire module, but is more commonly an individual function or procedure. In object-oriented programming, a unit is often an entire interface, such as a class, but could be an individual method. Unit tests are created by programmers or occasionally by white box testers during the development process. T L ; D R ?
  19. D O E S T H I S F U

    N C T I O N W O R K ?
  20. assertArrayHasKey() assertClassHasAttribute() assertClassHasStaticAttribute() assertContains() assertContainsOnly() assertContainsOnlyInstancesOf() assertCount() assertEmpty() assertEqualXMLStructure() assertEquals()

    assertFalse() assertFileEquals() assertFileExists() assertGreaterThan() assertGreaterThanOrEqual() assertInstanceOf() assertInternalType() assertJsonFileEqualsJsonFile() assertJsonStringEqualsJsonFile() assertJsonStringEqualsJsonString() assertLessThan() assertLessThanOrEqual() assertNull() assertObjectHasAttribute() assertRegExp() assertStringMatchesFormat() assertStringMatchesFormatFile() assertSame() assertSelectCount() assertSelectEquals() assertSelectRegExp() assertStringEndsWith() assertStringEqualsFile() assertStringStartsWith() assertTag() assertThat() assertTrue() assertXmlFileEqualsXmlFile() assertXmlStringEqualsXmlFile() assertXmlStringEqualsXmlString() H T T P : / / A L I S O . M E / A S S E R T
  21. T I M E F O R S O M

    E E X P E RT A D V O C AT I N G
  22. C U R R E N T P R O

    B L E M Testing by debugging
  23. C U R R E N T P R O

    B L E M , W O R S E V E R S I O N Testing by debugging without version control or staging environments
  24. S O L U T I O N Unit testing!

    DUH (it’s the topic of this presentation)
  25. W H Y U N I T T E S

    T I N G ? Automation saves time Debugging is easier Code is future-proof Architecture is modular Tests double as documentation
  26. A U T O M AT I O N S

    AV E S T I M E
  27. D E B U G G I N G I

    S E A S I E R
  28. C O D E I S F U T U

    R E - P R O O F
  29. E V E RY O N E ’ S FAV

    O R I T E Refactoring
  30. A R C H I T E C T U

    R E I S M O D U L A R
  31. D O E S T H I S F U

    N C T I O N W O R K ?
  32. ??? it does a bazillion things D O E S

    T H I S F U N C T I O N W O R K ?
  33. YES Ask me a harder question for once D O

    E S T H I S F U N C T I O N W O R K ?
  34. T E S T S D O U B L

    E A S D O C U M E N TAT I O N
  35. Y O U UNIT TEST ALL THE THINGS WRONG (sort

    of) UNIT TEST ALL THE THINGS
  36. J U LY 2 0 1 3 That unit testing

    worked great on the last bug! I’ll use it for this bug too!
  37. 2 0 H O U R S L AT E

    R This would have taken 5 minutes… if it wasn't for you meddling unit tests!
  38. Y O U M I G H T N O

    T WA N T T O U N I T T E S T I F… • You’re on a really tight deadline. • The budget is really tight and speed is important. • You’re on a really tight deadline. • You’re on a really tight deadline and you can’t unit test like a boss (yet).
  39. I D E A Try it on a pro-bono project

    that has time to spare.