diff --git a/python/grass/CMakeLists.txt b/python/grass/CMakeLists.txt index b089cb76160..eb2fb2a1064 100644 --- a/python/grass/CMakeLists.txt +++ b/python/grass/CMakeLists.txt @@ -18,6 +18,7 @@ set(PYDIRS pygrass/shell pygrass/tests pygrass/vector + pytest script semantic_label temporal diff --git a/python/grass/Makefile b/python/grass/Makefile index cc04b04b496..97b42afe7bc 100644 --- a/python/grass/Makefile +++ b/python/grass/Makefile @@ -16,6 +16,7 @@ SUBDIRS = \ jupyter \ pydispatch \ pygrass \ + pytest \ script \ semantic_label \ temporal \ diff --git a/python/grass/pytest/Makefile b/python/grass/pytest/Makefile new file mode 100644 index 00000000000..bc8fa1a7008 --- /dev/null +++ b/python/grass/pytest/Makefile @@ -0,0 +1,19 @@ +MODULE_TOPDIR = ../../.. + +include $(MODULE_TOPDIR)/include/Make/Other.make +include $(MODULE_TOPDIR)/include/Make/Python.make + +DSTDIR = $(ETC)/python/grass/pytest + +MODULES = assertions + +PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__) +PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__) + +default: $(PYFILES) $(PYCFILES) + +$(DSTDIR): + $(MKDIR) $@ + +$(DSTDIR)/%: % | $(DSTDIR) + $(INSTALL_DATA) $< $@ diff --git a/python/grass/pytest/__init__.py b/python/grass/pytest/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/python/grass/pytest/assertions.py b/python/grass/pytest/assertions.py new file mode 100644 index 00000000000..4171a22553e --- /dev/null +++ b/python/grass/pytest/assertions.py @@ -0,0 +1,7 @@ +import grass.script as gs + + +def rasterExists(map_name, env=None): + """Check if a raster map exists in the given mapset.""" + result = gs.find_file(map_name, element="cell", env=env) + return bool(result["name"]) diff --git a/python/grass/pytest/tests/conftest.py b/python/grass/pytest/tests/conftest.py new file mode 100644 index 00000000000..afdff253e3a --- /dev/null +++ b/python/grass/pytest/tests/conftest.py @@ -0,0 +1,12 @@ +import os +import pytest +import grass.script as gs + + +@pytest.fixture +def session(tmp_path): + project = tmp_path / "test_proj" + gs.create_project(project) + + with gs.setup.init(project, env=os.environ.copy()) as session: + yield session.env diff --git a/python/grass/pytest/tests/test_pytest_assertions.py b/python/grass/pytest/tests/test_pytest_assertions.py new file mode 100644 index 00000000000..23f3172305c --- /dev/null +++ b/python/grass/pytest/tests/test_pytest_assertions.py @@ -0,0 +1,9 @@ +import grass.script as gs +from grass.pytest.assertions import rasterExists + + +def test_rasterExists(session): + gs.run_command("r.mapcalc", expression="test_map = 1", env=session) + + assert rasterExists("test_map", env=session) + assert not rasterExists("missing_map", env=session)