Adding layout resources to androidTest

前端 未结 3 547
清酒与你
清酒与你 2021-02-13 01:13

I would like to add layout xml files into my androidTest folder to be used only for testing.

I added res/layout folder to androidTest

3条回答
  •  灰色年华
    2021-02-13 01:48

    It is tricky to add xml resources to androidTest.

    Android Instrumentation tests create another APK to run the tests against your original application. Although you can access your Context and objects from your main application, you cannot modify the generated APK file of your app.

    That means you cannot put additional layout xml to your original application from tests that are in the androidTest folder.

    Solution:

    Alternatively,

    • you can create a buildType called espresso.
    • Then, create an espresso folder where you can put any java or Android resource you want to add.
      • You can even modify your AndroidManifest there.
    • Then, use testBuildType 'espresso'

    Your build.gradle should look like this:

    android {
      testBuildType 'espresso'
    
      buildTypes {
        espresso.initWith(buildTypes.release)
      }
    }
    
    dependencies {
      espressoCompile 'somedependency' // you can even have special dependencies
    }
    

    When you run your espresso tests around that flavor, you will have an access to additional layout xml files you added.

    Should look like this:

提交回复
热议问题