python - 访问 Travis-CI 上的剪贴板

标签 python pytest travis-ci pyperclip xclip

我正在尝试对我的 application 运行(集成?)测试,验证它是否确实使用 pyperclip 将预期的字符串复制到剪贴板。

这部分正在我的开发机器(Windows 10)上运行;但在 travis-ci 上失败,我在 travis 作业日志中看到以下内容。

self = <pyperclip.init_no_clipboard.<locals>.ClipboardUnavailable object at 0x7ff0cd743588>
args = ('7 809823 102890 string 291',), kwargs = {}
    def __call__(self, *args, **kwargs):
>       raise PyperclipException(EXCEPT_MSG)
E       pyperclip.PyperclipException: 
E           Pyperclip could not find a copy/paste mechanism for your system.
E           For more information, please visit https://pyperclip.readthedocs.io/en/latest/introduction.html#not-implemented-error
../../../virtualenv/python3.7.1/lib/python3.7/site-packages/pyperclip/__init__.py:301: PyperclipException

根据pyperclip documentation ,当 Linux 上没有复制/粘贴机制时,就会发生这种情况。解决方案是安装以下其中一项(引用 pyperclip 文档):

  • sudo apt-get install xsel 安装 xsel 实用程序。
  • sudo apt-get install xclip 安装 xclip 实用程序。
  • pip install gtk 安装 gtk Python 模块。
  • pip install PyQt4 安装 PyQt4 Python 模块。

所以在我的 .travis.yml 文件中,我有

before_install:
  - sudo apt-get install xclip

我也尝试过 xsel,结果相同。

由于travis上的系统是Ubuntu 16.04.6,所以我尝试将sudo apt-get install python3-pyperclip添加到before_install键中,结果相同。

我无法通过将 gtkPyQt4 添加到 .travis.yml< 中的 install 键来安装它们.

install:
  - pip install -r requirements_dev.txt
  - pip install PyQt4
  # or
  - pip install gtk

因为这两者都会导致以下错误:

 Could not find a version that satisfies the requirement gtk (from versions: )
No matching distribution found for gtk
The command "pip install gtk" failed and exited with 1 during .

此时,我的 before_install 看起来像这样:

  - sudo apt-get install xclip
  - sudo apt-get install xsel
  - sudo apt-get install python3-pyperclip
  - sudo apt-get install gtk2.0

这看起来有点矫枉过正(而且仍然不起作用);但我目前不知道如何通过该测试。任何指示将不胜感激。

谢谢

最佳答案

xclip 需要运行 X 服务器,而 Travis 机器以 headless 模式运行。您需要在虚拟帧缓冲区中运行该命令;除了 xclip 之外,还安装 xvfb 并使用 xvfb-run pytest 而不是 pytest。完整配置示例:

language: python
addons:
  apt:
    packages:
    - xclip
    - xvfb
python:
  - "3.7"

# setup installation
install:
  - pip install -r requirements_dev.txt

script: xvfb-run pytest

这是一个示例 build on Travis 。请注意,我使用 addons 来声明应与 APT 一起安装的依赖项;您在 before_install 部分中显式安装它们的解决方案也完全有效,只是口味问题。

关于python - 访问 Travis-CI 上的剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57743427/

相关文章:

python - python如何优化条件列表理解

python - 在 Azure DevOps 管道中找不到 Pytest

python - 如何模拟一个属性

github - Travis 推送失败但通过 PR

ios - 在 travis-ci 中跳过在本地运行的测试

python - 使用 Beautifulsoup 抓取时如何删除多个空行

python - Pygame-太空侵略者子弹

python - 导入错误: No module named control

python - Pytest跳过具有特定参数值的测试

r - devtools::test()有效,但devtools::check()无效。为什么?