Skip to content

Commit

Permalink
removing fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
dipinknair committed Nov 13, 2024
1 parent d795efd commit 6fd1ce9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
17 changes: 10 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
# Check if Mechanical is installed
# NOTE: checks in this order to get the newest installed version

# Ignore functions starts with `test` from scripts folder
collect_ignore = ["scripts"]

valid_rver = [str(each) for each in SUPPORTED_MECHANICAL_VERSIONS]

Expand Down Expand Up @@ -201,14 +203,15 @@ def rootdir():
yield base.parent


@pytest.fixture()
def pass_expected(pytestconfig):
"""Checks for conditions to see if scripts run in subprocess are expected to pass or not."""
# @pytest.fixture()
# def subprocess_pass_expected(pytestconfig):
# """Checks for conditions to see if scripts run in subprocess are expected to pass or not."""

version = pytestconfig.getoption("ansys_version")
if os.name != "nt" and int(version) < 251:
yield False
yield True
# version = pytestconfig.getoption("ansys_version")
# if os.name != "nt" and int(version) < 251:
# yield False
# else:
# yield True


@pytest.fixture()
Expand Down
16 changes: 9 additions & 7 deletions tests/embedding/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@


def _run_background_app_test(
run_subprocess, rootdir: str, pytestconfig, testname: str, pass_expected: bool
run_subprocess, rootdir: str, pytestconfig, testname: str, pass_expected: bool = True
) -> typing.Tuple[bytes, bytes]:
"""Run the process and return stdout and stderr after it finishes."""

version = pytestconfig.getoption("ansys_version")
script = os.path.join(rootdir, "tests", "scripts", "background_app_test.py")

subprocess_pass_expected = pass_expected

# TODO: revert below condition once bug #975 is fixed
if testname == "multiple_instances" and os.name != "nt":
subprocess_pass_expected = False
if pass_expected and os.name != "nt":
if int(version) < 251 or testname == "multiple_instances":
subprocess_pass_expected = False

