SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
TESTING
Tomek Polanski
tpolansk
WHY?
WHEN?
HOW?
“Too much time pressure with making
visible changes.
Customer doesn't appreciate tests enough.”
“To make iterations as fast as possible I
would not use tests for prototypes or early
MVPs.”
“Specs are too volatile, tests would just
need to be updated constantly.”
“Writing tests is hard, they might be
difficult to understand.”
“Nobody else in the project uses them.
They weren't done or were broken when I
got involved.”
Time and budget
“Too much time
pressure with making
visible changes”
Instant changes Return on investment
vs
Speed
“Not for prototypes
or early MVPs”
Time
Progress
Prototype
Minimum
Viable
Product
Lovable
by Henrik Kniberg
Constant ChangeThe
“Specs are too
volatile”
Programming is hard
https://deeguns.files.wordpress.com/2013/07/computerman.jpg
urbanattitude.fr/wp-content/uploads/2015/03/BER_08.jpg
Architecture
“Tests are hard and
difficult to understand”
Maintenance
“Missing/broken
tests”
What should unit tests
look like?
Fast
Reliable
Simple to understand
@Test
public void get_whenElementExists() {
List<String> list = Collections.singletonList(DEFAULT);
assertThat(get(list, 0)).isEqualTo(DEFAULT);
}
@Test
public void get_whenIndexTooBig_returnNull() {
List<String> list = Collections.singletonList(DEFAULT);
assertThat(get(list, 1)).isNull();
}
@Test
public void get_whenIndexTooSmall_returnNull() {
List<String> list = Collections.singletonList(DEFAULT);
assertThat(get(list, -1)).isNull();
}
Simple to understand?
Arrange Act Assert
Test setup
@Test
public 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
}
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;
}
}
@Test
public 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
}
@Test
public void test() {
new ArrangeBuilder()
.withLanguages(GERMAN, ENGLISH)
.withGreetings(LanguageCode.EN, "Hi!");
// The rest of the test
}
Arrange builder
Simple to write
Is it all?
Do I have enough tests?
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?
@Test
public void isTwoEven() {
assertThat(isEven(2))
.isTrue();
}
static boolean isEven(int number) {
if ( number % 2 == 0 ) {
return true;
} else {
return false;
}
}
What’s the code coverage?
Observable<Boolean> isEven(int number) {
return Observable.just(number)
.map(num -> num % 2 == 0);
}
@Test
public void isTwoEven() {
TestSubscriber<Boolean> ts = new TestSubscriber<>();
isEven(2).subscribe(ts);
ts.assertValue(true);
}
@Test
public void isTwoEven() {
TestSubscriber<Boolean> ts = new TestSubscriber<>();
isEven(2).subscribe(ts);
}
Do I have too many tests?
Do I have too much code?
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;
}
@Override
public int hashCode() {
return mText != null ? mText.hashCode() : 0;
}
@Override
public 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;
}
}
. @Test
public void valueEquality() {
String value = "text";
Translation first = Translation.create(value);
Translation second = Translation.create(value);
assertThat(first).isEqualTo(second);
}
@AutoValue
public abstract class Translation {
public abstract String text();
public static Translation create(String text) {
return new AutoValue_Translation(text);
}
@Override
public int hashCode() {
// ...
}
@Override
public boolean equals(Object o) {
// ...
}
}
What is this code here?
Observable<String> getCurrentLanguage() {
return getSystemLanguage()
.skip(1)
.first();
}
Observable<String> getCurrentLanguage() {
return getSystemLanguage()
// .skip(1)
.first();
}
Test Driven Development
Less Code Less Tests Peace of mind
Be playful Don’t be dogmatic
Find a test mentor
“99 little bugs in the code
99 little bugs in the code
Take one down, patch I around
117 little bugs in the code”
“Stop breaking things!”
Tracy Rolling
Thank you!
tpolansk

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Android Unit Testing With Robolectric
Android Unit Testing With RobolectricAndroid Unit Testing With Robolectric
Android Unit Testing With Robolectric
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
JS and patterns
JS and patternsJS and patterns
JS and patterns
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
ReactJS for Programmers
ReactJS for ProgrammersReactJS for Programmers
ReactJS for Programmers
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008
 
GeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassleGeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassle
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
 
Introduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxIntroduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicox
 
Testing in android
Testing in androidTesting in android
Testing in android
 
Going On with the Check of Geant4
Going On with the Check of Geant4Going On with the Check of Geant4
Going On with the Check of Geant4
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDD
 
關於測試,我說的其實是......
關於測試,我說的其實是......關於測試,我說的其實是......
關於測試,我說的其實是......
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
 
Qunit Java script Un
Qunit Java script UnQunit Java script Un
Qunit Java script Un
 
groovy & grails - lecture 7
groovy & grails - lecture 7groovy & grails - lecture 7
groovy & grails - lecture 7
 

Andere mochten auch

Andere mochten auch (10)

Little Helpers for Android Development with Kotlin
Little Helpers for Android Development with KotlinLittle Helpers for Android Development with Kotlin
Little Helpers for Android Development with Kotlin
 
Add ClassyShark to your Android toolbox
Add ClassyShark to your Android toolboxAdd ClassyShark to your Android toolbox
Add ClassyShark to your Android toolbox
 
Creating Gradle Plugins - Oredev
Creating Gradle Plugins - OredevCreating Gradle Plugins - Oredev
Creating Gradle Plugins - Oredev
 
