Преглед изворни кода

pytest: Add very basic config to run pytest (#2183)

This adds pytest configuration and one pytest-ready test file so that pytest can run on the source code without errors.

Notably, this does not use or run any of the existing tests and searches for tests in directories called tests (instead of testsuite).

There is no handling of GRASS session or any test data. Each test needs to set up the session by itself.

However, given that the test assume import grass will work, the command 'pytest .' will work without errors only when Python path (PYTHONPATH) is set beforehand. (The CI is using 'grass --config ...' to get it.)

The idea is to allow for writing of tests in a different style with pytest and later add functionality for easier setup or data comparison.
Vaclav Petras пре 3 година
родитељ
комит
924db551b5

+ 70 - 0
.github/workflows/pytest.yml

@@ -0,0 +1,70 @@
+name: pytest
+
+on:
+  - push
+  - pull_request
+
+jobs:
+  pytest:
+    strategy:
+      matrix:
+        os:
+          - ubuntu-20.04
+        python-version:
+          - "3.8"
+          - "3.10"
+      fail-fast: true
+
+    runs-on: ${{ matrix.os }}
+
+    steps:
+      - uses: actions/checkout@v2
+
+      - name: Set up Python
+        uses: actions/setup-python@v2
+        with:
+          python-version: ${{ matrix.python-version }}
+
+      - name: Install non-Python dependencies
+        run: |
+          sudo apt-get update -y
+          sudo apt-get install -y wget git gawk findutils
+          xargs -a <(awk '! /^ *(#|$)/' ".github/workflows/apt.txt") -r -- \
+              sudo apt-get install -y --no-install-recommends --no-install-suggests
+
+      - name: Install Python dependencies
+        run: |
+          python -m pip install --upgrade pip
+          pip install -r .github/workflows/python_requirements.txt
+          pip install pytest
+
+      - name: Create installation directory
+        run: |
+          mkdir $HOME/install
+
+      - name: Set number of cores for compilation
+        run: |
+          echo "MAKEFLAGS=-j$(nproc)" >> $GITHUB_ENV
+
+      - name: Set LD_LIBRARY_PATH for compilation
+        run: |
+          echo "LD_LIBRARY_PATH=$HOME/install/lib" >> $GITHUB_ENV
+
+      - name: Build
+        run: .github/workflows/build_${{ matrix.os }}.sh $HOME/install
+
+      - name: Add the bin directory to PATH
+        run: |
+          echo "$HOME/install/bin" >> $GITHUB_PATH
+
+      - name: Test executing of the grass command
+        run: .github/workflows/test_simple.sh
+
+      - name: Run pytest
+        run: |
+          export PYTHONPATH=`grass --config python_path`:$PYTHONPATH
+          pytest .
+
+      - name: Print installed versions
+        if: always()
+        run: .github/workflows/print_versions.sh

+ 37 - 0
.github/workflows/python_apt.txt

@@ -0,0 +1,37 @@
+autoconf2.13
+autotools-dev
+bison
+flex
+g++
+gettext
+git
+imagemagick
+libblas-dev
+libbz2-dev
+libcairo2-dev
+libfftw3-dev
+libfreetype6-dev
+libgdal-dev
+libgeos-dev
+libglu1-mesa-dev
+libjpeg-dev
+liblapack-dev
+liblas-c-dev
+libncurses5-dev
+libnetcdf-dev
+libpng-dev
+libpq-dev
+libproj-dev
+libreadline-dev
+libsqlite3-dev
+libtiff-dev
+libxmu-dev
+libzstd-dev
+make
+netcdf-bin
+p7zip
+proj-bin
+sqlite3
+unixodbc-dev
+xvfb
+zlib1g-dev

+ 5 - 0
.github/workflows/python_requirements.txt

@@ -0,0 +1,5 @@
+matplotlib
+numpy
+Pillow
+ply
+PyVirtualDisplay

+ 12 - 0
pyproject.toml

@@ -26,3 +26,15 @@ exclude = '''
     | python/grass/pygrass/utils.py
 )
 '''
+
+[tool.pytest.ini_options]
+minversion = "6.0"
+python_files = "*/tests/*_test.py"
+addopts = """
+    --ignore-glob='dist.*'
+    --ignore-glob='bin.*'
+    --ignore-glob='*/tests/data/*'
+    --ignore-glob='*/grass/pygrass/tests/*'
+    --doctest-glob='*doctest*.txt'
+    --ignore='raster/r.category/test_rcategory_doctest.txt'
+"""

+ 23 - 0
python/grass/script/tests/utils_test.py

@@ -0,0 +1,23 @@
+"""Test functions in grass.script.utils"""
+
+import grass.script as gs
+
+
+def test_named_separators():
+    """Check that named separtors are recognized and correctly evaluated"""
+    assert gs.separator("pipe") == "|"
+    assert gs.separator("comma") == ","
+    assert gs.separator("space") == " "
+    assert gs.separator("tab") == "\t"
+    assert gs.separator("newline") == "\n"
+
+
+def test_backslash_separators():
+    """Check that separtors specified as an escape sequence are correctly evaluated"""
+    assert gs.separator(r"\t") == "\t"
+    assert gs.separator(r"\n") == "\n"
+
+
+def test_unrecognized_separator():
+    """Check that unknown strings are just passed through"""
+    assert gs.separator("apple") == "apple"