Automating unit testing in PyQt5 using Gitlab's CI: QXcbConnection error

自闭症网瘾萝莉.ら 提交于 2019-12-10 22:47:19

问题


I am creating a GUI skeleton (for now) using PyQt5 and I have written some unit tests (using Python's unittest package) for testing its basic features. While trying to automate the procedure of running those unit tests each time a commit is made to this repository (currently hosted in GitLab), I have created the following .gitlab-ci.yml file:

before_script:
 - sudo apt-get -qq update && sudo apt-get -qq install -y python3
 - sudo apt-get -qq update
 - sudo apt-get -qq install -y python3 python-virtualenv python3-pip 
 - virtualenv venv
 - . venv/bin/activate
 - sudo apt-get install python3-pyqt5 -y
 - sudo apt-get install python3-pyqt5.qtmultimedia -y
 - cd test

stages: 
    - test

job1:
    stage: test
    script: python3 -m unittest -v test.GuiTest

Which does run (so the runners should have been set up correctly) but I am getting the following error when executing the script of job 1:

$ python3 -m unittest -v test QXcbConnection: Could not connect to display bash: line 62: 50549 Aborted (core dumped) python3 -m unittest -v test ERROR: Job failed: exit status 1

From the research I did, it seems that the CI server is facing problems trying to run a graphical application. However, for running the unit tests, no any actual window needs to be opened. The problem seems to be this particular line of the test (.py) file:

application = QApplication(sys.argv)

Is there any way in which I can bypass this issue? I do understand that if the testing functions required any graphical feature (e.g. pressing a button) this would be a problem but in this case, there is no such need.

Thanks a lot.

EDIT: Could you please have a look at this question since it was probably posted on an incorrect timing.


回答1:


You can try setting the backend used by Qt to "offscreen" by setting the env var QT_QPA_PLATFORM in your .yml file.

job1:
    stage: test
    variables:
      QT_QPA_PLATFORM: "offscreen"
    script: python3 -m unittest -v test.GuiTest


来源:https://stackoverflow.com/questions/49184586/automating-unit-testing-in-pyqt5-using-gitlabs-ci-qxcbconnection-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!