Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

40
TESTING Tomek Polanski tpolansk WHY? WHEN? HOW?

Transcript of Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Page 1: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

TESTING

Tomek Polanskitpolansk

WHY?WHEN?HOW?

Page 2: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

“Too much time pressure with making visible changes.

Customer doesn't appreciate tests enough.”

Page 3: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

“To make iterations as fast as possible I would not use tests for prototypes or early

MVPs.”

Page 4: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

“Specs are too volatile, tests would just need to be updated constantly.”

Page 5: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

“Writing tests is hard, they might be difficult to understand.”

Page 6: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

“Nobody else in the project uses them.

They weren't done or were broken when I got involved.”

Page 7: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Time and budget“Too much time

pressure with making visible changes”

Page 8: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Instant changes Return on investment

vs

Page 9: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Speed

“Not for prototypes or early MVPs”

Page 10: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Time

Pro

gres

s

Page 11: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Prototype

Page 12: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Minimum Viable

ProductLovable

by Henrik Kniberg

Page 13: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Constant ChangeThe

“Specs are too volatile”

Page 14: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Programming is hard

https://deeguns.files.wordpress.com/2013/07/computerman.jpg

Page 15: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

urbanattitude.fr/wp-content/uploads/2015/03/BER_08.jpg

Page 16: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Architecture

“Tests are hard and difficult to understand”

Page 17: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Maintenance

“Missing/broken tests”

Page 18: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

What should unit tests look like?

Page 19: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Fast

Page 20: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Reliable

Page 21: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Simple to understand

@Testpublic void get_whenElementExists() {

List<String> list = Collections.singletonList(DEFAULT);

assertThat(get(list, 0)).isEqualTo(DEFAULT);}

@Testpublic void get_whenIndexTooBig_returnNull() {

List<String> list = Collections.singletonList(DEFAULT);

assertThat(get(list, 1)).isNull();}

@Testpublic void get_whenIndexTooSmall_returnNull() {

List<String> list = Collections.singletonList(DEFAULT);

assertThat(get(list, -1)).isNull();}

Page 22: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Simple to understand?

Page 23: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Arrange Act Assert

Page 24: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Test setup

@Testpublic void test() {

List<Language> list = Arrays.asList(GERMAN, ENGLISH);Mockito.when(mDataModel.getSupportedLanguages())

.thenReturn(list);Mockito.when(mDataModel.getGreetingByLanguageCode(LanguageCode.EN))

.thenReturn("Hi!");

// The rest of the test}

Page 25: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Arrange builder

class ArrangeBuilder {

ArrangeBuilder withLanguages(Language... languages) {Mockito.when(mDataModel.getSupportedLanguages())

.thenReturn(Arrays.asList(languages));return this;

}

ArrangeBuilder withGreetings(LanguageCode code, String greeting) {Mockito.when(mDataModel.getGreetingByLanguageCode(code))

.thenReturn(greeting);return this;

}}

Page 26: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

@Testpublic void test() {

List<Language> list = Arrays.asList(GERMAN, ENGLISH);Mockito.when(mDataModel.getSupportedLanguages())

.thenReturn(list);Mockito.when(mDataModel.getGreetingByLanguageCode(LanguageCode.EN))

.thenReturn("Hi!");

// The rest of the test}

@Testpublic void test() {

new ArrangeBuilder().withLanguages(GERMAN, ENGLISH).withGreetings(LanguageCode.EN, "Hi!");

// The rest of the test}

Arrange builder

Page 27: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Simple to write

Page 28: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Is it all?

Page 29: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Do I have enough tests?

Page 30: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

static boolean isEven(int number) {return number % 2 == 0;

}

static boolean isEven(int number) {return number % 2 == 0 ? true : false;

}

What’s the code coverage?

@Testpublic void isTwoEven() {

assertThat(isEven(2)).isTrue();

}

static boolean isEven(int number) {if ( number % 2 == 0 ) {

return true;} else {

return false;}

}

Page 31: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

What’s the code coverage?

Observable<Boolean> isEven(int number) {return Observable.just(number)

.map(num -> num % 2 == 0);}

@Testpublic void isTwoEven() {

TestSubscriber<Boolean> ts = new TestSubscriber<>();

isEven(2).subscribe(ts);

ts.assertValue(true);}

@Testpublic void isTwoEven() {

TestSubscriber<Boolean> ts = new TestSubscriber<>();

isEven(2).subscribe(ts);}

Page 32: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Do I have too many tests?

Page 33: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Do I have too much code?

Page 34: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

public class Translation {

public final String mText;

public Translation(String text) {mText = text;

}

public String text() {return mText;

}} @AutoValue

public abstract class Translation {

public abstract String text();

public static Translation create(String text) {return new AutoValue_Translation(text);

}}

.public class Translation {

private final String mText;

public Translation(String text) {mText = text;

}

public String text() {return mText;

}

@Overridepublic int hashCode() {

return mText != null ? mText.hashCode() : 0;}

@Overridepublic boolean equals(Object o) {

if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;

Translation that = (Translation) o;

return mText != null ? mText.equals(that.mText) : that.mText == null;

}}

.@Testpublic void valueEquality() {

String value = "text";Translation first = Translation.create(value);Translation second = Translation.create(value);

assertThat(first).isEqualTo(second);}

@AutoValuepublic abstract class Translation {

public abstract String text();

public static Translation create(String text) {return new AutoValue_Translation(text);

}

@Overridepublic int hashCode() {

// ...}

@Overridepublic boolean equals(Object o) {

// ...}

}

Page 35: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

What is this code here?

Observable<String> getCurrentLanguage() {return getSystemLanguage()

.skip(1)

.first();}

Observable<String> getCurrentLanguage() {return getSystemLanguage()

// .skip(1).first();

}

Page 36: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Test Driven Development

Less Code Less Tests Peace of mind

Page 37: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Be playful Don’t be dogmatic

Find a test mentor

Page 38: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

“99 little bugs in the code

99 little bugs in the code

Take one down, patch I around

117 little bugs in the code”

Page 39: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

“Stop breaking things!”Tracy Rolling

Page 40: Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how

Thank you!

tpolansk