2012-02-21 64 views
21

我們有一個android設備,作爲測試的一部分,我需要在目標設備上執行一個控制檯測試應用程序。如果測試應用程序檢測到錯誤,則返回-1。亞行錯誤代碼

我可以使用adb shell在目標上遠程運行測試應用程序,但找不到返回代碼的方法。我需要這個,以便我可以將其構建到自動化測試套件中。

我可以嘗試控制檯輸出的某些失敗文本,但這有點gr gre。有誰知道更優雅的解決方案?

+0

我有同樣的問題。無論adb執行什麼,它總是返回0. – 2012-02-21 15:19:56

+2

[問題3254:\t adb shell不返回程序的退出代碼](https://code.google.com/p/android/issues/detail?id=3254) – n611x007 2013-07-30 10:56:23

回答

9

這是一種解決方法來獲取退出代碼: adb shell'{your command here}>/dev/null 2> & 1; echo $?'

這是在Ruby中圍繞亞行的包裝:

def adb(opt) 
    input = "#{adb_command} #{opt[:command]} #{opt[:params]}" 
    puts "Executing #{input}...\n" 
    output = nil 
    exit_code = 0 

    def wait_for(secs) 
    if secs 
     begin 
     Timeout::timeout(secs) { yield } 
     rescue 
     print 'execution expired' 
     end 
    else 
     yield 
    end 
    end 

    wait_for(opt[:timeout]) do 
    case opt[:command] 
    when :install, :push, :uninstall 
     output, exit_code = `#{input}`, $?.to_i 
    when :shell 
     input = "#{adb_command} shell \"#{opt[:params]}; echo \\$?\"" 
     output = `#{input}`.split("\n") 
     exit_code = output.pop.to_i 
     output = output.join("\n") 
    else 
     raise 'Error: param command to adb not defined!' 
    end 
    end 

    return if opt[:ignore_fail] and output =~ /#{opt[:ignore_fail]}/ 
    raise output unless exit_code == 0 
end 
+1

你有沒有需要使用'echo \ $?'? – devin 2012-10-05 14:34:23

4

你可以使用Facebook的fb-adb,一個「更好的外殼爲Android設備」的「傳播計劃退出狀態,而不是總是以狀態0退出」。