Slide 1

Slide 1 text

Unit testing Henrique Vicente
 henvic.github.io

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Write failing test case Verify all tests pass Change code

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

www.mockaroo.com

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

build status

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Java HelloWorldTest

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Run the unit test here!

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Bad loop, but covered

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

KISS Keep ignoring. Refactor!

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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);

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Continuous Integration • Travis • Jenkins • others

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

https://github.com/caolan/nodeunit

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

https://asciinema.org/a/14458

Slide 55

Slide 55 text

• 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.