process, stdout, stderr = run_subprocess(
[sys.executable, script, version, testname], None, subprocess_pass_expected
Expand All @@ -56,10 +55,11 @@ def _run_background_app_test(


@pytest.mark.embedding_scripts
def test_background_app_multiple_instances(rootdir, run_subprocess, pytestconfig, pass_expected):
@pytest.mark.embedding_backgroundapp
def test_background_app_multiple_instances(rootdir, run_subprocess, pytestconfig):
"""Multiple instances of background app can be used."""
stderr = _run_background_app_test(
run_subprocess, rootdir, pytestconfig, "multiple_instances", pass_expected
run_subprocess, rootdir, pytestconfig, "multiple_instances", True
)
assert "Project 1" in stderr
assert "Project 2" in stderr
Expand All @@ -68,6 +68,7 @@ def test_background_app_multiple_instances(rootdir, run_subprocess, pytestconfig


@pytest.mark.embedding_scripts
@pytest.mark.embedding_backgroundapp
def test_background_app_use_stopped(rootdir, run_subprocess, pytestconfig):
"""Multiple instances of background app cannot be used after an instance is stopped."""
stderr = _run_background_app_test(
Expand All @@ -77,6 +78,7 @@ def test_background_app_use_stopped(rootdir, run_subprocess, pytestconfig):


@pytest.mark.embedding_scripts
@pytest.mark.embedding_backgroundapp
def test_background_app_initialize_stopped(rootdir, run_subprocess, pytestconfig):
"""Multiple instances of background app cannot be used after an instance is stopped."""
stderr = _run_background_app_test(
Expand Down
53 changes: 29 additions & 24 deletions tests/embedding/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,29 @@ def _unset_var(env, var) -> None:


def _run_embedding_log_test(
run_subprocess, rootdir: str, pytestconfig, testname: str, pass_expected: bool
run_subprocess,
rootdir: str,
pytestconfig,
testname: str,
pass_expected: bool = True,
) -> typing.Tuple[bytes, bytes]:
"""Runs the process and returns it after it finishes"""
version = pytestconfig.getoption("ansys_version")

embedded_py = os.path.join(rootdir, "tests", "scripts", "embedding_log_test.py")

subprocess_pass_expected = pass_expected
if pass_expected == True and os.name != "nt" and int(version) < 251:
subprocess_pass_expected = False

process, stdout, stderr = run_subprocess(
[sys.executable, embedded_py, version, testname],
_get_env_without_logging_variables(),
pass_expected,
subprocess_pass_expected,
)

stdout = stdout.decode()
if not subprocess_pass_expected:
stdout = stdout.decode()
_assert_success(stdout, pass_expected)
stderr = stderr.decode()
_assert_success(stdout, pass_expected)
return stderr


Expand All @@ -80,6 +88,7 @@ def _assert_success(stdout: str, pass_expected: bool) -> int:


@pytest.mark.embedding_scripts
@pytest.mark.embedding_logging
def test_logging_write_log_before_init(rootdir, run_subprocess, pytestconfig):
"""Test that an error is thrown when trying to log before initializing"""
stderr = _run_embedding_log_test(
Expand All @@ -89,65 +98,61 @@ def test_logging_write_log_before_init(rootdir, run_subprocess, pytestconfig):


@pytest.mark.embedding_scripts
@pytest.mark.embedding_logging
def test_logging_write_info_after_initialize_with_error_level(
rootdir, run_subprocess, pytestconfig, pass_expected
rootdir, run_subprocess, pytestconfig
):
"""Test that no output is written when an info is logged when configured at the error level."""
stderr = _run_embedding_log_test(
run_subprocess,
rootdir,
pytestconfig,
"log_info_after_initialize_with_error_level",
pass_expected,
)
assert "0xdeadbeef" not in stderr


@pytest.mark.parametrize("addin_configuration", ["Mechanical", "WorkBench"])
@pytest.mark.embedding_scripts
@pytest.mark.embedding_logging
@pytest.mark.minimum_version(241)
def test_addin_configuration(
rootdir, run_subprocess, pytestconfig, addin_configuration, pass_expected
):
def test_addin_configuration(rootdir, run_subprocess, pytestconfig, addin_configuration):
"""Test that mechanical can start with both the Mechanical and WorkBench configuration."""
stderr = _run_embedding_log_test(
run_subprocess,
rootdir,
pytestconfig,
f"log_configuration_{addin_configuration}",
pass_expected,
)
assert f"{addin_configuration} configuration!" in stderr


@pytest.mark.embedding_scripts
@pytest.mark.embedding_logging
def test_logging_write_error_after_initialize_with_info_level(
rootdir, run_subprocess, pytestconfig, pass_expected
rootdir, run_subprocess, pytestconfig
):
"""Test that output is written when an error is logged when configured at the info level."""
stderr = _run_embedding_log_test(
run_subprocess,
rootdir,
pytestconfig,
"log_error_after_initialize_with_info_level",
pass_expected,
run_subprocess, rootdir, pytestconfig, "log_error_after_initialize_with_info_level"
)
assert "Will no one rid me of this turbulent priest?" in stderr


@pytest.mark.embedding_scripts
def test_logging_level_before_and_after_initialization(
rootdir, run_subprocess, pytestconfig, pass_expected
):
@pytest.mark.embedding_logging
def test_logging_level_before_and_after_initialization(rootdir, run_subprocess, pytestconfig):
"""Test logging level API before and after initialization."""
stderr = _run_embedding_log_test(
run_subprocess, rootdir, pytestconfig, "log_check_can_log_message", pass_expected
run_subprocess, rootdir, pytestconfig, "log_check_can_log_message"
)


@pytest.mark.embedding_scripts
def test_logging_all_level(rootdir, run_subprocess, pytestconfig, pass_expected):
@pytest.mark.embedding_logging
def test_logging_all_level(rootdir, run_subprocess, pytestconfig):
"""Test all logging level after initialization."""
stderr = _run_embedding_log_test(
run_subprocess, rootdir, pytestconfig, "log_check_all_log_level", pass_expected
run_subprocess, rootdir, pytestconfig, "log_check_all_log_level"
)
assert all(keyword in stderr for keyword in ["debug", "warning", "info", "error", "fatal"])

0 comments on commit 6fd1ce9

Please sign in to comment.