Skip to content

Commit

Permalink
test: add option for store output
Browse files Browse the repository at this point in the history
  • Loading branch information
michiboo committed Jul 23, 2023
1 parent c47e6d2 commit 38eaaa2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
18 changes: 18 additions & 0 deletions python/tests/test_framework/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

# conftest.py
def pytest_addoption(parser):
parser.addoption(
"--store_output",
action="store",
default=False,
help="Store output file for tests.",
)


def pytest_configure(config):
_store_output = config.getoption("--store_output")
# Store the custom_arg_value in a global variable or a custom configuration object.
# For example, you can store it in a global variable like this:
global STORE_OUTPUT
STORE_OUTPUT = _store_output
18 changes: 17 additions & 1 deletion python/tests/test_framework/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
get_function_object,
evaluate_expression,
variable_mapping,
copy_file,
)
from conftest import STORE_OUTPUT

CURR_DICT = os.path.dirname(os.path.abspath(__file__))
TEST_CONFIG_FILES_NAME = os.listdir(os.path.join(CURR_DICT, "test_configs"))
Expand Down Expand Up @@ -57,6 +59,21 @@ def core_test(files, grid, outputs, job_assert, job_assert_args):
GENERATED_TEST_CASES.append(
[lambda: job_assert(j, **job_assert_args), test_name]
)
if STORE_OUTPUT:
if not os.path.exists(CURR_DICT + "/output"):
os.mkdir(CURR_DICT + "/output")
if not os.path.exists(CURR_DICT + "/output/" + test_name):
os.mkdir(CURR_DICT + "/output/" + test_name)
fileName = str(list(j.outputs.values())[0][0]).split("/")[-1]
for key, value in list(j.outputs.items()):
copy_file(
value[0],
CURR_DICT + "/output/" + test_name + "/" + f"{key}_" + fileName,
)
copy_file(
os.path.join(j.cache.path, "cacheNone/" + fileName),
CURR_DICT + "/output/" + test_name + "/" + fileName,
)


def get_options(grids, expression):
Expand All @@ -73,7 +90,6 @@ def init_all(test_descriptions):
if type(tests) is not list:
tests = [tests]
for test_description in tests:
print(test_description)
options = get_options(
test_description["grids"], test_description["grids_expression"]
)
Expand Down
13 changes: 13 additions & 0 deletions python/tests/test_framework/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import itertools
import inspect
import shutil

# Get the current directory
current_dir = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -65,3 +66,15 @@ def generate_string_combinations(*lists):
combinations = list(itertools.product(*lists))
combinations = ["".join(combination) for combination in combinations]
return combinations


def copy_file(source_file, destination_file):
try:
shutil.copy(source_file, destination_file)
print(f"File copied successfully from '{source_file}' to '{destination_file}'.")
except FileNotFoundError:
print(f"Source file '{source_file}' not found.")
except PermissionError:
print(
f"Permission denied. Unable to copy '{source_file}' to '{destination_file}'."
)

0 comments on commit 38eaaa2

Please sign in to comment.