A realtime infrastructure for Android apps: Firebase may be what you need..an...
A realtime infrastructure for Android apps: Firebase may be what you need..an...A realtime infrastructure for Android apps: Firebase may be what you need..an...
A realtime infrastructure for Android apps: Firebase may be what you need..an...
 
Developing Apps for Emerging Markets
Developing Apps for Emerging MarketsDeveloping Apps for Emerging Markets
Developing Apps for Emerging Markets
 
Develop Maintainable Apps - edUiConf
Develop Maintainable Apps - edUiConfDevelop Maintainable Apps - edUiConf
Develop Maintainable Apps - edUiConf
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기
 
MVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixMVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mix
 
2016 FunctionCup 풀이
2016 FunctionCup 풀이2016 FunctionCup 풀이
2016 FunctionCup 풀이
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
 

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

Ähnlich wie Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how (20)

Design for Testability
Design for TestabilityDesign for Testability
Design for Testability
 
Mutation testing
Mutation testingMutation testing
Mutation testing
 
Mutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The BugsMutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The Bugs
 
Mutation testing Bucharest Tech Week
Mutation testing Bucharest Tech WeekMutation testing Bucharest Tech Week
Mutation testing Bucharest Tech Week
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 
7 stages of unit testing
7 stages of unit testing7 stages of unit testing
7 stages of unit testing
 
Improve unit tests with Mutants!
Improve unit tests with Mutants!Improve unit tests with Mutants!
Improve unit tests with Mutants!
 
Google test training
Google test trainingGoogle test training
Google test training
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLID
 
First steps in testing analytics: Does test code quality matter?
First steps in testing analytics: Does test code quality matter?First steps in testing analytics: Does test code quality matter?
First steps in testing analytics: Does test code quality matter?
 
Mutation Testing
Mutation TestingMutation Testing
Mutation Testing
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Getting Started with Test-Driven Development at Midwest PHP 2021
Getting Started with Test-Driven Development at Midwest PHP 2021Getting Started with Test-Driven Development at Midwest PHP 2021
Getting Started with Test-Driven Development at Midwest PHP 2021
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PIT
 
utPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLutPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQL
 
STAMP Descartes Presentation
STAMP Descartes PresentationSTAMP Descartes Presentation
STAMP Descartes Presentation
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
The real beginner's guide to android testing
The real beginner's guide to android testingThe real beginner's guide to android testing
The real beginner's guide to android testing
 

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

  • 2. “Too much time pressure with making visible changes. Customer doesn't appreciate tests enough.”
  • 3. “To make iterations as fast as possible I would not use tests for prototypes or early MVPs.”
  • 4. “Specs are too volatile, tests would just need to be updated constantly.”
  • 5. “Writing tests is hard, they might be difficult to understand.”
  • 6. “Nobody else in the project uses them. They weren't done or were broken when I got involved.”
  • 7. Time and budget “Too much time pressure with making visible changes”
  • 8. Instant changes Return on investment vs
  • 16. Architecture “Tests are hard and difficult to understand”
  • 18. What should unit tests look like?
  • 19. Fast
  • 21. Simple to understand @Test public void get_whenElementExists() { List<String> list = Collections.singletonList(DEFAULT); assertThat(get(list, 0)).isEqualTo(DEFAULT); } @Test public void get_whenIndexTooBig_returnNull() { List<String> list = Collections.singletonList(DEFAULT); assertThat(get(list, 1)).isNull(); } @Test public void get_whenIndexTooSmall_returnNull() { List<String> list = Collections.singletonList(DEFAULT); assertThat(get(list, -1)).isNull(); }
  • 24. Test setup @Test public 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 }
  • 25. 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; } }
  • 26. @Test public 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 } @Test public void test() { new ArrangeBuilder() .withLanguages(GERMAN, ENGLISH) .withGreetings(LanguageCode.EN, "Hi!"); // The rest of the test } Arrange builder
  • 29. Do I have enough tests?
  • 30. 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? @Test public void isTwoEven() { assertThat(isEven(2)) .isTrue(); } static boolean isEven(int number) { if ( number % 2 == 0 ) { return true; } else { return false; } }
  • 31. What’s the code coverage? Observable<Boolean> isEven(int number) { return Observable.just(number) .map(num -> num % 2 == 0); } @Test public void isTwoEven() { TestSubscriber<Boolean> ts = new TestSubscriber<>(); isEven(2).subscribe(ts); ts.assertValue(true); } @Test public void isTwoEven() { TestSubscriber<Boolean> ts = new TestSubscriber<>(); isEven(2).subscribe(ts); }
  • 32. Do I have too many tests?
  • 33. Do I have too much code?
  • 34. 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; } @Override public int hashCode() { return mText != null ? mText.hashCode() : 0; } @Override public 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; } } . @Test public void valueEquality() { String value = "text"; Translation first = Translation.create(value); Translation second = Translation.create(value); assertThat(first).isEqualTo(second); } @AutoValue public abstract class Translation { public abstract String text(); public static Translation create(String text) { return new AutoValue_Translation(text); } @Override public int hashCode() { // ... } @Override public boolean equals(Object o) { // ... } }
  • 35. What is this code here? Observable<String> getCurrentLanguage() { return getSystemLanguage() .skip(1) .first(); } Observable<String> getCurrentLanguage() { return getSystemLanguage() // .skip(1) .first(); }
  • 36. Test Driven Development Less Code Less Tests Peace of mind
  • 37. Be playful Don’t be dogmatic Find a test mentor
  • 38. “99 little bugs in the code 99 little bugs in the code Take one down, patch I around 117 little bugs in the code”