About Me

My photo
Drum & Bass Producer, Software Developer, Love my Cats

Configuring Android Studio for Unit Testing mini-guide

I compiled a mini guide for configuring your project to use unit tests in Android studio, utilizing the mature JUnit framework:
  1. Create a folder in which you'll write all your unit tests (preferably com.example.app.tests)
  2. Create a new test class (preferably NameOfClassTestedTests, i.e BankAccountLoginActivityTests)
    1. Extend InstrumentationTestCase
    2. Write a failing unit test to make sure we succeeded configuring unit tests
    3. Note that a unit test method name must start with the word “test” (preferably testTestedMethodNameExpectedResult() i.e testBankAccountValidationFailedShouldLogout())
  3. Configure your project for unit tests:
    1. Open the 'Run...' menu and click 'edit configurations'
    2. Click the + button
    3. Select the Android Tests template
    4. Input a name for your run configuration (preferably 'AppName Tests')
    5. Select your app in the module combobox
    6. Select the “All In Package” radio button (generally you'd want to select this option because it runs all unit tests in all your test classes)
    7. Fill in the test package name from step 1 (i.e com.example.app.tests)
    8. Select the device you wish to run your tests on
    9. Apply and save the configuration
  4. Run unit tests (and expect failure):
    1. Select your newly created Tests configuration from the Run menu
    2. Click Run and read the results in the output console

Good luck making your code more readable, maintainable and well-tested!