introduction to android testing - oscon 2012

Download Introduction to android testing - oscon 2012

If you can't read please download the document

Post on 09-May-2015

2.524 views

Category:

Technology

10 download

Embed Size (px)

DESCRIPTION

It doesn’t matter how much time you invest in Android design, or even how careful you are when programming, mistakes are inevitable and bugs will appear.This session will help you minimize the impact of these errors in your Android project and increase your development productivity. We will introduce the most commonly-available techniques, frameworks, and tools to improve the development of your Android applications.Clear, step-by-step instructions show how to write tests for your applications and assure quality control using various methodologies.

TRANSCRIPT

  • 1.introduction to androidtesting a hands-on approach OSCON 2012 Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 1

2. diego torres milanoandroid system engineer extronicshttp://dtmilano.blogspot.comCopyright (C) 2011-2012 Diego Torres Milano All rights reserved2 3. Never test the depth ofthe water with both feet. -- Anonymous Copyright (C) 2011-2012 Diego Torres Milano All rights reserved3 4. agendaandroid testing backgroundtest driven developmentcode coveragecontinuous integrationbehavior driven developmentCopyright (C) 2011-2012 Diego Torres Milano All rights reserved4 5. operating systems May 2012OthersiOS 250MAndroid 400MCopyright (C) 2011-2012 Diego Torres Milano All rights reserved5 6. android testingbackgroundCopyright (C) 2011-2012 Diego Torres Milano All rights reserved6 7. WhatWhyWhenhow ? Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 7 8. types of testtestsunitfunctionalperformance integration Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 8 9. types of test by programmers for programmersunit in a programming language JUnit is the de-facto standard test objects in isolation in a repeatable way usually rely on mock objects Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 9 10. types of testtestsunitfunctionalperformance integration Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 10 11. types of testby business & QA people functionalin a business domain languagetest completeness & correctnessBDD has gained some popularityFitNesse can helpCopyright (C) 2011-2012 Diego Torres Milano All rights reserved11 12. types of testtestsunitfunctionalperformance integration Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 12 13. types of testhow components work together integrationmodules have been unit testedandroid components need integrationwith the systemtesting framework facilitatesintegrationCopyright (C) 2011-2012 Diego Torres Milano All rights reserved13 14. types of testtestsunitfunctionalperformance integration Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 14 15. types of test measure performance in aperformancerepeatable way if cannot be measure cannot be improved premature optimization does more harm than good measure-change-measure Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 15 16. types of testtestsunitfunctionalperformance integration Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 16 17. class diagram Assert TestCaseTestInstrumentationTestCase AndroidTestCaseActivityInstrumentationTestCase2ActivityUnitTestCase Copyright (C) 2011-2012 Diego Torres Milano All rights reserved17 18. InstrumentationTestCase instrumentation instantiated before application allows for monitoring interaction send keys and input events manual lifecycle direct or indirect base class of other tests Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 18 19. class diagram Assert TestCaseTestInstrumentationTestCase AndroidTestCaseActivityInstrumentationTestCase2ActivityUnitTestCase Copyright (C) 2011-2012 Diego Torres Milano All rights reserved19 20. ActivityUnitTestCaseisolated testing of singleActivityminimal connection tothe systemuses mocks fordependenciessome Activity methodsshould not be calledCopyright (C) 2011-2012 Diego Torres Milano All rights reserved20 21. class diagram Assert TestCaseTestInstrumentationTestCase AndroidTestCaseActivityInstrumentationTestCase2ActivityUnitTestCase Copyright (C) 2011-2012 Diego Torres Milano All rights reserved21 22. ActivityInstrumentationTestCase2functionaltesting of a single Activityhas access to Instrumentationcreates the AUT using systeminfrastructurecustom intent can be provided Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 22 23. class diagram Assert TestCaseTestInstrumentationTestCase AndroidTestCaseActivityInstrumentationTestCase2ActivityUnitTestCase Copyright (C) 2011-2012 Diego Torres Milano All rights reserved23 24. access to ContextAndroidTestCaseaccess to Resourcesbase class for Application, Providerand Service test casesContext stored in mContext eldcan start more than one Activity Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 24 25. class diagram Assert TestCaseTestInstrumentationTestCase AndroidTestCaseActivityInstrumentationTestCase2ActivityUnitTestCase Copyright (C) 2011-2012 Diego Torres Milano All rights reserved25 26. test driven development Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 26 27. test driven development advantages:the tests are writtenone way or anotherdevelopers take moreresponsibility for thequality of their work strategy of writing tests along development test cases written prior to the code single test added, then the code to satisfy it Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 27 28. activity diagramdesign decisions aretaken in single stepsand finally the codewrite testsatisfying the tests isimproved byrefactoring it run[fails] code[passes] refactorCopyright (C) 2011-2012 Diego Torres Milano All rights reserved28 29. temperature converterTitle celsius100autoupdate32fahrenheit keyboard Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 29 30. requirementsconverts between temperatureunitsone temperature is entered andthe other is updatederror is displayed in the fieldright aligned, 2 decimal digitsentry fields start emptyCopyright (C) 2011-2012 Diego Torres Milano All rights reserved30 31. understandingrequirementsto write a test you must understand therequirementdestination is quickly identiedif requirement change, changing thecorresponding test helps verify itCopyright (C) 2011-2012 Diego Torres Milano All rights reserved31 32. github$ mkdir myworkdir$ cd myworkdir$ git clone git://github.com/dtmilano/I2AT-OSCON-2012.gitCopyright (C) 2011-2012 Diego Torres Milano All rights reserved32 33. creating the main projectTemperatureConverter uses conventional settings Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 33 34. select build targetTemperatureConverter usesAndroid 4.0.3 Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 34 35. creating the test projectAutomatically selected valuesfor TemperatureConverterTest project Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 35 36. running the tests[13:21:28 - TemperatureConverterTest]Launching instrumentationandroid.test.InstrumentationTestRunner ondevice XXX[13:21:28 - TemperatureConverterlTest-local]Failed to launch testCopyright (C) 2011-2012 Diego Torres Milano All rights reserved36 37. warning due to the parameterized base classcreating the test case Use:ActivityInstrumentationTestCase2as the base classTemperatureConverterActivity as the class under testCopyright (C) 2011-2012 Diego Torres Milano All rights reserved37 38. creating teststubsSelect: onCreate(Bundle) to create a method stubCopyright (C) 2011-2012 Diego Torres Milano All rights reserved38 39. x constructor/*** Constructor* @param name*/public TemperatureConverterActivityTests(String name) { super(TemperatureConverterActivity.class); setName(name);} Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 39 40. running the testsjunit.framework.AssertionFailedError: Not yet implemented atcom.dtmilano.i2at.tc.test. TemperatureConverterActivityTests.testOnCreateBundle( TemperatureConverterActivityTests.java:50)Copyright (C) 2011-2012 Diego Torres Milano All rights reserved40 41. xtureprotected void setUp(String name) throws Exception { super.setUp();mActivity = getActivity(); references theassertNotNull(mActivity); main packagemCelsius = (EditText)mActivity.ndViewById( com.dtmilano.i2at.tc.R.id.celsius);assertNotNull(mCelsius);mFahrenheit = (EditText)mActivity.ndViewById(com...);assertNotNull(mFahrenheit);}Copyright (C) 2011-2012 Diego Torres Milano All rights reserved41 42. layoutsatisfy the test needsassign celsius and fahrenheit idsCopyright (C) 2011-2012 Diego Torres Milano All rights reserved42 43. x the test! /**! * Test method for {@link TemperatureConverterActivity #onCreate(android.os.Bundle)}.! */! public void testOnCreateBundle() {! ! assertNotNull(mActivity);! } Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 43 44. ui tests: visibility@SmallTestpublic void testFieldsOnScreen() { nal View origin = mActivity.getWindow().getDecorView(); ViewAsserts.assertOnScreen(origin, mCelsius); ViewAsserts.assertOnScreen(origin, mFahrenheit);} Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 44 45. annotations @SmallTest @MediumTest @LargeTest @UiThreadTest @Suppress @FlakyTestCopyright (C) 2011-2012 Diego Torres Milano All rights reserved45 46. ui tests: alignment@SmallTestpublic void testAlignment() {! ! ViewAsserts.assertRightAligned(mCelsius,mFahrenheit);! ! ViewAsserts.assertLeftAligned(mCelsius,mFahrenheit);}Copyright (C) 2011-2012 Diego Torres Milano All rights reserved46 47. ui tests: initialization@SmallTestpublic void testFieldsShouldStartEmpty() {! ! assertTrue("".equals(mCelsius.getText().toString()));! ! assertTrue("".equals(mFahrenheit.getText().toString()));} Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 47 48. ui tests: justication@SmallTestpublic void testJustication() {! ! nal int expected = Gravity.RIGHT|Gravity.CENTER_VERTICAL;! ! assertEquals(expected,mCelsius.getGravity());! ! assertEquals(expected,mFahrenheit.getGravity());} Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 48 49. running the tests video plays on click >>>Copyright (C) 2011-2012 Diego Torres Milano All rights reserved49 50. gravity add right andcenter_vertical gravity for celsius and fahrenheit Copyright (C) 2011-2012 Diego Torres Milano All rights reserved 50 51. running the tests video plays on click >>>Copyright (C) 2011-2012 Diego Torres Milano All rights reserved51 52. temperature conversion run on the! @UiThreadTest main thread! public void te