Android Test Automation Workshop

Preview:

Citation preview

Íntel Software and Services Group

Android: test automation workshopEduardo CarraraDeveloper Evangelist – Intel Developers Relations Division

Intel Software and Services Group

#AndroidOnIntel

2

+EduardoCarraraDeAraujo

https://www.facebook.com/ducarrara

@DuCarrara

br.linkedin.com/in/eduardocarrara/

ecarrara-araujo

Intel Software and Services Group 3

ObjectivePractice the usage of test automation tools available in the Android Framework through the following activities:

• Prepare the test environment for development and execution.

• Development of unit tests that run on the local JVM.

• Development of UI Tests with Espresso.

• Development of Cross Apps Tests with UI Automator.

• Running tests in the cloud with TestDroid.

Intel Software and Services Group 4

Requirements

• Android Studio 1.3+

• Test Device with Android 4.0+

• SDK Version 23

• Build Tools 23.0.0

• AppCompat-v7 23.3.0

Intel Software and Services Group 5

Base App - BeerWith• Simple demo app to help you keep track of

what have you been drinking and with who.

• https://github.com/ecarrara-araujo/beer-with

• If you want to follow the workshop just clone it and work on branch master.

• Check the branch ahead for the implementations:

• Imasters-android-devconf-tests-workshop

Intel Software and Services Group

#1 – Creating your first Unit Test

6

Intel Software and Services Group 7

#1 – Creating your first Unit Test

testCompile 'junit:junit:4.12'

• Add the Junit dependency to the app/build.gradle

Intel Software and Services Group 8

#1 – Creating your first Unit Test

Create these directories.

Intel Software and Services Group 9

#1 – Creating your first Unit Test

Intel Software and Services Group 10

#1 – Creating your first Unit Test

Intel Software and Services Group 11

#1 – Creating your first Unit Test

Intel Software and Services Group 12

#1 – Creating your first Unit Test

public class UtilityTest {

@Test

public void testGetDateFormattedTime() throws Exception {

String expectedDateFormat = "yyyy, MMMM dd";

Calendar calendar = Calendar.getInstance();

calendar.set(2015, 4 - 1, 3); //20150403

String expectedResult = (new SimpleDateFormat(expectedDateFormat)).format(calendar.getTime());

String result = Utility.getDateFormattedTime(calendar, expectedDateFormat);

assertEquals("Date was not correctly formatted.", expectedResult, result);

// testing a second format to be sure

expectedDateFormat = "dd MM yyyy";

expectedResult = (new SimpleDateFormat(expectedDateFormat)).format(calendar.getTime());

result = Utility.getDateFormattedTime(calendar, expectedDateFormat);

assertEquals("Date was not correctly formatted.", expectedResult, result);

}

}

Intel Software and Services Group 13

#1 – Creating your first Unit Test

$ ./gradlew testor

Intel Software and Services Group 14

#1 – Creating your first Unit Test

or

\app\build\reports\tests\debug\index.html

Intel Software and Services Group

#2 Testing the ui with espresso

15

Intel Software and Services Group 16

#2 Testing the ui with espresso

• App/build.gradle

defaultConfig {

testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

}

dependencies {

androidTestCompile 'com.android.support.test:runner:0.3'

androidTestCompile 'com.android.support.test:rules:0.3'

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'

}

• ./build.gradle

allprojects {

configurations.all {

resolutionStrategy.force 'com.android.support:support-annotations:23.0.0' } }

Intel Software and Services Group 17

#2 Testing the ui with espresso

Intel Software and Services Group 18

#2 Testing the ui with espresso

Intel Software and Services Group 19

#2 Testing the ui with espresso

Here comes a whole lot of code…Check the file: BeerWith\app\src\androidTest\java\br\eng\ecarrara\beerwith\BeerWithMainActivityInstrumentationTest.java

Intel Software and Services Group 20

#2 Testing the ui with espresso

or $ ./gradlew connectedCheck

Intel Software and Services Group 21

#2 Testing the ui with espresso

or

app\build\reports\androidTests\connected\index.html

Intel Software and Services Group

#3 CROss app Testing with UI Automator

22

Intel Software and Services Group 23

#3 Cross app Testing with UI Automator

• App/build.gradle

defaultConfig {

testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

}

• ./build.gradle

allprojects {

configurations.all {

resolutionStrategy.force 'com.android.support:support-annotations:23.0.0' } }

dependecies {

androidTestCompile 'com.android.support.test:runner:0.3'

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

androidTestCompile 'org.hamcrest:hamcrest-integration:1.3'

}

Intel Software and Services Group 24

#3 Cross app Testing with UI Automator

Intel Software and Services Group 25

#3 Cross app Testing with UI Automator

Intel Software and Services Group 26

#3 Cross app Testing with UI Automator

Androidsdk/tools/uiautomatorviewer

Intel Software and Services Group 27

#3 Cross app Testing with UI Automator

Here comes a whole lot of code…Check the file: BeerWith\app\src\androidTest\java\br\eng\ecarrara\beerwith\AddingBeerWithCompleteTestFlow.java

Intel Software and Services Group 28

#3 Cross app Testing with UI Automator

or $ ./gradlew connectedCheck

Intel Software and Services Group 29

or

Intel Software and Services Group 30

Intel Software and Services Group 31

Intel Software and Services Group 32

Intel Software and Services Group 33

Intel Software and Services Group 34

Intel Information Technology

What is next?

35

• Follow the guide and automate your tests!

• How to integrate continous integration and delivery with Android?

• Code Coverage

• Mocking

Intel Software and Services Group 36

Intel Developer Zone

https://software.intel.com/en-us/android/app-testing

Intel Software and Services Group

Thanks!

37

+EduardoCarraraDeAraujo

https://www.facebook.com/ducarrara

@DuCarrara

br.linkedin.com/in/eduardocarrara/

ecarrara-araujo/vilibra

Intel Software and Services Group 38

References

• Android Testing: https://developer.android.com/tools/testing/testing_android.html

• Android Unit Testing Support: http://tools.android.com/tech-docs/unit-testing-support

• UI Testing: https://developer.android.com/training/testing/ui-testing/index.html

• Android Testing Support Library: https://developer.android.com/tools/testing-support-library

• Android Instrumentation: http://developer.android.com/tools/testing/testing_android.html#Instrumentation

• Junit: http://junit.org

• Testdroid: http://testdroid.com

• Intel App Testing Page: https://software.intel.com/en-us/android/app-testing

Intel Software and Services Group 39

References

• Codelab Android Studio Testing: https://io2015codelabs.appspot.com/codelabs/android-studio-testing#1

• Android Testing Blueprints: https://github.com/googlesamples/android-testing-templates/tree/master/AndroidTestingBlueprint

• Testing on Android by Vincent Brison: http://vincentbrison.com/2015/08/05/testing-on-android-part1-a-practical-approach/

• Android Developers Unit Testing Training: https://developer.android.com/training/testing/unit-testing/index.html

• Android Testing Samples: https://github.com/googlesamples/android-testing/

• G+ Community: https://plus.google.com/communities/10515313437206298596

• Stack Overflow: http://stackoverflow.com/questions/tagged/android-testing

Intel Software and Services Group 40

References

• Espresso v2 Cheat Sheet - https://code.google.com/p/android-test-kit/wiki/EspressoV2CheatSheet

Placeholder Footer Copy / BU Logo or Name Goes Here