Skip to content

test: enhance testing logging #4097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/4097.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test: enhance testing logging
32 changes: 19 additions & 13 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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...")
Expand Down
17 changes: 12 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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__))
Expand Down
Loading