Testing and Building Android

download Testing and Building Android

of 102

  • date post

    23-Aug-2014
  • Category

    Mobile

  • view

    98
  • download

    0

Embed Size (px)

description

 

Transcript of Testing and Building Android

  • Agenda: -Intro -Android Testing -Creating a project -JUnit Testing -Mock Objects + UI Testing -Integration and acceptance test -Performance and system testing -Advance assertions -TDD -Real App Dev !
  • Agenda: -Tools -Continuous Integration !
  • Ego slide Mobile Developer @ Sixt M. Sc. UCM/RWTH CS Teacher at Alcal University ! ! ! +EnriqueLpezMaas @eenriquelopez
  • Ego slide Mobile Developer @ Sixt M.Sc. Politehnica Timisoara ! ! ! +ami.antoch
  • Keywords Continuous Integration TDD JUnit / Unit Testing Instrumentation Behavior Driven Development Performance Testing Proling
  • What you need Java SE Version 1.6 Android SDK Tools Eclipse IDE for Java Developers ADT (Android Bundle)
  • Testing Develop Finding Correcting
  • Is about productivity! $59.5 billion annually ! 1/3 could be saved ! (National Institute of Standards and Technology (USA))
  • Why, what, how, when During, before ! Continuous Integration ! Classes, methods, lines, basic blocks..
  • Activity lifecycle
  • Activity lifecycle Databases and lesystem operations
  • Activity lifecycle Databases and lesystem operations Physical characteristics of the device
  • Activity lifecycle Databases and lesystem operations Physical characteristics of the device (AVD and Genymotion)
  • UnitTests Written by programmers for programmers JUnit
  • Elements of a test The xture Method setUp() Method tearDown() Test preconditions (retrospection vs. annotations)
  • Assertions assertEquals() assertFalse() assertNotNull() assertNotSame() assertNull() assertSame() assertTrue() fail()
  • Mock objects Mimic objects MockApplication MockContentProvider MockContentResolver MockContext MockCursor MockDialogInterface MockPackageManager MockResources
  • UI Tests Annotation @UIThreadTest Class TouchUtils Click Drag LongClick Scroll Tap Touch
  • Environments Eclipse but also IntelliJ! and Netbeans!
  • Instrumentation Foundation Control application under test and permit injection
  • Project Test MyProject MyProjectTest
  • Testing Target Device AVD Machines GenyMotion
  • Summary
  • Android Testing Best practice: test should live in a separate correlated project Stripped (not included in main) Easier to run in Emulator Takes less time Encourages code reusability
  • Create project
  • AVD
  • Genymotion
  • DDMS
  • TestAnnotations @SmallTest @MediumTest @LargeTest @Smoke @FlakyTest (tolerance=4) @UIThreadTest @Suppress
  • Running Test From Eclipse
  • Running Single Test
  • Running from the emulator
  • Running from the command line
  • Running from the command line All From an specic test case Specic test by name By category (create custom annotations)
  • Debug bugs
  • Summary Created rst Android project Followed best practice creating companion project Simple test class Eclipse Command line options
  • Mockup All Mock implementations All methods non functional Throw UnsupportedOperationExceiption
  • UI Testing Android SDK Tools, 21+ API 16+ uiaumatorviewer uiautomator Tools
  • Integration tests Individual components work jointly
  • Integration tests Individual components work jointly Acceptance tests QA, Business language
  • Performance tests Performance behavior System test GUI Test Smoke Tests Performance Installation
  • Benchmarking Traditional logging statement methods Creating Android performance tests Using proling tools Microbenchmarks using Caliper
  • Benchmarking /* (non-Javadoc) * @see android.text.TextWatcher#onTextChanged( * java.lang.CharSequence, int, int, int) */ public void onTextChanged(CharSequence s, int start, int before, int count) { if (!mDest.hasWindowFocus() || mDest.hasFocus() || s == null ) { return; } nal String str = s.toString(); if ( "".equals(str) ) { mDest.setText(""); return; } nal long t0; if ( BENCHMARK_TEMPERATURE_CONVERSION ) { t0 = System.currentTimeMillis(); } !
  • Benchmarking try { nal double temp = Double.parseDouble(str); nal double result = (mOp == OP.C2F) ? TemperatureConverter.celsiusToFarenheit(temp); TemperatureConverter.fahrenheitToCelsius(temp); nal String resultString = String.format("%.2f", result); mDest.setNumber(result); mDest.setSelection(resultString.length()); } catch (NumberFormatException e) { // WARNING // this is generated while a number is entered, // for example just a '-' // so we don't want to show the error } catch (Exception e) { mSource.setError("ERROR: " + e.getLocalizedMessage()); } if ( BENCHMARK_TEMPERATURE_CONVERSION ) { long t = System.currentTimeMillis() - t0; Log.i(TAG, "TemperatureConversion took " + t + " ms to complete."); } }
  • Android performance tests Classes hidden only for System apps :(
  • Assertions in depth assertEquals assertFalse assertNotNull assertNotSame assertNull assertSame assertTrue assertFail
  • Assertions in depth public void testNotImplementedYet() { fail("Not implemented yet"); }
  • Assertions in depth public void testShouldThrowException() { try { MyFirstProjectActivity.methodThatShouldThrowException(); fail("Exception was not thrown"); } catch ( Exception ex ) { // do nothing } }
  • Custom messages public void testMax() { nal int a = 1; nal int b = 2; nal int expected = b; nal int actual = Math.max(a, b); assertEquals("Expection " + expected + " but was " + actual, expected, actual); }
  • Static imports public void testAlignment() { nal int margin = 0; ... android.test.ViewAsserts.assertRightAligned( mMessage, mCapitalize, margin); ! }
  • Static imports import static android.test.ViewAsserts.assertRightAligned; public void testAlignment() { nal int margin = 0; assertRightAligned(mMessage, mCapitalize, margin); }
  • View assertions assertBaselineAligned
  • View assertions assertBaselineAligned assertBotomAligned
  • View assertions assertBaselineAligned assertBotomAligned assertGroupContains
  • View assertions assertBaselineAligned assertBotomAligned assertGroupContains assertGroupIntegrity
  • View assertions assertBaselineAligned assertBotomAligned assertGroupContains assertGroupIntegrity assertGroupNotContains
  • View assertions assertBaselineAligned assertBotomAligned assertGroupContains assertGroupIntegrity assertGroupNotContains assertHasScreenCoordinates