$30 off During Our Annual Pro Sale. View Details »

Groovy/Spock for Android app testing

Groovy/Spock for Android app testing

Slide for potatotips #22 http://connpass.com/event/20240/

---

Use Spock testing framework for unitt test on Android.

Masayuki Izumi

October 13, 2015
Tweet

More Decks by Masayuki Izumi

Other Decks in Programming

Transcript

  1. Groovy/Spock
    for Android app testing
    Masayuki IZUMI - @izumin5210
    potatotips #22

    View Slide


  2. > Masayuki IZUMI a.k.a. @izumin5210
    > Rekimoto Lab. at the University of Tokyo
    > Wantedly, Inc.
    > Rubyist / Androider / {Java,Type}Scripter

    View Slide

  3. Ruby
    JavaScript
    Android
    Design

    View Slide

  4. Parameterized test
    with JUnit

    View Slide

  5. public class GreeterTest {
    @Test
    public void sayHello() throws Exception {
    Greeter greeter = new Greeter();
    assertEquals("Hello", greeter.say());
    }
    }

    View Slide

  6. public class GreeterTest {
    private Greeter greeter;
    @Before
    public void setUp() throws Exception {
    greeter = new Greeter();
    }
    @Test
    public void sayMorning() throws Exception {
    assertEquals("Good morning", greeter.say("morning"));
    }
    @Test
    public void sayHello() throws Exception {
    assertEquals("Hello", greeter.say("afternoon"));
    }
    }

    View Slide

  7. public class GreeterTest {
    private Greeter greeter;
    @Before
    public void setUp() throws Exception {
    greeter = new Greeter();
    }
    @Test
    public void sayMorning() throws Exception {
    assertEquals("Good morning", greeter.say("morning"));
    }
    @Test
    public void sayHello() throws Exception {
    assertEquals("Hello", greeter.say("afternoon"));
    }
    @Test
    public void sayNight() throws Exception {
    assertEquals("Good night", greeter.say("night"));
    }
    }

    View Slide

  8. public class GreeterTest {
    @Test
    public void say() throws Exception {
    Greeter greeter = new Greeter();
    String[][] pairs = {
    {"morning", "Good morning"},
    {"afternoon", "Hello"},
    {"night", "Good night"}
    };
    for (String[] pair : pairs) {
    assertEquals(pair[1], greeter.say(pair[0]));
    }
    }
    }

    View Slide

  9. @RunWith(Theories.class)
    public class GreeterTest {
    public static class Fixture { /* ... */ }
    @DataPoints
    public static Fixture[] getParameters() {
    return new Fixture[] {
    new Fixture("morning", "Good morning"), /* ... */
    };
    }
    @Theory
    public void say(Fixture fixture) throws Exception {
    Greeter greeter = new Greeter();
    assertEquals(fixture.getExpected(), greeter.say(fixture.getArg()));
    }
    }

    View Slide

  10. Parameterized test with JUnit
     Too complecated and verbose
     Stop execution when assertion failed
    - run -> fail -> fix -> run -> fail -> fix -> run -> fail ...

    View Slide

  11. Spock
    “The Enterprise-ready testing and specification framework.”
    https://github.com/spockframework/spock
    https://www.flickr.com/photos/124561666@N02/14218023847

    View Slide

  12. Spock
     Testing framework written in Groovy
     Inspired from JUnit, RSpec, Mockito, ...
     Very fluent specification DSL (like RSpec?)
     Descriptive assertion message (Power Assert!!!)

    View Slide

  13. Installation
    buildscript {
    repositories {
    jcenter()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
    classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
    }
    }

    View Slide

  14. Installation
    buildscript {
    repositories {
    jcenter()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
    classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
    }
    }

    View Slide

  15. Installation
    buildscript {
    repositories {
    jcenter()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
    classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
    }
    }
    gradle-groovy-android-plugin 0.3.6
    has not supported
    android gradle plugin 1.3 higher...

    View Slide

  16. Installation
    dependencies {
    // ...
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
    }

    View Slide

  17. GreeterSpec with Spock
    class GreeterSpec extends Specification {
    def "#say()"() {
    expect:
    expected == new Greeter().say(period)
    where:
    period | expected
    "morning" | "Good morning"
    "afternoon" | "Hello"
    "night" | "Good night"
    }
    }

    View Slide

  18. GreeterSpec with Spock
    class GreeterSpec extends Specification {
    def "#say()"() {
    expect:
    expected == new Greeter().say(period)
    where:
    period | expected
    "morning" | "Good morning"
    "afternoon" | "Hello"
    "night" | "Good night"
    }
    }

    View Slide

  19. GreeterSpec with Spock
    class GreeterSpec extends Specification {
    def "#say()"() {
    expect:
    expected == new Greeter().say(period)
    where:
    period | expected
    "morning" | "Good morning"
    "afternoon" | "Hello"
    "night" | "Good night"
    }
    }

    View Slide

  20. Assertion message

    View Slide

  21. Assertion message

    View Slide

  22. JUnit
    v.s.
    Spock

    View Slide

  23. https://speakerdeck.com/jnchito/number-kanrk06

    View Slide

  24. Groovy/Spock
    for Android app testing
    Masayuki IZUMI - @izumin5210
    potatotips #22

    View Slide