2017-10-26 19 views
0

私は知っておきたいことですが、計測テストの実行中にシステムと通信する方法はありますか。 例: オンボードにIRポートの電話があります&プライベートSDKを使用して作業することもできますし、自分のアプリケーションで調整することもできます。 Instrumentationのテストケースでは、テスト前に設定したい外部イベントに基づいて、テストアプリケーションの動作をテストしたいと思っています。それは非常に単純な例だが、「条件」がたくさんある、そして高へのSDKの更新頻度 AndroidJUnitとのプロセス間通信またはadb通信

@Test 
public void test() throws Exception { 
    setupExternalCondition(condition1_ON); // setup external transiver 
    assertNotNull(IR.read()); 
    assertTrue(assertIR.write()); 

    setupExternalCondition(condition1_OFF); 
    assertNotNull(IR.read()); 
    assertFalse(IR.write()); 
} 

みたいだ
が見えます。私はこの検証をすべて手動で行うことはできません。また、 "transiver & SDKチーム"にカバレッジの単体テストを書くためのモック州リストを作成することはできません。だから私はどうにかして外部条件を設定するためにローカルマシン(またはCIマシン)上でイベント(またはテストケース実行前にtestName)を受け取るためにTestRunerに外部コンポーネントの実行を注入したい。

appUnderTestでTCPサーバーを実行して外部条件の変更をリクエストする簡単な解決策 - 私はそれが可能かどうかは確かではなく、安定した接続(Wi-Fi)についてはわからないため、adb 。

提案がありますか?

P.S:テストデバイスにはroot権限があります。

答えて

0

だから、悪くはないが理想的な解決策を見つけてください。 まだ良い提案を待っていますが、もしこの答えが他人に役立つかもしれません。私はテストの隣にクラスを追加し、ローカルマシンとAndroidJUnitTest間の「橋を建設」のために :

class IPCServiceBridge extends BroadcastReceiver { 
    private static final String FILTER_ID = "IPC_SERVICE"; 
    private static IPCServiceBridge sInstance; 
    private boolean mIsPermitted; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals("ipc.service.action")) { 
      mIsPermitted = true; 
     } 
    } 


    public static IPCServiceBridge getInstance() { 
     if (sInstance == null) { 
      sInstance = new IPCServiceBridge(); 
      IntentFilter filter = new IntentFilter(); 
      filter.addAction("ipc.service.action"); 
      Context context = InstrumentationRegistry.getContext(); 
      context.registerReceiver(sInstance, filter); 
     } 
     return sInstance; 
    } 

    public void sendIpcCommand(String commandName) { 
     try { 
      int i = 30; 
      mIsPermitted = false; 
      while (i > 0) { 
       pub("request:" + commandName); 
       Thread.sleep(1000); 
       if (mIsPermitted) { 
        break; 
       } 
       i--; 
      } 
     } catch (InterruptedException e) { 
      throw new RuntimeException(e); 
     } 
     if (!mIsPermitted) { 
      throw new RuntimeException("IPC service does not respond"); 
     } 
    } 

    private static void pub(String msg) { 
     Log.e(FILTER_ID, msg); 
    } 
} 

私はadbのlogcat -s「FILTER_NAME」を開始したより、InsttUnitテストのために適用すべき条件解析して確認してください。条件が整うと、必要な処置をして放送受信機を送り返します。

@Test 
public void test2() throws Exception { 
    IPCServiceBridge.getInstance().sendIpcCommand("CONDITION#123"); 
} 

正常に動作しますが、それは非常に安定しているとは確信していません。