Skip to content

Commit

Permalink
Add test directory creation for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kohei-noda-qcrg committed Jan 15, 2024
1 parent 6bac038 commit 559edb8
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions tools/dcaspt2_test_generator
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ def main():
selected_test_type = check_test_type()
# mkdir the test directory (the name of the directory is the name of the reference output file without the extension)
test_directory_name = ask_test_directory_name()
dir_path = Path.joinpath(test_dir, selected_test_type.dir, test_directory_name).resolve()
if test_type == "Unit test":
dir_path = Path.joinpath(test_dir, "unit_test", test_directory_name).resolve()
else:
dir_path = Path.joinpath(test_dir, selected_test_type.dir, test_directory_name).resolve()
if dir_path.is_dir():
logging.error(f"The test directory {dir_path} already exists. Exiting...")
raise FileExistsError
Expand Down Expand Up @@ -272,8 +275,47 @@ def main():
logging.info(f"dirac_data directory: {dirac_files.data_directory}")
logging.info("Before you commit a new test, please run the test to make sure it passes.")

elif test_type == "Unit test":
# Reference output file
ref_output_path = ask_general_file_path(program_name="unit test", file_type="reference output", default_path=user_dir)
dir_path.mkdir(parents=True, exist_ok=True)
shutil.copy(ref_output_path, Path.joinpath(dir_path, "expected"))

# Python file
template_python_file_path = Path.joinpath(test_template_dir, "unit_test", "test_template_unit_test.py")
python_file_path = Path.joinpath(dir_path, f"test_{test_directory_name}.py")
replace_dict = {
"def replace_test_template": f"def test_{test_directory_name}",
"# @replace_marker": selected_test_type.marker,
"replace_test_exe": f"{test_directory_name}_test_exe",
}
replace_template_file(template_python_file_path, python_file_path, replace_dict)

# Fortran file
template_fortran_file_path = Path.joinpath(test_template_dir, "unit_test", "test_template_unit_test.f90")
fortran_file_path = Path.joinpath(dir_path, f"test_{test_directory_name}.f90")
shutil.copy(template_fortran_file_path, fortran_file_path)

# CMakeLists.txt
template_cmakelists_file_path = Path.joinpath(test_template_dir, "unit_test", "CMakeLists.txt")
cmakelists_file_path = Path.joinpath(dir_path, "CMakeLists.txt")
replace_dict = {
"replace": test_directory_name,
}
replace_template_file(template_cmakelists_file_path, cmakelists_file_path, replace_dict)

logging.info("Successfully generated the test template!")
logging.info("================== NOTE ==================")
logging.info("Then follow these steps to replace the automatically created test with the process you want to test")
logging.info(f"1. Edit {fortran_file_path} to add the process you wish to test and write the results to result.out.")
logging.info(f"2. Edit {python_file_path} to rewrite the process after run_test according to the type of data you want to test.\
\n\tIn the automatically created file, the process takes a single line of string as a string and checks if it matches.\
\n\tIn the {test_dir}/module_testing.py file, there are functions that can be used to convert the output file to desired data type.")
logging.info("That's it! Now you can run the test!")
logging.info("==========================================")
logging.info(f"test directory: {dir_path}")
logging.info("Before you commit a new test, please run the test to make sure it passes.")
else:
# Currently only CASPT2 energy test is supported
logging.error("Sorry, this test type is not supported yet.")


Expand Down

0 comments on commit 559edb8

Please sign in to comment.