After git pushing the code repository developed in Python, unittest is automatically run on Github Actions.
When testing a module that requires screen drawing such as Tkinter on GitActions, it is necessary to set the display in the virtual environment. If this setting is not made, an error will be spit out as no display name.
The following is a memo of how to deal with the error. Before running the unit test, set the display with the following contents.
.github\workflows\test.yml
disp=:99
screen=0
geom=640x480x24
exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &
export DISPLAY=:99
The yml file for Github Actions looks like this. It is a rule to keep the yml file in .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 }}
#Installation
- 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
#View python version
- name: Display Python version
run: python -c "import sys; print(sys.version)"
#Run 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
I referred to this article on StackOverflow. _tkinter.TclError: no display name and no $DISPLAY environment variable
If you want to skip unittests in Github Actions
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")