How can I create tests in Android Studio?

后端 未结 12 2078
野的像风
野的像风 2020-11-27 10:38

Just downloaded Android Studio which is based off of the Intellij Idea.

How would one create tests?

I notice there is a option for create a Test Module but t

12条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 11:06

    The easiest way I found is the streamlined in my following blog post:

    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)
    3. Extend InstrumentationTestCase
    4. Write a failing unit test to make sure we succeeded configuring unit tests
    5. Note that a unit test method name must start with the word “test” (preferably testTestedMethodNameExpectedResult() i.e testBankAccountValidationFailedShouldLogout())
    6. Configure your project for unit tests:
    7. Open the 'Run...' menu and click 'edit configurations'
    8. Click the + button
    9. Select the Android Tests template
    10. Input a name for your run configuration (preferably 'AppName Tests')
    11. Select your app in the module combobox
    12. 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)
    13. Fill in the test package name from step 1 (i.e com.example.app.tests)
    14. Select the device you wish to run your tests on
    15. Apply and save the configuration
    16. Run unit tests (and expect failure):
    17. Select your newly created Tests configuration from the Run menu
    18. Click Run and read the results in the output console

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

提交回复
热议问题