2013-03-13 47 views
1

我在Eclipse中運行Android JUnit測試。我有一個在JUnit運行期間測試過的函數。在函數內部我保存一些值到本地文件。但它似乎沒有被保存。什麼原因,以及我怎樣才能從該功能在控制檯輸出一些數據?在eclipse中運行android junit測試時將數據保存到文件

它與robotium開發人員提供的示例應用程序基本上是相同的單元測試代碼。

public void testDisplayBlackBox() { 
     //Enter any integer/decimal value for first editfield, we are writing 10 
     solo.enterText(0, "10"); 
     //Enter any interger/decimal value for first editfield, we are writing 20 
     solo.enterText(1, "20"); 
     //Click on Multiply button 
     solo.clickOnButton("Multiply"); 
     //Verify that resultant of 10 x 20 
     assertTrue(solo.searchText("200")); 
     ArrayList <View> aview= solo.getCurrentViews(); 
     for (View v:aview) 
     { 
      //System.out.println(v.toString()); 
      //solo.enterText(0, v.toString()); 
      try { 
       FileUtils.writeStringToFile(new File("test.txt"), v.toString()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      //FileUtils 
     } 
     //aview.get(0). 
    } 
+0

感謝您的建議,@ Mat – subodh 2013-03-13 10:15:34

+0

@subodh任何解決方案?我沒有看到任何。 – 2013-03-13 10:16:04

+0

你可以請張貼一些代碼嗎? – subodh 2013-03-13 10:18:40

回答

0

在測試執行過程中,您無法在本地保存文件。你可以將它保存例如SD卡,然後你就可以(在這裏更多的幫助:http://developer.android.com/tools/help/adb.html):拉亞行

public void testDisplayBlackBox() { 
    //Enter any integer/decimal value for first editfield, we are writing 10 
    solo.enterText(0, "10"); 
    //Enter any interger/decimal value for first editfield, we are writing 20 
    solo.enterText(1, "20"); 
    //Click on Multiply button 
    solo.clickOnButton("Multiply"); 
    //Verify that resultant of 10 x 20 
    assertTrue(solo.searchText("200")); 
    ArrayList <View> aview= solo.getCurrentViews(); 
    StringBuilder sB = new StringBuilder(); 
    for (View v:aview) 
    {   
     sB.append(v.toString()); 
    } 
    saveToFile(sB.toString()) 
} 


public void saveToFile(String text) { 
    try { 
     String path = Environment.getExternalStorageDirectory(); 
     File file = new File(path, test.txt); 
     FileOutputStream fos = new FileOutputStream(file); 
     byte[] data = text.getBytes(); 
     fos.write(data); 
     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

爲了調試,您可以使用android.util.Log:

Log.d("TEST", "text"); 
相關問題