From 916fca2c9ca5161917733ec30582f08b11584a5c Mon Sep 17 00:00:00 2001
From: Mohammad Torkashvand <mohammad.torkashvand@geant.org>
Date: Sat, 19 Oct 2024 11:07:38 +0200
Subject: [PATCH] Add pytest-xdist for parallel test execution

---
 .gitlab-ci.yml   |  2 ++
 requirements.txt |  1 +
 test/conftest.py | 10 +++++++---
 tox.ini          |  9 +--------
 4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 0ed900004..46b472054 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -25,6 +25,7 @@ run-tox-pipeline:
     POSTGRES_PASSWORD: nwa
     DATABASE_URI_TEST: 'postgresql://nwa:nwa@postgres:5432/gso-test-db'
     PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
+    POSTGRES_INITDB_ARGS: "--username=nwa --pwfile=/tmp/pwfile --auth=trust"
 
   cache:
     key: tox-virtualenv
@@ -33,6 +34,7 @@ run-tox-pipeline:
       - venv/
 
   before_script:
+    - echo "$POSTGRES_PASSWORD" > /tmp/pwfile
     - pip install virtualenv
     - virtualenv venv
     - . venv/bin/activate
diff --git a/requirements.txt b/requirements.txt
index 621780cce..52ed7721d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -23,3 +23,4 @@ sphinx-rtd-theme==2.0.0
 urllib3_mock==0.3.3
 pytest-asyncio==0.23.6
 pre-commit~=3.7.0
+pytest-xdist==3.6.1
\ No newline at end of file
diff --git a/test/conftest.py b/test/conftest.py
index 2eb7e6a28..8646c13ea 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -142,8 +142,12 @@ def data_config_filename() -> str:
 
 @pytest.fixture(scope="session")
 def db_uri():
-    """Provide the database uri configuration to run the migration on."""
-    return os.environ.get("DATABASE_URI_TEST", "postgresql://nwa:nwa@localhost/gso-test-db")
+    """Provide a unique database URI for each pytest-xdist worker, or a default URI if running without xdist."""
+    worker_id = os.getenv("PYTEST_XDIST_WORKER")
+    if worker_id:
+        return f"postgresql://nwa:nwa@localhost/gso-test-db_{worker_id}"
+
+    return "postgresql://nwa:nwa@localhost/gso-test-db"
 
 
 def run_migrations(db_uri: str) -> None:
@@ -281,7 +285,7 @@ def partner_factory():
     return _create_partner
 
 
-@pytest.fixture(scope="session")
+@pytest.fixture()
 def geant_partner(partner_factory):
     return partner_factory(name="GEANT-TEST", email="goat-test@geant.org")
 
diff --git a/tox.ini b/tox.ini
index a8eab7b2f..222a360fa 100644
--- a/tox.ini
+++ b/tox.ini
@@ -17,14 +17,7 @@ deps =
     -r requirements.txt
 
 commands =
-    ruff check --respect-gitignore --preview .
-    ruff format --respect-gitignore --preview --check .
-    mypy .
-    coverage erase
-    coverage run --source gso --omit="gso/migrations/*" -m pytest {posargs}
-    coverage xml
-    coverage html
-    sh -c "if [ $SKIP_ALL_TESTS -eq 1 ]; then echo 'Skipping coverage report'; else coverage report --fail-under 80; fi"
+    pytest -n auto {posargs}
 
 allowlist_externals =
     sh
-- 
GitLab