Android Testing

20
Others Talk, We Listen. Android Testing Antoine Campbell 1

Transcript of Android Testing

Page 1: Android Testing

Others Talk, We Listen.

Android TestingAntoine Campbell

1

Page 2: Android Testing

Overview

• Background• Testing Approaches and Demos

• Android Unit ( JUnit 3 )• JUnit 4• Robolectric ( JUnit 4 )• Instrumentation ( JUnit 3/ JUnit 4new )

• Espresso• Isolated Fragments ( why not, it works )

• UIAutomator ( JUnit 3/ JUnit 4new )• Pure (End-to-End capable)• Instrumentation

• Questions• Resources

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

2

Page 3: Android Testing

Background

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

3

• UNC-Charlotte• B.A./M.S. Computer Science

• Android Developer• 2 client apps in prod, 4 supported

• iOS Developer• 1 enterprise app in prod

• Web Services (SOAP, REST)• 2 client apps in prod

• Game Developer (former)• CT native / NC native• CapTech Consulting – 3rd year

Page 4: Android Testing

Android Unit (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

4

• Must extend TestCase, AndroidTestCase• Test methods must begin with ‘test’ e.g. ‘testMethod’• Runs on device• Full Android API available

Page 5: Android Testing

Android Unit (JUnit 3) - DEMO

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

5

DEMO

Page 6: Android Testing

JUnit 4

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

6

• Test methods must be annotated with @Test• Runs in JVM on host machine• Android API stub available

• All methods throw exceptions

Page 7: Android Testing

JUnit 4

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

7

DEMO

Page 8: Android Testing

Robolectric (JUnit 4)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

8

• Test methods must be annotated with @Test• Runs in JVM on host machine• Android API stub available (Robolectric implements some Android

APIs)

Page 9: Android Testing

Robolectric (JUnit 4) – Android Studio Bug

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

9

• Newly created test have incorrect working directory • Should be set to $MODULE_DIR$

Page 10: Android Testing

Robolectric (JUnit 4)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

10

DEMO

Page 11: Android Testing

Instrumentation - Espresso (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

11• Must extend InstrumentationTestCase,

ActivityInstrumentationTestCase2<>• Test methods must begin with ‘test’ e.g. ‘testMethod’• Runs on device• Full Android API available

Page 12: Android Testing

Instrumentation - Espresso (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

12

DEMO

Page 13: Android Testing

Instrumentation – Isolated Fragments (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

13• Restrictions same as Instrumentation• Attach fragment to anonymous activity

Page 14: Android Testing

Instrumentation – Isolated Fragments (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

14

DEMO

Page 15: Android Testing

UIAutomator – Pure (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

15• Restrictions same as Instrumentation (InstrumentationTestCase)• Full access to entire device (muahahahahah)• Test can become tedious and flaky, yet powerful

Page 16: Android Testing

UIAutomator – Pure (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

16

DEMO

Page 17: Android Testing

UIAutomator – Instrumentation (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

17

• Restrictions same as Instrumentation (ActivityInstrumentationTestCase2)

• Full access to entire device (muahahahahah)• Even contexts your app does not own

• Activity started and stopped for you between tests

Page 18: Android Testing

UIAutomator – Instrumentation (JUnit 3)

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

18

DEMO

Page 19: Android Testing

Questions

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

19

Page 20: Android Testing

Resources

Copyright © 2015 CapTech Ventures, Inc. All rights reserved.

20

Demo App: https://github.com/antoinecampbell/GithubUserBroswerTesting Docs: http://developer.android.com/tools/testing/testing_android.htmlRobolectric: http://robolectric.org/Espresso: https://code.google.com/p/android-test-kit/Robotium: https://code.google.com/p/robotium/

Email / G+: [email protected] blog: http://antoine-campbell.com