Create separate test module/project in Android Stu

2019-02-26 02:41发布

I'm using Android Studio version 0.8.14. Currently, after creating a new Android project, by default the test source code folder androidTest is located inside the src folder of the app module, at the same level as main (which contains application source code). But if, for any particular reason, I want to move the test code into a separate module in my project, or even further, into a separate project other than the project I'm using for application source code, can I do this?

To make it easier for you to understand what I'm asking, a 'picture' would be better. The current default new project structure which Android Studio create is something looks like this:

My Project
    |___my app module
        |___src
            |___androidTest
            |___main

Now I want something looks like this:

My Project
    |___my app module
    |    |___src
    |        |___main
    |___my test module

or this:

My App Project
|    |___my app module
|
My Test Project (some how linked to My App Project)

How can I get this? Any help would be appreciated.

2条回答
forever°为你锁心
2楼-- · 2019-02-26 03:32

If I understand what you are asking, the bit of information that you might be missing is to add something like:

include ':my app module', ':my test module'

to your settings.gradle file. I use this technique to separate out my 'connected' tests from pure unit tests. You might be interested in checking out a fork that I made of the popular deckard-gradle project that does exactly this: https://github.com/jdonmoyer/deckard-gradle

查看更多
Viruses.
3楼-- · 2019-02-26 03:37

The technique for specifying a test directory other than "androidTest" is to add a "sourceSets" configuration option to the "build.gradle" file as shown below:

 sourceSets {
        androidTest {
            setRoot('src/test')
        }
 }

The above example would only change the name of the test directory from "androidTest" to "test".

But you could try the same technique using a relative or full path name such as

setRoot('../my test module/my app module/src/androidTest')

or

setRoot('../../My Test Project/src/androidTest')
查看更多
登录 后发表回答