Skip to content

Commit 831acbe

Browse files
author
d.kovalenko
committed
[test] sync with standard (common) code
1 parent 95f16ac commit 831acbe

File tree

3 files changed

+78
-33
lines changed

3 files changed

+78
-33
lines changed

tests/TestServices.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ def MakeRootTmpDirForGlobalResources(globalResourceID: str) -> str:
3232
# --------------------------------------------------------------------
3333
def GetCurTestTmpDir(request: pytest.FixtureRequest) -> str:
3434
assert isinstance(request, pytest.FixtureRequest)
35-
return __class__.Helper__GetCurTestTmpDir(request)
35+
return __class__.Helper__GetCurTestTmpDir(request.node)
3636

3737
# --------------------------------------------------------------------
38-
def Helper__GetCurTestTmpDir(request: pytest.FixtureRequest) -> str:
39-
assert isinstance(request, pytest.FixtureRequest)
38+
def Helper__GetCurTestTmpDir(function: pytest.Function) -> str:
39+
assert isinstance(function, pytest.Function)
4040

4141
rootDir = TestServices.GetRootDir()
4242
rootTmpDir = TestServices.GetRootTmpDir()
4343

4444
# [2024-12-18] It is not a fact now.
4545
# assert rootTmpDir.startswith(rootDir)
4646

47-
testPath = str(request.path)
47+
testPath = str(function.path)
4848

4949
if not testPath.startswith(rootDir):
5050
raise Exception(
@@ -55,23 +55,23 @@ def Helper__GetCurTestTmpDir(request: pytest.FixtureRequest) -> str:
5555

5656
result = os.path.join(rootTmpDir, testPath2)
5757

58-
if request.node.cls is not None:
59-
clsName = request.node.cls.__name__
58+
if function.cls is not None:
59+
clsName = function.cls.__name__
6060
result = os.path.join(result, clsName)
6161

62-
result = os.path.join(result, request.node.name)
62+
result = os.path.join(result, function.name)
6363

6464
return result
6565

6666
# --------------------------------------------------------------------
67-
def CleanTestTmpDirBeforeExit(request: pytest.FixtureRequest):
68-
assert isinstance(request, pytest.FixtureRequest)
67+
def CleanTestTmpDirBeforeExit(function: pytest.Function):
68+
assert isinstance(function, pytest.Function)
6969

7070
if TestConfigHelper.NoCleanup():
7171
logging.info("A final data cleanup is disabled.")
7272
return
7373

74-
tmpDir = __class__.GetCurTestTmpDir(request)
74+
tmpDir = __class__.Helper__GetCurTestTmpDir(function)
7575
assert type(tmpDir) == str
7676

7777
if not os.path.exists(tmpDir):

tests/TestStartupData.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ def CalcRootTmpDir() -> str:
4343
assert type(resultPath) == str
4444
return resultPath
4545

46+
# --------------------------------------------------------------------
47+
def CalcRootLogDir() -> str:
48+
if TestConfigPropNames.TEST_CFG__LOG_DIR in os.environ:
49+
resultPath = os.environ[TestConfigPropNames.TEST_CFG__LOG_DIR]
50+
else:
51+
rootDir = __class__.CalcRootDir()
52+
resultPath = os.path.join(rootDir, "logs")
53+
54+
assert type(resultPath) == str
55+
return resultPath
56+
4657
# --------------------------------------------------------------------
4758
def CalcCurrentTestWorkerSignature() -> str:
4859
currentPID = os.getpid()
@@ -84,11 +95,18 @@ class TestStartupData:
8495
sm_RootTmpDataDir, sm_CurrentTestWorkerSignature
8596
)
8697

98+
sm_RootLogDir: str = TestStartupData__Helper.CalcRootLogDir()
99+
87100
# --------------------------------------------------------------------
88101
def GetRootDir() -> str:
89102
assert type(__class__.sm_RootDir) == str
90103
return __class__.sm_RootDir
91104

105+
# --------------------------------------------------------------------
106+
def GetRootLogDir() -> str:
107+
assert type(__class__.sm_RootLogDir) == str
108+
return __class__.sm_RootLogDir
109+
92110
# --------------------------------------------------------------------
93111
def GetCurrentTestWorkerSignature() -> str:
94112
assert type(__class__.sm_CurrentTestWorkerSignature) == str

tests/conftest.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .TestConfigProp import TestConfigPropNames
55
from .TestStartupData import TestStartupData
66
from .TestGlobalCache import TestGlobalCache
7+
from .TestServices import TestServices
78

