diff --git a/doc/changelog.d/4097.miscellaneous.md b/doc/changelog.d/4097.miscellaneous.md new file mode 100644 index 0000000000..86ca5c524a --- /dev/null +++ b/doc/changelog.d/4097.miscellaneous.md @@ -0,0 +1 @@ +Test: enhance testing logging \ No newline at end of file diff --git a/tests/common.py b/tests/common.py index 91deae0e99..4866534bbb 100644 --- a/tests/common.py +++ b/tests/common.py @@ -169,6 +169,10 @@ def debug_testing() -> bool: return False +def testing_dpf_backend() -> bool: + return os.environ.get("TEST_DPF_BACKEND", "NO").upper().strip() in ["YES", "TRUE"] + + def is_float(s: str) -> bool: try: float(s) @@ -220,16 +224,8 @@ def get_details_of_elements(mapdl_) -> Dict[int, Node]: return elements -def log_test_start(mapdl: Mapdl) -> None: +def log_start_test(mapdl: Mapdl, test_name: str) -> None: """Print the current test to the MAPDL log file and console output.""" - test_name = os.environ.get( - "PYTEST_CURRENT_TEST", "**test id could not get retrieved.**" - ) - - mapdl.run("!") - mapdl.run(f"! PyMAPDL running test: {test_name}"[:639]) - mapdl.run("!") - # To see it also in MAPDL terminal output if len(test_name) > 75: # terminal output is limited to 75 characters @@ -239,13 +235,19 @@ def log_test_start(mapdl: Mapdl) -> None: else: types_ = ["File path", "Test function"] - mapdl._run("/com,Running test in:", mute=True) + mapdl.com("Running test in:", mute=True) for type_, name_ in zip(types_, test_name_): - mapdl._run(f"/com, {type_}: {name_}", mute=True) + mapdl.com(f" {type_}: {name_}", mute=True) else: - mapdl._run(f"/com,Running test: {test_name}", mute=True) + mapdl.com(f"Running test: {test_name}", mute=True) + + +def log_end_test(mapdl: Mapdl, test_name: str) -> None: + mapdl.com("!", mute=True) + mapdl.com(f"! End of test: {test_name.split('::')[1]}"[:639], mute=True) + mapdl.com("!", mute=True) def restart_mapdl(mapdl: Mapdl, test_name: str = "") -> Mapdl: @@ -265,9 +267,11 @@ def is_exited(mapdl: Mapdl): if ON_LOCAL: # First we try to reconnect try: + LOG.debug("Reconnecting to MAPDL...") mapdl.reconnect_to_mapdl(timeout=5) assert mapdl.finish() + LOG.debug("Reconnected to MAPDL successfully.") return mapdl except MapdlConnectionError as e: @@ -277,9 +281,11 @@ def is_exited(mapdl: Mapdl): # Killing the instance (just in case) try: + + LOG.debug("Exiting MAPDL...") mapdl.exit(force=True) except Exception as e: - pass + LOG.warning(f"Failed to exit MAPDL: {str(e)}") # Relaunching MAPDL LOG.debug("Relaunching MAPDL...") diff --git a/tests/conftest.py b/tests/conftest.py index 9897d41f71..fee828b785 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -46,10 +46,12 @@ is_on_ubuntu, is_running_on_student, is_smp, - log_test_start, + log_end_test, + log_start_test, make_sure_not_instances_are_left_open, restart_mapdl, support_plotting, + testing_dpf_backend, testing_minimal, ) @@ -60,6 +62,7 @@ # DEBUG_TESTING = debug_testing() TESTING_MINIMAL = testing_minimal() +TEST_DPF_BACKEND = testing_dpf_backend() ON_LOCAL = is_on_local() ON_CI = is_on_ci() @@ -395,9 +398,10 @@ def get_xpassed_message(rep: CollectReport): def get_error_message(rep: CollectReport): if hasattr(rep.longrepr, "reprcrash"): message = str(rep.longrepr.reprcrash.message) - else: - # Error string + elif hasattr(rep.longrepr, "errorstring"): message = str(rep.longrepr.errorstring) + else: + raise Exception(str(rep.longrepr)) header = markup("[ERROR]", **ERROR_COLOR) return get_failure_message(rep, header, message) @@ -564,7 +568,7 @@ def run_before_and_after_tests( # Write test info to log_apdl if DEBUG_TESTING: - log_test_start(mapdl) + log_start_test(mapdl, test_name) # check if the local/remote state has changed or not prev = mapdl.is_local @@ -573,6 +577,9 @@ def run_before_and_after_tests( yield # this is where the testing happens + if DEBUG_TESTING: + log_end_test(mapdl, test_name) + mapdl.prep7() # Check resetting state @@ -717,7 +724,7 @@ def mapdl(request, tmpdir_factory): cleanup_on_exit=cleanup, license_server_check=False, start_timeout=50, - loglevel="ERROR", # Because Pytest captures all output + loglevel="DEBUG", # Because Pytest captures all output # If the following file names are changed, update `ci.yml`. log_apdl="pymapdl.apdl" if DEBUG_TESTING else None, mapdl_output="apdl.out" if (DEBUG_TESTING and ON_LOCAL) else None, diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index e33e2c0dbf..3c55ce4889 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -39,12 +39,17 @@ import pytest from conftest import ( + IS_SMP, + ON_CI, + ON_LOCAL, PATCH_MAPDL, PATCH_MAPDL_START, + QUICK_LAUNCH_SWITCHES, VALID_PORTS, NullContext, Running_test, has_dependency, + requires, ) if has_dependency("pyvista"): @@ -68,7 +73,6 @@ from ansys.mapdl.core.mapdl_grpc import SESSION_ID_NAME from ansys.mapdl.core.misc import random_string, stack from ansys.mapdl.core.plotting import GraphicsBackend -from conftest import IS_SMP, ON_CI, ON_LOCAL, QUICK_LAUNCH_SWITCHES, requires # Path to files needed for examples PATH = os.path.dirname(os.path.abspath(__file__))