简体   繁体   中英

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

I'm trying to run Appium tests written in python 3 on AWS Device Farm. As stated in the 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
      # ...

I did run tests successfully on Device Farm in the past, using a python 3 custom environment, with this spec file (I include the install phase only):

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
  # ...

Now, when I run the tests, I get this log an then the test crashes due to incompatible code.

[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)

Is python 3.x not supported anymore, or have there been undocumented changes? Is there a new way to run tests in a python 3 environment?

The documented way is still correct.

I found there was an error during the installation of the pytest package in the virtual environment. This made the py.test command refer to the default environment.

In my case the issue was resolved by installing an older version of pytest and bundling it in requirements.txt with other packages.

pip install pytest==6.2.4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM