Testing and Building Android

561 views
430 views

Published on

Published in: Mobile, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
561
On SlideShare
0
From Embeds
0
Number of Embeds
58
Actions
Shares
0
Downloads
6
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Testing and Building Android

  1. 1. 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 !
  2. 2. Agenda: -Tools -Continuous Integration !
  3. 3. Ego slide Mobile Developer @ Sixt M. Sc. UCM/RWTH CS Teacher at Alcalá University ! ! ! +EnriqueLópezMañas @eenriquelopez
  4. 4. Ego slide Mobile Developer @ Sixt M.Sc. Politehnica Timisoara ! ! ! +ami.antoch
  5. 5. Keywords Continuous Integration TDD JUnit / Unit Testing Instrumentation Behavior Driven Development Performance Testing Profiling
  6. 6. What you need • Java SE Version 1.6 • Android SDK Tools • Eclipse IDE for Java Developers • ADT • (Android Bundle)
  7. 7. Testing • Develop • Finding • Correcting
  8. 8. Is about productivity! $59.5 billion annually ! 1/3 could be saved ! (National Institute of Standards and Technology (USA))
  9. 9. Why, what, how, when During, before ! Continuous Integration ! Classes, methods, lines, basic blocks..
  10. 10. Activity lifecycle
  11. 11. Activity lifecycle Databases and filesystem operations
  12. 12. Activity lifecycle Databases and filesystem operations Physical characteristics of the device
  13. 13. Activity lifecycle Databases and filesystem operations Physical characteristics of the device (AVD and Genymotion)
  14. 14. UnitTests Written by programmers for programmers JUnit
  15. 15. Elements of a test The fixture Method setUp() Method tearDown() Test preconditions (retrospection vs. annotations)
  16. 16. Assertions • assertEquals() • assertFalse() • assertNotNull() • assertNotSame() • assertNull() • assertSame() • assertTrue() • fail()
  17. 17. Mock objects Mimic objects • MockApplication • MockContentProvider • MockContentResolver • MockContext • MockCursor • MockDialogInterface • MockPackageManager • MockResources
  18. 18. UI Tests Annotation @UIThreadTest Class TouchUtils • Click • Drag • LongClick • Scroll • Tap • Touch
  19. 19. Environments Eclipse… …but also IntelliJ! …and Netbeans!
  20. 20. Instrumentation Foundation Control application under test and permit injection
  21. 21. Project Test MyProject MyProjectTest
  22. 22. Testing Target Device AVD Machines GenyMotion
  23. 23. Summary
  24. 24. 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
  25. 25. Create project
  26. 26. AVD
  27. 27. Genymotion
  28. 28. DDMS
  29. 29. TestAnnotations @SmallTest @MediumTest @LargeTest @Smoke @FlakyTest (tolerance=4) @UIThreadTest @Suppress
  30. 30. Running Test From Eclipse
  31. 31. Running Single Test
  32. 32. Running from the emulator
  33. 33. Running from the command line
  34. 34. Running from the command line • All • From an specific test case • Specific test by name • By category • (create custom annotations)
  35. 35. Debug bugs
  36. 36. Summary • Created first Android project • Followed best practice creating companion project • Simple test class • Eclipse • Command line options
  37. 37. Mockup • All Mock implementations • All methods non functional • Throw UnsupportedOperationExceiption
  38. 38. UI Testing • Android SDK Tools, 21+ • API 16+ • uiaumatorviewer • uiautomator Tools
  39. 39. Integration tests Individual components work jointly
  40. 40. Integration tests Individual components work jointly Acceptance tests QA, Business language
  41. 41. Performance tests Performance behavior System test • GUI Test • Smoke Tests • Performance • Installation
  42. 42. Benchmarking • Traditional logging statement methods • Creating Android performance tests • Using profiling tools • Microbenchmarks using Caliper
  43. 43. 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; } final String str = s.toString(); if ( "".equals(str) ) { mDest.setText(""); return; } final long t0; if ( BENCHMARK_TEMPERATURE_CONVERSION ) { t0 = System.currentTimeMillis(); } !
  44. 44. Benchmarking try { final double temp = Double.parseDouble(str); final double result = (mOp == OP.C2F) ? TemperatureConverter.celsiusToFarenheit(temp); TemperatureConverter.fahrenheitToCelsius(temp); final 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."); } }
  45. 45. Android performance tests Classes hidden only for System apps :(
  46. 46. Assertions in depth • assertEquals • assertFalse • assertNotNull • assertNotSame • assertNull • assertSame • assertTrue • assertFail
  47. 47. Assertions in depth public void testNotImplementedYet() { fail("Not implemented yet"); }
  48. 48. Assertions in depth public void testShouldThrowException() { try { MyFirstProjectActivity.methodThatShouldThrowException(); fail("Exception was not thrown"); } catch ( Exception ex ) { // do nothing } }
  49. 49. Custom messages public void testMax() {
 final int a = 1;
 final int b = 2;
 final int expected = b;
 final int actual = Math.max(a, b);
 assertEquals("Expection " + expected + " but was " + actual, expected, actual); }
  50. 50. Static imports public void testAlignment() { final int margin = 0; ... android.test.ViewAsserts.assertRightAligned( mMessage, mCapitalize, margin); ! }
  51. 51. Static imports import static android.test.ViewAsserts.assertRightAligned; public void testAlignment() {
 final int margin = 0;
 assertRightAligned(mMessage, mCapitalize, margin); }
  52. 52. View assertions assertBaselineAligned
  53. 53. View assertions assertBaselineAligned
 assertBotomAligned
  54. 54. View assertions assertBaselineAligned
 assertBotomAligned assertGroupContains
  55. 55. View assertions assertBaselineAligned
 assertBotomAligned assertGroupContains assertGroupIntegrity
  56. 56. View assertions assertBaselineAligned
 assertBotomAligned assertGroupContains assertGroupIntegrity assertGroupNotContains
  57. 57. View assertions assertBaselineAligned
 assertBotomAligned assertGroupContains assertGroupIntegrity assertGroupNotContains assertHasScreenCoordinates
  58. 58. View assertions assertBaselineAligned
 assertBotomAligned assertGroupContains assertGroupIntegrity assertGroupNotContains assertHasScreenCoordinates assertHorizontalCenterAligned
  59. 59. View assertions assertLeftAligned
  60. 60. View assertions assertLeftAligned assertOffScreenAbove
  61. 61. View assertions assertLeftAligned assertOffScreenAbove assertOffScreenBelow
  62. 62. View assertions assertLeftAligned assertOffScreenAbove assertOffScreenBelow assertOnScreen
  63. 63. View assertions assertLeftAligned assertOffScreenAbove assertOffScreenBelow assertOnScreen assertRightAligned
  64. 64. View assertions assertLeftAligned assertOffScreenAbove assertOffScreenBelow assertOnScreen assertRightAligned assertTopAligned
  65. 65. View assertions assertLeftAligned assertOffScreenAbove assertOffScreenBelow assertOnScreen assertRightAligned assertTopAligned assertVerticalCenterAligned
  66. 66. Example public void testUserInterfaceLayout() {
 final int margin = 0;
 final View origin = mActivity.getWindow().getDecorView(); assertOnScreen(origin, mMessage); assertOnScreen(origin, mCapitalize); assertRightAligned(mMessage, mCapitalize, margin); }
  67. 67. Even more 
 assertions assertAssignableFrom
  68. 68. Even more 
 assertions assertAssignableFrom assertContainsRegex
  69. 69. Even more 
 assertions assertAssignableFrom assertContainsRegex assertContainsInAnyOrder
  70. 70. Even more 
 assertions assertAssignableFrom assertContainsRegex assertContainsInAnyOrder assertContainsInOrder
  71. 71. Even more 
 assertions assertAssignableFrom assertContainsRegex assertContainsInAnyOrder assertContainsInOrder assertEmpty
  72. 72. Even more 
 assertions assertAssignableFrom assertContainsRegex assertContainsInAnyOrder assertContainsInOrder assertEmpty assertEquals
  73. 73. Even more 
 assertions assertMatchesRegex
  74. 74. Even more 
 assertions assertMatchesRegex assertNotContainsRegex
  75. 75. Even more 
 assertions assertMatchesRegex assertNotContainsRegex assertNotEmpty
  76. 76. Even more 
 assertions assertMatchesRegex assertNotContainsRegex assertNotEmpty assertNotMatchesRegex
  77. 77. Even more 
 assertions assertMatchesRegex assertNotContainsRegex assertNotEmpty assertNotMatchesRegex checkEqualsAndHashCodeMethods
  78. 78. Example @UiThreadTest public void testNoErrorInCapitalization() { final String msg = "this is a sample"; mMessage.setText(msg); mCapitalize.performClick(); final String actual = mMessage.getText().toString(); final String notExpectedRegexp = “(?i:ERROR)"; assertNotContainsRegex("Capitalization found error:", notExpectedRegexp, actual); }
  79. 79. assertActivityRequiresPermission public void testActivityPermission() { final String PKG = "com.example.aatg.myfirstproject"; final String ACTIVITY = PKG + ".MyFirstProjectActivity"; final String PERMISSION = android.Manifest.permission.WRITE_EXTERNAL_STORAGE; assertActivityRequiresPermission(PKG, ACTIVITY, PERMISSION); }
  80. 80. assertReadingContentUriRequiresPermission public void testReadingContacts() { final Uri URI = ContactsContract.AUTHORITY_URI; final String PERMISSION = android.Manifest.permission.READ_CONTACTS; assertReadingContentUriRequiresPermission(URI, PERMISSION); }
  81. 81. assertWritingContentUriRequiresPermission public void testWritingContacts() { final Uri URI = ContactsContract.AUTHORITY_URI; final String PERMISSION = android.Manifest.permission.WRITE_CONTACTS; assertWritingContentUriRequiresPermission(URI, PERMISSION); }
  82. 82. ActivityMonitor public void testFollowLink() { final Instrumentation inst = getInstrumentation(); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW); intentFilter.addDataScheme("http"); intentFilter.addCategory(Intent.CATEGORY_BROWSABLE); ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false); assertEquals(0, monitor.getHits()); TouchUtils.clickView(this, mLink); monitor.waitForActivityWithTimeout(5000); assertEquals(1, monitor.getHits()); inst.removeMonitor(monitor); }
  83. 83. Example project
  84. 84. Summary Used several types of assertion Explained mock objects Exemplified different tests
  85. 85. Test Driven Development
  86. 86. Test Driven Development
  87. 87. Test Driven Development • Difficult to divert code • Focus NO SILVER BULLET
  88. 88. Example project - temperature converter
  89. 89. Requirements: • Application convert from Celsius to F. • … and viceversa • Two fields to input data • When temperature is entered, other field updates • Error displayed • Some space reserved keyboard • Entry fields start empty • Digits right aligned • Last entered values retained after onPause()
  90. 90. Concept design
  91. 91. Create projects
  92. 92. From requirement to tests
  93. 93. AVD and Emulator Options
  94. 94. Headless emulator emulator -avd test -no-window -no-audio -no-boot-anim -port 5580 &
  95. 95. Monkeyrunner
  96. 96. Building with ant android update project —path .
  97. 97. Jenkins
  98. 98. Testing recipes
  99. 99. Thank you ! + Enrique López Mañas @eenriquelopez

×