2012-02-21 63 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退出”。