LoginSignup
2
0

More than 3 years have passed since last update.

Python+TkinterをGithubActions上でunittestする場合にno display nameが発生したときの対応方法メモ

Last updated at Posted at 2020-12-28

Python+TkinterをGithubActions上でunittestする場合、ディスプレイ設定が必要

Pythonで開発したコードリポジトリをgit pushしたら、GithubActions上で自動でunittestが走るようにしています。

Tkinterなど画面描画を必要とするmoduleのテストをGitActions上で実施する場合、仮想環境でのディスプレイ設定をする必要があります。この設定をしていないと、no display name とエラーが吐き出されます。

以下、エラーを回避するための対応方法メモとなります。
unittestを走らせる前に下記内容でディスプレイ設定します。

github\workflows\test.yml
disp=:99
screen=0
geom=640x480x24
exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &
export DISPLAY=:99

GithubActions用のymlファイルはこんな感じになります。
ymlファイルは.github\workflows内に入れておく決まりになっています。

github\workflows\test.yml
name: Python package

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7]

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    # インストール
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    # pythonバージョンの表示
    - name: Display Python version
      run: python -c "import sys; print(sys.version)"
    # unittestの実行
    - name: Test
      run: |
        disp=:99
        screen=0
        geom=640x480x24
        exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &
        export DISPLAY=:99
        python -m unittest discover tests

参考資料

StackOverflowのこちらの記事を参考にしました。
_tkinter.TclError: no display name and no $DISPLAY environment variable

その他GithubActionsでのTips

GithubActionsでのunittestをskipしたい場合

test.py
@unittest.skipIf("GITHUB_ACTIONS" in os.environ and os.environ["GITHUB_ACTIONS"] == "true", "Skipping this test on Github Actions")
class TestHoge(unittest.TestCase):
    def setUp(self):
        print("setUp")

    def test_hoge(self):
        print("test_hoge")
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0