首页 > 解决方案 > Pytest 和 Tox ADB 连接问题 - 设备未经授权

问题描述

场景:我正在尝试通过在 os.popen() 中使用 shell 命令将 ADB 连接到 Android 手机,作为自动化框架测试的一部分,我能够连接,但“adb 设备”将设备返回为已连接但未经授权,例如代码:

def test_connect_phone(self, phone):
        connect_stream = os.popen(f'adb connect {phone.host}:{phone.port}')
        print(connect_stream.read())
        install_stream = os.popen('adb devices')
        output = install_stream.read()
        print(output)
        assert output
# output
connected to <HOST>:<PORT>

List of devices attached
<HOST>:<PORT>   unauthorized

直接从终端运行相同的命令可以工作并且设备已获得授权:

adb connect <HOST>:<PORT>
adb devices

#output
connected to <HOST>:<PORT>

List of devices attached
<HOST>:<PORT>   device

注意:我将 tox 用于 pytest,这让我认为这可能是一个环境问题,但在测试中我在运行时看到我的主目录保持不变。任何帮助表示赞赏。

标签: python-3.xadbpytesttox

解决方案


找到了解决方案,显然我需要在“adb connect”之后添加 sleep(1),因为设备需要一段时间才能获得授权


推荐阅读