2016-09-28 2 views
2

基本的には、モックロケーションプロバイダを使用するAndroidTestを実行する必要があるたびに、デバイスエミュレータの[設定] - > [モックの場所]のボックスを手動でチェックする必要があります。どのようにアンドロイドテストから直接この作業を自動化するには?エスプレッソ/ uiautomator /他の何かを使用する方法はありますか?uiautomatorとespressoでAndroidTestを実行する前に、Androidデバイスでモックの場所を許可する方法を設定するには?

+0

答えの半分:http://stackoverflow.com/questions/36339817/is-there-an-adb-command-to-toggle-allow-mock-locations –

+0

ありがとう、私はコメントを追加しました質問。見てみよう... – Daniele

+0

あなたはそのadbコマンドを取ってhttps://developer.android.com/studio/run/rundebugconfig.html#android-testsに起動前操作として追加しようとしましたか? –

答えて

2

私はそれが私が望むようにすることができた。コメントに投稿されたリンクのおかげで。

task enableMockLocationForTestsOnDevice(type: Exec) { 
    Properties properties = new Properties() 
    properties.load(project.rootProject.file('local.properties').newDataInputStream()) 
    def sdkDir = properties.getProperty('sdk.dir') 
    def adb = "$sdkDir/platform-tools/adb" 
    description 'enable mock location on connected android device before executing any android test' 
    commandLine "$adb", 'shell', 'appops', 'set', 'indian.fig.whatsaround', 'android:mock_location', 'allow' 
} 

afterEvaluate { 
    // Note: the app must be already installed on device in order to this to run! 
    connectedDebugAndroidTest.dependsOn enableMockLocationForTestsOnDevice 
    connectedAndroidTest.dependsOn enableMockLocationForTestsOnDevice 
} 


// execute android tests before realising a new apk 
tasks.whenTaskAdded { task -> 
    if (task.name == 'assembleRelease') { 
     task.dependsOn('enableMockLocationForTestsOnDevice') 
     task.dependsOn('testReleaseUnitTest') // Run unit tests for the release build 
     task.dependsOn('connectedAndroidTest') // Installs and runs instrumentation tests for all flavors on connected devices. 

    } 
} 

また、あなたは「実行」の設定を編集し、実行する前に、としてそれを追加する必要がありアンドロイドスタジオを経由してアプリを起動する前にタスクを実行する必要がある場合:私は私のGradleファイルに以下のコードを追加しました。

関連する問題