How can I create tests in Android Studio?

后端 未结 12 1948
野的像风
野的像风 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:00

    Add below lib inside gradle file

     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
    

    Create class HomeActivityTest inside androidTest directory and before running the test add flurry_api_key and sender_id string inside string resource file and change the value for failure and success case.

    @RunWith(AndroidJUnit4.class)
    public class HomeActivityTest
    {
        private static final String SENDER_ID = "abc";
        private static final String RELEASE_FLURRY_API_KEY = "xyz";
    
        @Test
        public void gcmRegistrationId_isCorrect() throws Exception
        {
            // Context of the app under test.
            Context appContext = InstrumentationRegistry.getTargetContext();
    
            Assert.assertEquals(SENDER_ID, appContext.getString(R.string.sender_id));
        }
    
        @Test
        public void flurryApiKey_isCorrect() throws Exception
        {
            // Context of the app under test.
            Context appContext = InstrumentationRegistry.getTargetContext();
    
            Assert.assertEquals(RELEASE_FLURRY_API_KEY, appContext.getString(R.string.flurry_api_key));
        }
    }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-27 11:08

    As of now (studio 0.61) maintaining proper project structure is enough. No need to create separate test project as in eclipse (see below).

    Tests structure

    0 讨论(0)
  • 2020-11-27 11:09

    Edit: As of 0.1.8 this is now supported in the IDE. Please follow the instructions in there, instead of using the instructions below.

    Following the Android Gradle Plugin User Guide I was able to get tests working on the command line by performing the following steps on a newly created project (I used the default 'com.example.myapplication' package):

    1. Add a src/instrumentTest/java directory for the tests
    2. Add a test class (extending ActivityTestCase) in the package com.example.myapplication.test
    3. Start a virtual device
    4. On the command line (in the MyApplicationProject/MyApplication directory) use the command '../gradlew connectedInstrumentTest'

    This ran my tests and placed the test results in MyApplicationProject/MyApplication/build/reports/instrumentTests/connected. I'm new to testing Android apps, but it seem to work fine.

    From within the IDE, it's possible to try and run the same test class. You'll need to

    1. Update build.gradle to list Maven Central as a repo
    2. Update build.gradle add JUnit 3.8 as a instrumentTestCompile dependency e.g. instrumentTestCompile 'junit:junit:3.8'
    3. In 'Project Structure' manually move JUnit to be first in the dependency order

    However this fails (the classpath used when running the tests is missing the test output directory). However, I'm not sure that this would work regardless as it's my understanding that an Android specific test runner is required.

    0 讨论(0)
  • 2020-11-27 11:12

    Android Studio keeps evolving so the responses above will eventually be no longer applicable. For the current version of Android Studio 1.2.1.1, there's a nice tutorial on testing at:

    http://evgenii.com/blog/testing-activity-in-android-studio-tutorial-part-1/

    0 讨论(0)
  • 2020-11-27 11:16

    Android Studio v.2.3.3

    Highlight the code context you want to test, and use the hotkey: CTRL+SHIFT+T

    Use the dialog interface to complete your setup.

    The testing framework is supposed to mirror your project package layout for best results, but you can manually create custom tests, provided you have the correct directory and build settings.

    0 讨论(0)
提交回复
热议问题