89
import pluggy
910
import pytest
@@ -18,6 +19,10 @@
1819
import _pytest.unittest
1920
import _pytest.logging
2021

22+
# /////////////////////////////////////////////////////////////////////////////
23+
24+
T_TUPLE__str_int = typing.Tuple[str, int]
25+
2126
# /////////////////////////////////////////////////////////////////////////////
2227
# TEST_PROCESS_STATS
2328

@@ -35,11 +40,11 @@ class TEST_PROCESS_STATS:
3540
cUnexpectedTests: int = 0
3641
cAchtungTests: int = 0
3742

38-
FailedTests = list[str, int]()
39-
XFailedTests = list[str, int]()
40-
NotXFailedTests = list[str]()
41-
WarningTests = list[str, int]()
42-
AchtungTests = list[str]()
43+
FailedTests: typing.List[T_TUPLE__str_int] = list()
44+
XFailedTests: typing.List[T_TUPLE__str_int] = list()
45+
NotXFailedTests: typing.List[str] = list()
46+
WarningTests: typing.List[T_TUPLE__str_int] = list()
47+
AchtungTests: typing.List[str] = list()
4348

4449
cTotalDuration: datetime.timedelta = datetime.timedelta()
4550

@@ -303,6 +308,15 @@ def helper__makereport__setup(
303308
return
304309

305310

311+
# ------------------------------------------------------------------------
312+
class ExitStatusNames:
313+
FAILED = "FAILED"
314+
PASSED = "PASSED"
315+
XFAILED = "XFAILED"
316+
SKIPPED = "SKIPPED"
317+
UNEXPECTED = "UNEXPECTED"
318+
319+
306320
# ------------------------------------------------------------------------
307321
def helper__makereport__call(
308322
item: pytest.Function, call: pytest.CallInfo, outcome: pluggy.Result
@@ -345,28 +359,29 @@ def helper__makereport__call(
345359

346360
# --------
347361
exitStatus = None
362+
exitStatusInfo = None
348363
if rep.outcome == "skipped":
349364
assert call.excinfo is not None # research
350365
assert call.excinfo.value is not None # research
351366

352367
if type(call.excinfo.value) == _pytest.outcomes.Skipped: # noqa: E721
353368
assert not hasattr(rep, "wasxfail")
354369

355-
exitStatus = "SKIPPED"
370+
exitStatus = ExitStatusNames.SKIPPED
356371
reasonText = str(call.excinfo.value)
357372
reasonMsgTempl = "SKIP REASON: {0}"
358373

359374
TEST_PROCESS_STATS.incrementSkippedTestCount()
360375

361376
elif type(call.excinfo.value) == _pytest.outcomes.XFailed: # noqa: E721
362-
exitStatus = "XFAILED"
377+
exitStatus = ExitStatusNames.XFAILED
363378
reasonText = str(call.excinfo.value)
364379
reasonMsgTempl = "XFAIL REASON: {0}"
365380

366381
TEST_PROCESS_STATS.incrementXFailedTestCount(testID, item_error_msg_count)
367382

368383
else:
369-
exitStatus = "XFAILED"
384+
exitStatus = ExitStatusNames.XFAILED
370385
assert hasattr(rep, "wasxfail")
371386
assert rep.wasxfail is not None
372387
assert type(rep.wasxfail) == str # noqa: E721
@@ -403,7 +418,7 @@ def helper__makereport__call(
403418
assert item_error_msg_count > 0
404419
TEST_PROCESS_STATS.incrementFailedTestCount(testID, item_error_msg_count)
405420

406-
exitStatus = "FAILED"
421+
exitStatus = ExitStatusNames.FAILED
407422
elif rep.outcome == "passed":
408423
assert call.excinfo is None
409424

@@ -423,17 +438,25 @@ def helper__makereport__call(
423438
assert not hasattr(rep, "wasxfail")
424439

425440
TEST_PROCESS_STATS.incrementPassedTestCount()
426-
exitStatus = "PASSED"
441+
exitStatus = ExitStatusNames.PASSED
427442
else:
428443
TEST_PROCESS_STATS.incrementUnexpectedTests()
429-
exitStatus = "UNEXPECTED [{0}]".format(rep.outcome)
444+
exitStatus = ExitStatusNames.UNEXPECTED
445+
exitStatusInfo = rep.outcome
430446
# [2025-03-28] It may create a useless problem in new environment.
431447
# assert False
432448

433449
# --------
434450
if item_warning_msg_count > 0:
435451
TEST_PROCESS_STATS.incrementWarningTestCount(testID, item_warning_msg_count)
436452

453+
# --------
454+
if exitStatus == ExitStatusNames.FAILED:
455+
assert item_error_msg_count > 0
456+
pass
457+
else:
458+
TestServices.CleanTestTmpDirBeforeExit(item)
459+
437460
# --------
438461
assert type(TEST_PROCESS_STATS.cTotalDuration) == datetime.timedelta # noqa: E721
439462
assert type(testDurration) == datetime.timedelta # noqa: E721
@@ -442,11 +465,17 @@ def helper__makereport__call(
442465

443466
assert testDurration <= TEST_PROCESS_STATS.cTotalDuration
444467

468+
# --------
469+
exitStatusLineData = exitStatus
470+
471+
if exitStatusInfo is not None:
472+
exitStatusLineData += " [{}]".format(exitStatusInfo)
473+
445474
# --------
446475
logging.info("*")
447476
logging.info("* DURATION : {0}".format(timedelta_to_human_text(testDurration)))
448477
logging.info("*")
449-
logging.info("* EXIT STATUS : {0}".format(exitStatus))
478+
logging.info("* EXIT STATUS : {0}".format(exitStatusLineData))
450479
logging.info("* ERROR COUNT : {0}".format(item_error_msg_count))
451480
logging.info("* WARNING COUNT: {0}".format(item_warning_msg_count))
452481
logging.info("*")
@@ -694,7 +723,7 @@ def helper__calc_W(n: int) -> int:
694723

695724

696725
# ------------------------------------------------------------------------
697-
def helper__print_test_list(tests: list[str]) -> None:
726+
def helper__print_test_list(tests: typing.List[str]) -> None:
698727
assert type(tests) == list # noqa: E721
699728

700729
assert helper__calc_W(9) == 1
@@ -721,7 +750,7 @@ def helper__print_test_list(tests: list[str]) -> None:
721750

722751

723752
# ------------------------------------------------------------------------
724-
def helper__print_test_list2(tests: list[str, int]) -> None:
753+
def helper__print_test_list2(tests: typing.List[T_TUPLE__str_int]) -> None:
725754
assert type(tests) == list # noqa: E721
726755

727756
assert helper__calc_W(9) == 1
@@ -773,7 +802,9 @@ def LOCAL__print_line1_with_header(header: str):
773802
assert header != ""
774803
logging.info(C_LINE1 + " [" + header + "]")
775804

776-
def LOCAL__print_test_list(header: str, test_count: int, test_list: list[str]):
805+
def LOCAL__print_test_list(
806+
header: str, test_count: int, test_list: typing.List[str]
807+
):
777808
assert type(header) == str # noqa: E721
778809
assert type(test_count) == int # noqa: E721
779810
assert type(test_list) == list # noqa: E721
@@ -788,7 +819,7 @@ def LOCAL__print_test_list(header: str, test_count: int, test_list: list[str]):
788819
logging.info("")
789820

790821
def LOCAL__print_test_list2(
791-
header: str, test_count: int, test_list: list[str, int]
822+
header: str, test_count: int, test_list: typing.List[T_TUPLE__str_int]
792823
):
793824
assert type(header) == str # noqa: E721
794825
assert type(test_count) == int # noqa: E721
@@ -880,13 +911,9 @@ def pytest_configure(config: pytest.Config) -> None:
880911
log_name = TestStartupData.GetCurrentTestWorkerSignature()
881912
log_name += ".log"
882913

883-
if TestConfigPropNames.TEST_CFG__LOG_DIR in os.environ:
884-
log_path_v = os.environ[TestConfigPropNames.TEST_CFG__LOG_DIR]
885-
log_path = pathlib.Path(log_path_v)
886-
else:
887-
log_path = config.rootpath.joinpath("logs")
914+
log_dir = TestStartupData.GetRootLogDir()
888915

889-
log_path.mkdir(exist_ok=True)
916+
pathlib.Path(log_dir).mkdir(exist_ok=True)
890917

891918
logging_plugin: _pytest.logging.LoggingPlugin = config.pluginmanager.get_plugin(
892919
"logging-plugin"
@@ -895,7 +922,7 @@ def pytest_configure(config: pytest.Config) -> None:
895922
assert logging_plugin is not None
896923
assert isinstance(logging_plugin, _pytest.logging.LoggingPlugin)
897924

898-
logging_plugin.set_log_path(str(log_path / log_name))
925+
logging_plugin.set_log_path(os.path.join(log_dir, log_name))
899926

900927

901928
# /////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)