Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaecdcfb

Browse files
[conftest] Advanced processing of logging
This patch does the following things: - it processes the calls of logging.error as test errors - it prints the number of errors/warnings for each test
1 parent2090fbc commitaecdcfb

File tree

1 file changed

+209
-3
lines changed

1 file changed

+209
-3
lines changed

‎tests/conftest.py

Lines changed: 209 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
importpathlib
99
importmath
1010
importdatetime
11+
importtyping
1112

1213
import_pytest.outcomes
1314
import_pytest.unittest
@@ -212,6 +213,12 @@ def helper__build_test_id(item: pytest.Function) -> str:
212213

213214
returntestID
214215

216+
217+
# /////////////////////////////////////////////////////////////////////////////
218+
219+
g_error_msg_count_key=pytest.StashKey[int]()
220+
g_warning_msg_count_key=pytest.StashKey[int]()
221+
215222
# /////////////////////////////////////////////////////////////////////////////
216223

217224

@@ -285,6 +292,16 @@ def helper__makereport__call(
285292
asserttype(call)==pytest.CallInfo# noqa: E721
286293
asserttype(outcome)==pluggy.Result# noqa: E721
287294

295+
# --------
296+
item_error_msg_count=item.stash.get(g_error_msg_count_key,0)
297+
asserttype(item_error_msg_count)==int# noqa: E721
298+
assertitem_error_msg_count>=0
299+
300+
item_warning_msg_count=item.stash.get(g_warning_msg_count_key,0)
301+
asserttype(item_warning_msg_count)==int# noqa: E721
302+
assertitem_warning_msg_count>=0
303+
304+
# --------
288305
rep=outcome.get_result()
289306
assertrepisnotNone
290307
asserttype(rep)==pytest.TestReport# noqa: E721
@@ -336,6 +353,7 @@ def helper__makereport__call(
336353
reasonMsgTempl="XFAIL REASON: {0}"
337354

338355
logging.error(call.excinfo.value)
356+
item_error_msg_count+=1
339357

340358
asserttype(reasonText)==str# noqa: E721
341359

@@ -350,7 +368,13 @@ def helper__makereport__call(
350368

351369
TEST_PROCESS_STATS.incrementFailedTestCount(testID)
352370

353-
logging.error(call.excinfo.value)
371+
iftype(call.excinfo.value)==SIGNAL_EXCEPTION:# noqa: E721
372+
assertitem_error_msg_count>0
373+
pass
374+
else:
375+
logging.error(call.excinfo.value)
376+
item_error_msg_count+=1
377+
354378
exitStatus="FAILED"
355379
elifrep.outcome=="passed":
356380
assertcall.excinfoisNone
@@ -380,9 +404,11 @@ def helper__makereport__call(
380404

381405
# --------
382406
logging.info("*")
383-
logging.info("* DURATION : {0}".format(timedelta_to_human_text(testDurration)))
407+
logging.info("* DURATION: {0}".format(timedelta_to_human_text(testDurration)))
384408
logging.info("*")
385-
logging.info("* EXIT STATUS : {0}".format(exitStatus))
409+
logging.info("* EXIT STATUS : {0}".format(exitStatus))
410+
logging.info("* ERROR COUNT : {0}".format(item_error_msg_count))
411+
logging.info("* WARNING COUNT: {0}".format(item_warning_msg_count))
386412
logging.info("*")
387413
logging.info("* STOP TEST {0}".format(testID))
388414
logging.info("*")
@@ -437,6 +463,186 @@ def pytest_runtest_makereport(item: pytest.Function, call: pytest.CallInfo):
437463
# /////////////////////////////////////////////////////////////////////////////
438464

439465

466+
classLogErrorWrapper2:
467+
_old_method:any
468+
_counter:typing.Optional[int]
469+
470+
# --------------------------------------------------------------------
471+
def__init__(self):
472+
self._old_method=None
473+
self._counter=None
474+
475+
# --------------------------------------------------------------------
476+
def__enter__(self):
477+
assertself._old_methodisNone
478+
assertself._counterisNone
479+
480+
self._old_method=logging.error
481+
self._counter=0
482+
483+
logging.error=self
484+
returnself
485+
486+
# --------------------------------------------------------------------
487+
def__exit__(self,exc_type,exc_val,exc_tb):
488+
assertself._old_methodisnotNone
489+
assertself._counterisnotNone
490+
491+
assertlogging.errorisself
492+
493+
logging.error=self._old_method
494+
495+
self._old_method=None
496+
self._counter=None
497+
returnFalse
498+
499+
# --------------------------------------------------------------------
500+
def__call__(self,*args,**kwargs):
501+
assertself._old_methodisnotNone
502+
assertself._counterisnotNone
503+
504+
asserttype(self._counter)==int# noqa: E721
505+
assertself._counter>=0
506+
507+
r=self._old_method(*args,**kwargs)
508+
509+
self._counter+=1
510+
assertself._counter>0
511+
512+
returnr
513+
514+
515+
# /////////////////////////////////////////////////////////////////////////////
516+
517+
518+
classLogWarningWrapper2:
519+
_old_method:any
520+
_counter:typing.Optional[int]
521+
522+
# --------------------------------------------------------------------
523+
def__init__(self):
524+
self._old_method=None
525+
self._counter=None
526+
527+
# --------------------------------------------------------------------
528+
def__enter__(self):
529+
assertself._old_methodisNone
530+
assertself._counterisNone
531+
532+
self._old_method=logging.warning
533+
self._counter=0
534+
535+
logging.warning=self
536+
returnself
537+
538+
# --------------------------------------------------------------------
539+
def__exit__(self,exc_type,exc_val,exc_tb):
540+
assertself._old_methodisnotNone
541+
assertself._counterisnotNone
542+
543+
assertlogging.warningisself
544+
545+
logging.warning=self._old_method
546+
547+
self._old_method=None
548+
self._counter=None
549+
returnFalse
550+
551+
# --------------------------------------------------------------------
552+
def__call__(self,*args,**kwargs):
553+
assertself._old_methodisnotNone
554+
assertself._counterisnotNone
555+
556+
asserttype(self._counter)==int# noqa: E721
557+
assertself._counter>=0
558+
559+
r=self._old_method(*args,**kwargs)
560+
561+
self._counter+=1
562+
assertself._counter>0
563+
564+
returnr
565+
566+
567+
# /////////////////////////////////////////////////////////////////////////////
568+
569+
570+
classSIGNAL_EXCEPTION(Exception):
571+
def__init__(self):
572+
pass
573+
574+
575+
# /////////////////////////////////////////////////////////////////////////////
576+
577+
578+
@pytest.hookimpl(hookwrapper=True)
579+
defpytest_pyfunc_call(pyfuncitem:pytest.Function):
580+
assertpyfuncitemisnotNone
581+
assertisinstance(pyfuncitem,pytest.Function)
582+
583+
debug__log_error_method=logging.error
584+
assertdebug__log_error_methodisnotNone
585+
586+
debug__log_warning_method=logging.warning
587+
assertdebug__log_warning_methodisnotNone
588+
589+
pyfuncitem.stash[g_error_msg_count_key]=0
590+
pyfuncitem.stash[g_warning_msg_count_key]=0
591+
592+
try:
593+
withLogErrorWrapper2()aslogErrorWrapper,LogWarningWrapper2()aslogWarningWrapper:
594+
asserttype(logErrorWrapper)==LogErrorWrapper2# noqa: E721
595+
assertlogErrorWrapper._old_methodisnotNone
596+
asserttype(logErrorWrapper._counter)==int# noqa: E721
597+
assertlogErrorWrapper._counter==0
598+
assertlogging.errorislogErrorWrapper
599+
600+
asserttype(logWarningWrapper)==LogWarningWrapper2# noqa: E721
601+
assertlogWarningWrapper._old_methodisnotNone
602+
asserttype(logWarningWrapper._counter)==int# noqa: E721
603+
assertlogWarningWrapper._counter==0
604+
assertlogging.warningislogWarningWrapper
605+
606+
r:pluggy.Result=yield
607+
608+
assertrisnotNone
609+
asserttype(r)==pluggy.Result# noqa: E721
610+
611+
assertlogErrorWrapper._old_methodisnotNone
612+
asserttype(logErrorWrapper._counter)==int# noqa: E721
613+
assertlogErrorWrapper._counter>=0
614+
assertlogging.errorislogErrorWrapper
615+
616+
assertlogWarningWrapper._old_methodisnotNone
617+
asserttype(logWarningWrapper._counter)==int# noqa: E721
618+
assertlogWarningWrapper._counter>=0
619+
assertlogging.warningislogWarningWrapper
620+
621+
assertg_error_msg_count_keyinpyfuncitem.stash
622+
assertg_warning_msg_count_keyinpyfuncitem.stash
623+
624+
assertpyfuncitem.stash[g_error_msg_count_key]==0
625+
assertpyfuncitem.stash[g_warning_msg_count_key]==0
626+
627+
pyfuncitem.stash[g_error_msg_count_key]=logErrorWrapper._counter
628+
pyfuncitem.stash[g_warning_msg_count_key]=logWarningWrapper._counter
629+
630+
ifr.exceptionisnotNone:
631+
pass
632+
eliflogErrorWrapper._counter==0:
633+
pass
634+
else:
635+
assertlogErrorWrapper._counter>0
636+
r.force_exception(SIGNAL_EXCEPTION())
637+
finally:
638+
assertlogging.errorisdebug__log_error_method
639+
assertlogging.warningisdebug__log_warning_method
640+
pass
641+
642+
643+
# /////////////////////////////////////////////////////////////////////////////
644+
645+
440646
defhelper__calc_W(n:int)->int:
441647
assertn>0
442648

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp