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

5733fd332f2a0da11931e0e73ddfb20d?s=128

Henrique Vicente

December 05, 2014
Tweet

Transcript

  1. Unit testing Henrique Vicente
 henvic.github.io

  2. None
  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
  4. Test-Driven Development • Programming technique • Part of Agile methodologies,

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

  6. None
  7. Challenges • Lack of company culture • Legacy code •

    Code coverage • Experimenting • Dependencies • Data providers • Nondeterministic algorithms
  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
  9. Mocks • Isolate dependencies by mocking • Dependency injection /

    inversion of control to the rescue!
  10. Data provider • Mock your data • Unit tests shouldn’t

    talk with SQL databases, remote procedures, and so on.
  11. www.mockaroo.com

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

    • Testing several production code
  13. Continuous Integration • Not to be confused with integration testing

    • Useful for integration testing • Let your developers know the status of your software builds
  14. build status

  15. None
  16. None
  17. None
  18. None
  19. None
  20. Java HelloWorldTest

  21. None
  22. None
  23. Run the unit test here!

  24. None
  25. None
  26. None
  27. None
  28. None
  29. It’ll ask you to accept license and, once installed, to

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

  31. Bad loop, but covered

  32. Why are JUnit4 test cases with expected exceptions shown as

    not covered? http://www.eclemma.org/faq.html
  33. KISS Keep ignoring. Refactor!

  34. None
  35. None
  36. JUnit tests annotations • @Test • @Before • @After •

    @BeforeClass • @AfterClass • @Ignore
  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);
  38. http://docs.travis-ci.com/user/languages/java/

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

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

    many others
  41. Continuous Integration • Travis • Jenkins • others

  42. • pre-commit to abort commits without —no-verify • post-commit to

    give you an opportunity to —amend before pushing to a remote.
  43. None
  44. None
  45. None
  46. https://travis-ci.org/eduardolundgren/senna/builds/40133436

  47. None
  48. None
  49. None
  50. https://github.com/caolan/nodeunit

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

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

  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.