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

Unit testing

Unit testing

Talk about Unit Testing: Pet / Cin / Universidade Federal de Pernambuco.
https://www.facebook.com/events/372202459624692/

Cloning and running tests on senna https://asciinema.org/a/14458

Henrique Vicente

December 05, 2014
Tweet

More Decks by Henrique Vicente

Other Decks in Programming

Transcript

  1. Unit testing Henrique Vicente

    henvic.github.io

    View Slide

  2. View Slide

  3. Why
    • Legacy code = code without tests
    • Safety net = fewer defects
    • Test cases = free and up-to-date docs
    • Prove that your code works
    • Ensure changes don’t break a thing

    View Slide

  4. Test-Driven Development
    • Programming technique
    • Part of Agile methodologies, including XP

    View Slide

  5. Write failing
    test case
    Verify all
    tests pass
    Change
    code

    View Slide

  6. View Slide

  7. Challenges
    • Lack of company culture
    • Legacy code
    • Code coverage
    • Experimenting
    • Dependencies
    • Data providers
    • Nondeterministic algorithms

    View Slide

  8. Unit tests
    • Independent from each other
    • Runs damn fast (ms to seconds)
    • Essential if you decide to use TDD
    • Don’t test private methods, cover it indirectly

    View Slide

  9. Mocks
    • Isolate dependencies by mocking
    • Dependency injection / inversion of control to the
    rescue!

    View Slide

  10. Data provider
    • Mock your data
    • Unit tests shouldn’t talk with SQL databases,
    remote procedures, and so on.

    View Slide

  11. www.mockaroo.com

    View Slide

  12. Integration testing
    • Testing of your unit tests in conjunction
    • Testing several production code

    View Slide

  13. Continuous Integration
    • Not to be confused with integration testing
    • Useful for integration testing
    • Let your developers know the status of your
    software builds

    View Slide

  14. build status

    View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. View Slide

  20. Java HelloWorldTest

    View Slide

  21. View Slide

  22. View Slide

  23. Run the unit test here!

    View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. View Slide

  29. It’ll ask you to
    accept license and,
    once installed, to
    restart Eclipse.

    View Slide

  30. Run the coverage!
    http://www.eclemma.org/faq.html

    View Slide

  31. Bad loop, but covered

    View Slide

  32. Why are JUnit4 test cases with
    expected exceptions shown as
    not covered?
    http://www.eclemma.org/faq.html

    View Slide

  33. KISS
    Keep ignoring.
    Refactor!

    View Slide

  34. View Slide

  35. View Slide

  36. JUnit tests annotations
    • @Test
    • @Before
    • @After
    • @BeforeClass
    • @AfterClass
    • @Ignore

    View Slide

  37. JUnit asserts
    • void assertEquals(boolean expected, boolean actual)
    • void assertTrue(boolean expected, boolean actual)
    • void assertFalse(boolean condition)
    • void assertNotNull(Object object)
    • void assertNull(Object object)
    • void assertSame(boolean condition)
    • void assertNotSame(boolean condition)
    • void assertArrayEquals(expectedArray, resultArray);

    View Slide

  38. http://docs.travis-ci.com/user/languages/java/

    View Slide

  39. https://github.com/mplacona/java-junit-template-project

    View Slide

  40. JavaScript helloWorld()
    • Client? Server?
    • Mocha
    • Karma
    • many others

    View Slide

  41. Continuous Integration
    • Travis
    • Jenkins
    • others

    View Slide

  42. • pre-commit to abort commits
    without —no-verify
    • post-commit to give you an
    opportunity to —amend before
    pushing to a remote.

    View Slide

  43. View Slide

  44. View Slide

  45. View Slide

  46. https://travis-ci.org/eduardolundgren/senna/builds/40133436

    View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. https://github.com/caolan/nodeunit

    View Slide

  51. View Slide

  52. http://karma-runner.github.io/

    View Slide

  53. View Slide

  54. https://asciinema.org/a/14458

    View Slide

  55. • A Virtual Conference for Developers http://hacksummit.org/
    • http://en.wikipedia.org/wiki/File:Wistar_rat.jpg
    • OMG PONIES!!! (AKA HUMANITY: EPIC FAIL)

    http://codeblog.jonskeet.uk/2009/11/02/omg-ponies-aka-humanity-epic-fail/ or Jon Skeet’s
    video at hack.summit
    • http://en.wikipedia.org/wiki/File:Test-driven_development.PNG
    • http://www.zyracuze.com/for-your-information/kiss-keep-it-simple-stupid/
    • Watch Uncle Bob (Robert Martin) videos, read his Code Clean and Coder Clean books, and
    if you have to deal with legacy code, try Working Effectively with Legacy Code.

    View Slide