I compiled a mini guide for configuring your project to use unit tests in Android studio, utilizing the mature JUnit framework:
- Create a folder in which you'll write all your unit tests (preferably com.example.app.tests)
- Create a new test class (preferably NameOfClassTestedTests, i.e BankAccountLoginActivityTests)
- Extend InstrumentationTestCase
- Write a failing unit test to make sure we succeeded configuring unit tests
- Note that a unit test method name must start with the word “test” (preferably testTestedMethodNameExpectedResult() i.e testBankAccountValidationFailedShouldLogout())
- Configure your project for unit tests:
- Open the 'Run...' menu and click 'edit configurations'
- Click the + button
- Select the Android Tests template
- Input a name for your run configuration (preferably 'AppName Tests')
- Select your app in the module combobox
- 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)
- Fill in the test package name from step 1 (i.e com.example.app.tests)
- Select the device you wish to run your tests on
- Apply and save the configuration
- Run unit tests (and expect failure):
- Select your newly created Tests configuration from the Run menu
- Click Run and read the results in the output console
Good luck making your code more readable, maintainable and well-tested!