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.

9eed44f137609e6ce3b6f1e14f80b9e1?s=128

Masayuki Izumi

October 13, 2015
Tweet

Transcript

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

    #22
  2.  > Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at

    the University of Tokyo > Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter
  3. Ruby JavaScript Android Design

  4. Parameterized test with JUnit

  5. public class GreeterTest { @Test public void sayHello() throws Exception

    { Greeter greeter = new Greeter(); assertEquals("Hello", greeter.say()); } }
  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")); } }
  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")); } }
  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])); } } }
  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())); } }
  10. Parameterized test with JUnit  Too complecated and verbose 

    Stop execution when assertion failed - run -> fail -> fix -> run -> fail -> fix -> run -> fail ...
  11. Spock “The Enterprise-ready testing and specification framework.” https://github.com/spockframework/spock https://www.flickr.com/photos/124561666@N02/14218023847

  12. Spock  Testing framework written in Groovy  Inspired from

    JUnit, RSpec, Mockito, ...  Very fluent specification DSL (like RSpec?)  Descriptive assertion message (Power Assert!!!)
  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' } }
  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' } }
  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... 
  16. Installation dependencies { // ... testCompile "org.spockframework:spock-core:1.0-groovy-2.4" }

  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" } }
  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" } }
  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" } }
  20. Assertion message

  21. Assertion message

  22. JUnit v.s. Spock

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

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

    #22