为什么 Travis 在 运行 单元测试时找不到 rospy?

Why does Travis fail to find rospy when running unit tests?

我们有需要 rospy 的单元测试(其中一个测试使用 geometry_msgs/Twist)。

当我们运行在本地进行单元测试时,一切正常。

pytest
============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: /home/cedric/Programming/robotics-prototype, inifile: pytest.ini
collected 10 items                                                             

tests/unit/branch_name_test.py ......                                    [ 60%]
tests/unit/drive_controls_test.py ..                                     [ 80%]
tests/unit/import_test.py ..                                             [100%]

========================== 10 passed in 2.73 seconds ===========================

当我们在 Travis 上 运行 它时,它失败了。这是因为它找不到rospy。我们的 Travis 安装程序安装 ROS,运行s catkin_make 成功,但在单元测试中失败。

repo 是 public,所以如果您有兴趣查看完整的 travis 日志,请单击 here

os: linux
dist: bionic
language: python
cache:
  - apt
env:
  global:
    - REPO_ROOT=$(pwd)
    - ARDUINO_IDE_VERSION="1.8.12"
    - ARDUINO_PATH="$HOME/arduino-$ARDUINO_IDE_VERSION" # Required for CMake script
    - TEENSYDUINO_VERSION="152"
    - ROS_PYTHON_VERSION="3.6"
    - ROS_VERSION="melodic"
    - OS_VERSION="bionic"

install:
  # Taken from this tutorial https://github.com/newdigate/teensy-blink
  - mkdir -p robot/rover/build
  - mkdir -p ~/Arduino/libraries
  - wget --quiet https://downloads.arduino.cc/arduino-$ARDUINO_IDE_VERSION-linux64.tar.xz
  - tar xf arduino-$ARDUINO_IDE_VERSION-linux64.tar.xz -C /home/$USER/
  - curl -fsSL https://www.pjrc.com/teensy/td_$TEENSYDUINO_VERSION/TeensyduinoInstall.linux64 -o TeensyduinoInstall.linux64
  - chmod +x TeensyduinoInstall.linux64
  - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
  - sleep 3
  - export DISPLAY=:1.0
  - ./TeensyduinoInstall.linux64 --dir=$ARDUINO_PATH
  
  # Install pip dependencies and setup robot python package
  - pip install -r requirements.txt -r requirements-dev.txt
  - pip install -e .
    
  # Install ros-base and necessary packages 
  - sudo sh -c "echo \"deb http://packages.ros.org/ros/ubuntu $OS_VERSION main\" > /etc/apt/sources.list.d/ros-latest.list"
  - sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
  - sudo apt update -q
  - sudo apt install -y ros-$ROS_VERSION-ros-base ros-$ROS_VERSION-cv-camera ros-$ROS_VERSION-web-video-server 
  - sudo apt install -y python-rosdep python-wstool 
    
  # Install ros package dependencies
  - cd $REPO_ROOT/robot/rospackages
  - sudo rosdep init
  - rosdep update 
  - rosdep install -y -r --from-paths src --ignore-src --rosdistro $ROS_VERSION

script:
  # Set up 'robot' python module and test python installation
  - cd $REPO_ROOT
  - python setup.py develop
  - pytest --continue-on-collection-errors

  # Set up and initialize catkin workspace for ros packages
  - cd $REPO_ROOT/robot/rospackages
  - source /opt/ros/$ROS_VERSION/setup.bash
  - catkin_make

  # Allow to compile arduino code in external IDEs or something
  - cd $REPO_ROOT/robot/rover/build
  - cmake ..
  - make
    
notifications:
  email:
    recipients:
      - (hidden for the purpose of the question)
    on_success: change
    on_failure: change

python:
  - "3.6"

git:
  submodules: true

看来您是在采购ROS环境之前调用测试。你需要改变它:

script:
  # Set up and initialize catkin workspace for ros packages
  - cd $REPO_ROOT/robot/rospackages
  - source /opt/ros/$ROS_VERSION/setup.bash
  - catkin_make

  # Set up 'robot' python module and test python installation
  - cd $REPO_ROOT
  - python setup.py develop
  - pytest --continue-on-collection-errors