How to do Android Testing
(Part I)
John
Outline
Part I
● Software Testing
○ What is Software Testing
○ Why Testing is important
○ Types
● Android Test
○ Unit Testing
■ JUnit
■ Mock object
■ Case Stuydies
Part II
○ Android Instrumented tests
○ Case studies
● Best practice - Android Device
Testing
● Test Android Device on the cloud
● How we bring Testing into Android
Team?
Software Testing
What is Software Testing
Software testing is an activity to check whether the actual results match the
expected results and to ensure that the software system is defect free. It involves
execution of a software component or system component to evaluate one or more
properties of interest.
TL;DR:
Check whether your implementation is matched the project goal or tasks.
Why testing is important
Testing is important because software bugs could be expensive or even
dangerous. Software bugs can potentially cause monetary(money loss) and
human loss, history is full of such examples:
● Nissan cars have to recall over 1 million cars from the market due to software failure in the airbag sensory detectors.
There has been reported two accident due to this software failure.
● Starbucks was forced to close about 60 percent of stores in the U.S and Canada due to software failure in its POS
system. At one point store served coffee for free as they unable to process the transaction.
● Some of the Amazon’s third party retailers saw their product price is reduced to 1p due to a software glitch. They
were left with heavy losses.
● China Airlines Airbus A300 crashed due to a software bug on April 26, 1994, killing 264 innocent live
● ….
If Origami made user to loss money even we can refund but reputation,
customer relationship, … all may loss.
Software Testing Types
White Box Testing Black Box Testing
It’s Software Engineer’s duty It’s QA’s duty
● Use Case Testing
● Software performance
testing
● Pressure Testing
● ...
Testing Cycle
Software Testing, we are not used to
do, we feel it troublesome, take a lot of
time. But it is a part of Software
Development and it is the most we
need to be serious on it. Additionally, it
can improve to write good code.
Android Test
Unit Testing
Def: A unit test should test a class in isolation. Side effects from other classes or
the system should be eliminated if possible. To eliminate these side effects you
have to replace dependencies to other classes. This can be done via using
replacements for the real dependencies.
3 phases(Also known as Arrange, Act and Assert, or simply AAA.):
1. First, it initializes a small piece of an application it wants to test (also
known as the system under test, or SUT)
2. then it applies some stimulus to the system under test (usually by calling
a method on it)
3. finally, it observes the resulting behavior
What’s the good Unit Test
● Easy to write
● Readable
● Reliable
● Fast
● Truly unit, not integration
Write testable code
● SRP (Single Responsibility Principle)
● IoC (Inversion of Control)
● ...
It is as same as we do modularize or OOP with Design Pattern. If you are used to
those design principle you are writing testable code.
JUnit
JUnit is a unit testing framework for the Java programming language. JUnit has
been important in the development of test-driven development, and is one of a
family of unit testing frameworks which is collectively known as xUnit that
originated with SUnit.
How do we use Unit Test on Android
Android tests are based on JUnit, and you can run them either as local unit tests
on the JVM or as instrumented tests on an Android device.
Test types
● Local unit tests
In Android Studio,
Located at [module-name]/src/test/java/.
● Instrumented tests (talk in next part)
How do we use Unit Test on Android - JUnit
Set up the environment
Be careful of what build configuration you are using. If Unit Test, it should be ‘testCompile’,
not ‘androidTestCompile’
Mock testing lib
How do we use Unit Test on Android - JUnit
Suppose now we want to test the class,
EmailValidator.
- We should make a test case as smallest as
possible.
- We should make the true and the false cases
- Naming your UnitTest class with a postfix,
‘Test’
- Naming your readable test case function.
- Give a expectant result.
EmailValidator
+ isValidEmail(String)
+ ...
How do we use Unit Test on Android - JUnit
ExpectationReal Result
How do we use Unit Test on Android - JUnit
Run your JUnit Test
How do we use Unit Test on Android - JUnit
See the result.
How do we use Unit Test on Android - JUnit and Mock
Set up your environment
Add Mockito in your dependencies,
i.e., testCompile 'org.mockito:mockito-core:1.10.19'
Recommend use @RunWith(AndroidJUnitRunner.class) annotation as your
test rule.
How do we use Unit Test on Android - JUnit and Mock
How it work:
● Not talking about TDD or BDD
● As Unit Test definition, ‘A unit test should test a class in isolation’
How do we use Unit Test on Android - JUnit and Mock
How do we use Unit Test on Android - JUnit and Mock
How do we use Unit Test on Android - JUnit and Mock
How do we use Unit Test on Android - JUnit and Mock
spy()/@Spy: partial mocking, real methods are invoked but still can be verified and
stubbed
Case Studies
OrigamiAuthenticatedState
× onCreated
× onAuthenticated
× onStop
× tryLogin
Origami
AuthenticationApiClient
IAuthentication.
AuthenticationCallback
Case Studies
Protected Method
Case Studies
Package/Private
Static Method
Case Studies
Asynchronous Function: OrigamiAuthenticatedState.tryLogin() ->
AuthenticationApiClient.login -----> OrigamiAuthenticatedState.onAuthenticated()
Summary
● Software Testing is to evaluate what you did match project or task goal.
● Software Testing is also a part of Software Development and it’s coverted in
Software Engineer’s duty.
● Android Test Types: Local Unit Testing and Instruments Testing
● Unit Test: Write testable code and good test code.
● How to do Android Unit Test with Mock object.
Sharing
● Google Test Blog: https://testing.googleblog.com/
● Chiu-Ki Chan blog: http://blog.sqisland.com/
After being a software engineer for 6+ years at Google and 1.5 years at two
startups, I now run my own mobile development company.
https://developers.google.com/experts/people/chiu-ki-chan
○ How to be an Android Expert - Android Sumit 2015
References
● http://www.guru99.com/software-testing-introduction-importance.html
● Frustration-free Android Device Test:
https://m.signalvnoise.com/5-steps-to-creating-frustration-free-android-test-de
vices-9bb2750edd19#.h6r7y2bgd
● https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters
● https://testing.googleblog.com/
● Do’s and Don’ts:
https://blog.mindorks.com/the-dos-and-don-ts-of-writing-test-cases-in-android-
70f1b5dab3e1#.jntarglw5
● https://afourtech.com/devices-to-test-android-app/

Android testing part i

  • 1.
    How to doAndroid Testing (Part I) John
  • 2.
    Outline Part I ● SoftwareTesting ○ What is Software Testing ○ Why Testing is important ○ Types ● Android Test ○ Unit Testing ■ JUnit ■ Mock object ■ Case Stuydies Part II ○ Android Instrumented tests ○ Case studies ● Best practice - Android Device Testing ● Test Android Device on the cloud ● How we bring Testing into Android Team?
  • 3.
  • 4.
    What is SoftwareTesting Software testing is an activity to check whether the actual results match the expected results and to ensure that the software system is defect free. It involves execution of a software component or system component to evaluate one or more properties of interest. TL;DR: Check whether your implementation is matched the project goal or tasks.
  • 5.
    Why testing isimportant Testing is important because software bugs could be expensive or even dangerous. Software bugs can potentially cause monetary(money loss) and human loss, history is full of such examples: ● Nissan cars have to recall over 1 million cars from the market due to software failure in the airbag sensory detectors. There has been reported two accident due to this software failure. ● Starbucks was forced to close about 60 percent of stores in the U.S and Canada due to software failure in its POS system. At one point store served coffee for free as they unable to process the transaction. ● Some of the Amazon’s third party retailers saw their product price is reduced to 1p due to a software glitch. They were left with heavy losses. ● China Airlines Airbus A300 crashed due to a software bug on April 26, 1994, killing 264 innocent live ● …. If Origami made user to loss money even we can refund but reputation, customer relationship, … all may loss.
  • 6.
    Software Testing Types WhiteBox Testing Black Box Testing It’s Software Engineer’s duty It’s QA’s duty ● Use Case Testing ● Software performance testing ● Pressure Testing ● ...
  • 8.
  • 9.
    Software Testing, weare not used to do, we feel it troublesome, take a lot of time. But it is a part of Software Development and it is the most we need to be serious on it. Additionally, it can improve to write good code.
  • 10.
  • 11.
    Unit Testing Def: Aunit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. To eliminate these side effects you have to replace dependencies to other classes. This can be done via using replacements for the real dependencies. 3 phases(Also known as Arrange, Act and Assert, or simply AAA.): 1. First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT) 2. then it applies some stimulus to the system under test (usually by calling a method on it) 3. finally, it observes the resulting behavior
  • 12.
    What’s the goodUnit Test ● Easy to write ● Readable ● Reliable ● Fast ● Truly unit, not integration
  • 13.
    Write testable code ●SRP (Single Responsibility Principle) ● IoC (Inversion of Control) ● ... It is as same as we do modularize or OOP with Design Pattern. If you are used to those design principle you are writing testable code.
  • 14.
    JUnit JUnit is aunit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit.
  • 15.
    How do weuse Unit Test on Android Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as instrumented tests on an Android device. Test types ● Local unit tests In Android Studio, Located at [module-name]/src/test/java/. ● Instrumented tests (talk in next part)
  • 16.
    How do weuse Unit Test on Android - JUnit Set up the environment Be careful of what build configuration you are using. If Unit Test, it should be ‘testCompile’, not ‘androidTestCompile’ Mock testing lib
  • 17.
    How do weuse Unit Test on Android - JUnit Suppose now we want to test the class, EmailValidator. - We should make a test case as smallest as possible. - We should make the true and the false cases - Naming your UnitTest class with a postfix, ‘Test’ - Naming your readable test case function. - Give a expectant result. EmailValidator + isValidEmail(String) + ...
  • 18.
    How do weuse Unit Test on Android - JUnit ExpectationReal Result
  • 19.
    How do weuse Unit Test on Android - JUnit Run your JUnit Test
  • 20.
    How do weuse Unit Test on Android - JUnit See the result.
  • 21.
    How do weuse Unit Test on Android - JUnit and Mock Set up your environment Add Mockito in your dependencies, i.e., testCompile 'org.mockito:mockito-core:1.10.19' Recommend use @RunWith(AndroidJUnitRunner.class) annotation as your test rule.
  • 22.
    How do weuse Unit Test on Android - JUnit and Mock How it work: ● Not talking about TDD or BDD ● As Unit Test definition, ‘A unit test should test a class in isolation’
  • 23.
    How do weuse Unit Test on Android - JUnit and Mock
  • 24.
    How do weuse Unit Test on Android - JUnit and Mock
  • 25.
    How do weuse Unit Test on Android - JUnit and Mock
  • 26.
    How do weuse Unit Test on Android - JUnit and Mock spy()/@Spy: partial mocking, real methods are invoked but still can be verified and stubbed
  • 27.
    Case Studies OrigamiAuthenticatedState × onCreated ×onAuthenticated × onStop × tryLogin Origami AuthenticationApiClient IAuthentication. AuthenticationCallback
  • 28.
  • 29.
  • 30.
    Case Studies Asynchronous Function:OrigamiAuthenticatedState.tryLogin() -> AuthenticationApiClient.login -----> OrigamiAuthenticatedState.onAuthenticated()
  • 31.
    Summary ● Software Testingis to evaluate what you did match project or task goal. ● Software Testing is also a part of Software Development and it’s coverted in Software Engineer’s duty. ● Android Test Types: Local Unit Testing and Instruments Testing ● Unit Test: Write testable code and good test code. ● How to do Android Unit Test with Mock object.
  • 32.
    Sharing ● Google TestBlog: https://testing.googleblog.com/ ● Chiu-Ki Chan blog: http://blog.sqisland.com/ After being a software engineer for 6+ years at Google and 1.5 years at two startups, I now run my own mobile development company. https://developers.google.com/experts/people/chiu-ki-chan ○ How to be an Android Expert - Android Sumit 2015
  • 33.
    References ● http://www.guru99.com/software-testing-introduction-importance.html ● Frustration-freeAndroid Device Test: https://m.signalvnoise.com/5-steps-to-creating-frustration-free-android-test-de vices-9bb2750edd19#.h6r7y2bgd ● https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters ● https://testing.googleblog.com/ ● Do’s and Don’ts: https://blog.mindorks.com/the-dos-and-don-ts-of-writing-test-cases-in-android- 70f1b5dab3e1#.jntarglw5 ● https://afourtech.com/devices-to-test-android-app/