如何使用 python 3.x 进行 AWS Device Farm 运行 Appium 测试?

How do I make AWS Device Farm run Appium tests with python 3.x?

我正在尝试 运行 在 AWS Device Farm 上用 python 3 编写的 Appium 测试。 如 documentation

中所述

Python 2.7 is supported in both the standard environment and using Custom Mode. It is the default in both when specifying Python.

Python 3 is only supported in Custom Mode. To choose Python 3 as your python version, change the test spec to set the PYTHON_VERSION to 3, as shown here:

phases:
  install:
    commands:
      # ...
      - export PYTHON_VERSION=3
      - export APPIUM_VERSION=1.14.2
      # Activate the Virtual Environment that Device Farm sets up for Python 3, then use Pip to install required packages.
      - cd $DEVICEFARM_TEST_PACKAGE_PATH
      - . bin/activate
      - pip install -r requirements.txt
      # ...

我过去在 Device Farm 上成功进行了 运行 测试,使用 python 3 自定义环境,使用这个规范文件(我只包括安装阶段):

phases:
  install:
    commands:
      # Device Farm support two major versions of Python, each with one minor version: 2 (minor version 2.7), and 3 (minor version 3.7.4).
      # The default Python version is 2, but you can switch to 3 by setting the following variable to 3:
      - export PYTHON_VERSION=3

      # This command will install your dependencies and verify that they're using the proper versions that you specified in your requirements.txt file. Because Device Farm preconfigures your environment within a
      # Python Virtual Environment in its setup phase, you can use pip to install any Python packages just as you would locally.
      - cd $DEVICEFARM_TEST_PACKAGE_PATH
      - . bin/activate
      - pip install -r requirements.txt
  # ...

现在,当我 运行 测试时,我得到了这个日志,然后测试由于代码不兼容而崩溃。

[DEVICEFARM] ########### Entering phase test ###########
 
[DeviceFarm] echo "Navigate to test package directory"
Navigate to test package directory
[DeviceFarm] cd $DEVICEFARM_TEST_PACKAGE_PATH
[DeviceFarm] echo "Start Appium Python test"
Start Appium Python test
[DeviceFarm] py.test tests/ --junit-xml $DEVICEFARM_LOG_DIR/junitreport.xml
============================= test session starts ==============================
platform linux2 -- Python 2.7.6, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: /tmp/scratchrPuGRa.scratch/test-packageM5E89M/tests, inifile: 
collected 0 items / 3 errors

==================================== ERRORS ====================================
____________________ ERROR collecting test_home_buttons.py _____________________
/usr/local/lib/python2.7/dist-packages/_pytest/python.py:610: in _importtestmodule
    mod = self.fspath.pyimport(ensuresyspath=importmode)

python 3.x 是否不再受支持,或者是否有未记录的更改? 在 python 3 环境中有 运行 测试的新方法吗?

记录的方法仍然正确。

发现在虚拟环境中安装pytest包时出现错误。这使得 py.test 命令引用默认环境。

在我的例子中,通过安装旧版本的 pytest 并将其与其他软件包捆绑在 requirements.txt 中解决了这个问题。

pip install pytest==6.2.4