我正在尝试对我的应用程序运行(集成?(测试,以验证它是否确实使用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文档,当没有复制/粘贴机制时,这种情况发生在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
键中,结果相同。
我无法通过将gtk
或PyQt4
添加到.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机器以无头模式运行。您需要在虚拟帧缓冲区中运行该命令;除了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
下面是一个基于 Travis 构建的示例。请注意,我使用addons
来声明应该与 APT 一起安装的依赖项;在before_install
部分中显式安装它们的解决方案也是完全有效的,只是品味问题。