From 504b9e8eb6970503043b150e7c7c08e6453c0d89 Mon Sep 17 00:00:00 2001
From: Kevin Kuns <kevin.kuns@ligo.org>
Date: Fri, 25 Mar 2022 11:17:56 -0400
Subject: [PATCH] add a pytest marker to specify tests that generate test data

---
 .gitignore  |  3 ++-
 conftest.py | 29 +++++++++++++++++++++++++++++
 pytest.ini  |  1 +
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index f2d219a4..b13ff3fc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,9 @@
 #for docs and setup.py outputs
 build/
-tresults/
+tresults*/
 # test cache
 gwinc/test/cache
+test/*/*.h5
 
 # Byte-compiled / optimized / DLL files
 __pycache__/
diff --git a/conftest.py b/conftest.py
index 546d9707..8e838280 100644
--- a/conftest.py
+++ b/conftest.py
@@ -40,6 +40,14 @@ def pytest_addoption(parser):
         help="Do not preclear tpaths",
     )
 
+    parser.addoption(
+        "--generate",
+        action="store_true",
+        default=False,
+        dest="generate",
+        help="Generate test data",
+    )
+
 
 @pytest.fixture
 def plot(request):
@@ -270,3 +278,24 @@ def relfile_test(_file_, request, pre = None, post = None, fname = None):
     else:
         return relfile(_file_, testname, fname = fname)
 
+
+def pytest_collection_modifyitems(config, items):
+    """
+    Modifies tests to be selectively skipped with command line options
+
+    https://docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option
+    """
+    # run tests marked as generate if and only if --generate is given
+    # skip all others in this case
+    if config.getoption('--generate'):
+        skip = pytest.mark.skip(
+            reason='only running tests that generate data')
+        for item in items:
+            if 'generate' not in item.keywords:
+                item.add_marker(skip)
+    else:
+        skip = pytest.mark.skip(
+            reason='generates test data: needs --generate to run')
+        for item in items:
+            if 'generate' in item.keywords:
+                item.add_marker(skip)
diff --git a/pytest.ini b/pytest.ini
index e5feb650..4818fd89 100644
--- a/pytest.ini
+++ b/pytest.ini
@@ -9,6 +9,7 @@ markers =
     fast: marks tests as being fast, usually it does not make plots
     logic: is a test of the code logic
     noise: is a test of the noise curves
+    generate: marks a function that generates test data
 
 [pytest-watch]
 ignore = .* 
-- 
GitLab