Slide 1

Slide 1 text

Test-Driven Development in Android Writing tests without (that much) pain

Slide 2

Slide 2 text

Do you have unit tests on your project?

Slide 3

Slide 3 text

How do you know your project works?

Slide 4

Slide 4 text

What is a unit test? @Test public void testSqrtWith25 () { //PREPARE SomeObjectToTest object = new SomeObjectToTest() ; //PERFORM int result = object.sqrt( 25); //CHECK assertEquals(5, result); }

Slide 5

Slide 5 text

Software testing pyramid

Slide 6

Slide 6 text

Software testing ice cream cone

Slide 7

Slide 7 text

Unit tests benefits 1) The only documentation that is always up to date 2) Making refactoring really possible 3) Adding new features without risks 4) Continuous testing

Slide 8

Slide 8 text

Knowing it works before (manually) testing it

Slide 9

Slide 9 text

F.I.R.S.T 1) Fast 2) Independent 3) Repeatable 4) Self-Validating 5) Timely Three 1’s 1) ONE logical branch 2) ONE concept to test 3) ONE way to structure Your testing is a joke © James Thomas

Slide 10

Slide 10 text

Smells of bad tests in the morning 1) If 2) assertTrue(x == y); 3) Evergreen tests 4) Test avalanche twice a week 5) Discussing private matters with your test 6) Reflection 7) Testing toString() 8) It’s hard to name the test

Slide 11

Slide 11 text

If your code wasn’t designed to be testable - it is not 1) Non-injected dependencies 2) Android framework dependencies 3) Static methods and variables

Slide 12

Slide 12 text

Run it!

Slide 13

Slide 13 text

Ok, so what is TDD?

Slide 14

Slide 14 text

Three Rules of TDD 1) If there is no test - there is no code 2) You write as much of the test as required for it to fail 3) You write as much of the code as required for the test to pass

Slide 15

Slide 15 text

Step 1: If there is no test - there is no code @Test public void testSqrtWith25 () { //PREPARE SomeObjectToTest object = new SomeObjectToTest() ; //PERFORM int result = object.sqrt( 25); //CHECK } public class SomeObjectToTest {}

Slide 16

Slide 16 text

Step 2: make your test pass... @Test public void testSqrtWith25 () { //PREPARE SomeObjectToTest object = new SomeObjectToTest() ; //PERFORM int result = object.sqrt( 25); //CHECK } public class SomeObjectToTest { public int sqrt(int value) { return 0; } }

Slide 17

Slide 17 text

Step 3: ...and then make it fail again @Test public void testSqrtWith25 () { //PREPARE SomeObjectToTest object = new SomeObjectToTest() ; //PERFORM int result = object.sqrt( 25); //CHECK assertEquals(5, result); //assertTrue(5 == result); } public class SomeObjectToTest { public int sqrt(int value) { return 0; } }

Slide 18

Slide 18 text

Unit test in TDD is the first user of your new functionality

Slide 19

Slide 19 text

What TDD is not 1) It’s not a religion - you change the rules the way it fits you 2) It’s not a silver bullet - it’s not making your code perfect instantly 3) It’s not a universal solution - it might be not required

Slide 20

Slide 20 text

Deciding on TDD/TLD/FTT

Slide 21

Slide 21 text

Thanks!