Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Use Testing to Rock Your Android Development

Elvis Lin
October 26, 2014

Use Testing to Rock Your Android Development

簡介測試的基本概念,與如何透過 unit test, UI test 與 user acceptance test 來強化 Android app 的穩定性

Elvis Lin

October 26, 2014
Tweet

More Decks by Elvis Lin

Other Decks in Programming

Transcript

  1. Agenda • 㻌ز介手 • 疩獈介手ଉ憎ጱࢯ櫞 • Android Ӥጱ㻌ز介手 • 㻌ز介手ጱૡٍ

    • অᓕ疑ҁJenkins҂虏Ӟ獥ᛔ㵕玕 • አ讙絤ҁCucumber҂޾մ㴟牏PM传蝢
  2. 㻌ز介手 • int add(int x, int y) {return x +

    y} • @Test
 void testAdd() {
 int result = add(5, 4);
 assertEquals(9, result);
 }
  3. Why not very popular? • 翥䌃㻌ز介手ॡ窚揲碻樌 • 纷ୗ嘨૪妿ݢ犥᪒ԧ牧䓄琳螭ᥝ䌃㻌 ز介手 •

    ֕ইຎ磪碝౮㹓ے獈牧ݢ胼ࣁ䷱磪䁆 ᤈ涢挨介手纷ୗጱ眐丆ӥ݄ๅ硬纷ୗ 嘨牧蝨౮介手纷ୗ篷ဩ䁆ᤈ
  4. • 螨ع介手አጱ纷ୗڊ匍ࣁ叨ߝӾ public class Testee {
 public void doSomthing() {


    if (TESTING) {
 // do something for testing
 } else {
 // do something for the real user
 }
 }
 }
  5. 獢碍ᥝ㱑ᰁ墋Ꭸ public void foo { // do nothing } •

    纷ୗ嘨᩼੝牧襑ᥝ䌃ጱ介手纷ୗ疰᩼੝
  6. 螕吚ጱ੗蕕 public class AuthenticationManager {
 public static final EXPIRATION_DAYS =

    14;
 
 public boolean expire(Token token) {
 Calendar calendar = computeNow();
 // 涢挨 token ጱ碻樌ฎ玽螂๗
 }
 
 protected Calendar computeNow() {
 return Calendar.getInstance();
 }
 }
  7. ፓ袅奾䯤 MyProject/
 AndroidManifest.xml
 res/
 ... (resources for main application)
 src/


    ... (source code for main application) ...
 tests/
 AndroidManifest.xml
 res/
 ... (resources for tests)
 src/
 ... (source code for tests)
  8. JUnit 塅ֺ public class AccountTest {
 
 @Test (expected=AccountInsufficientFundsException.class) 


    public void transferWithoutEnoughFunds() {
 long balance = 1000;
 long amontToTransfer = 2000;
 
 Account credit = new Acocunt(balance);
 Account debit = new Account();
 
 credit.transfer(debit, amountToTransfer);
 }
 }
  9. Instrumentation 塅ֺ public class WelcomeActivityTests extends ActivityInstrumentationTestCase2<WelcomeActivity> {
 ....
 


    public void testOnResumeNotAuthenticated() {
 Instrumentation instrumentation = getInstrumentation();
 Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(RegistrationActivity.class.getName(), null, false); // The user is not authenticated, so it should switch to RegisterActivity.
 Activity activity = instrumentation.waitForMonitor(monitor);
 assertNotNull(activity);
 instrumentation.removeMonitor(monitor);
 activity.finish();
 }
 }
  10. 虏 Model 蚤 View 獤櫝 • 虏 Model 揗揣 business

    logic ጱ蟂獤 • 虏 View 揗揣蚤 Android UI زկ磪橕ጱ蟂 獤 • ٌ犢蚤 Android ଘݣ磪橕ጱ蟂獤犖稳獈 View ጱ project Ӿ
  11. 虏 Model 蚤 View 獤櫝 • 虏 Model ጱ蟂獤౮傶奈羅ጱ Java

    Project • Model ݢ犥አ Desktop/Laptop ፗ矑介手 • 疥 View 揗砾ጱ揣犨㱑ᰁ婘ੜ • View 蚤 Model 戢㲘 • ಅ磪氥纈ጱ襑ᥝጱ虻碘᮷蚤 Model ᔱ玲 • Model ጱ蟂獤አ sync ጱොୗ蒂ቘ虻碘牧View አ Async ጱොୗ蚤 Model ᥝ虻碘 • ڥአ instrumentation 蝱ᤈ Android ଘݣ磪橕ጱ介手
  12. Eclemma EclEmma is a free Java code coverage tool for

    Eclipse, available under the Eclipse Public License. It brings code coverage analysis directly into the Eclipse workbench http://www.eclemma.org
  13. Cobertura Cobertura is a free Java tool that calculates the

    percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage. http://cobertura.github.io/cobertura/
  14. 介手薟荠ሲ • Eclemma for Eclipse • Cobertura for Gradle •

    叨ኞ介手䁭蔭 • Jacoco for Android project • 䒻֦ತڊ介手ጱ碻狡笙ധጱ瑿ො
  15. 介手薟荠ሲ • Eclemma for Eclipse • Cobertura for Gradle •

    叨ኞ介手䁭蔭 • Jacoco for Android project • 䒻֦ತڊ介手ጱ碻狡笙ധጱ瑿ො
  16. 瞱媲碉ݳ • 䒻ۗ㿁褧൉ṛ commit code ጱ毱ሲ • 仂੝ “It works

    on my machine!” ጱ眐丆 • 犋螂牧姘瞱樄咳㿁褧樄咳絑हጱӞ膌牧 螭ฎ䌘樄咳蝧ଶ磪䒻ۗ
  17. Calabash-Android Scenario Outline: register
 Given I am on the welcome

    screen
 When I am not have a valid account
 Then I should be able to register a new account with <type>
 Examples:
 | type | 
 | phone number | 
 | e-mail |
  18. Calabash-Android • Calabash ฎӞ㮆朼䌘 Android 膏 iOS ጱᛔ 㵕玕介手ૡٍ •

    ݢ犥አݗ承玕ጱොୗ砰䌃介手脻๜ • մ㴟膏PMݢ犥ፗ矑砰䌃介手አጱ脻๜ • Android 樄咳ᘏ襑ᥝႲے䌘䛑ጱ ID 妔 View