1212import shutil
1313import warnings
1414from abc import abstractmethod
15- from contextlib import contextmanager
15+ from contextlib import contextmanager , ExitStack
1616from pathlib import Path
1717from pytest import FixtureRequest , Pytester , MonkeyPatch
1818from typing import Tuple , Mapping , TypeVar , Generator , Callable , Union
@@ -54,7 +54,7 @@ def allure_plugin_context():
5454
5555@contextmanager
5656def allure_in_memory_context (
57- path : str = None
57+ * paths : str
5858) -> Generator [AllureMemoryLogger , None , None ]:
5959 """Creates a context to test an allure integration.
6060
@@ -70,28 +70,30 @@ def allure_in_memory_context(
7070 restored.
7171
7272 Arguments:
73- path (str): a path to a class to replace.
74- Defaults to :code:`"allure_commons.logger.AllureFileLogger"`.
75- Provide this argument if the integration under test imports the
76- logger using the
77- :code:`from allure_commons.logger import AllureFileLogger` syntax.
73+ *paths (str): paths to classes to replace with the in-memory logger in
74+ addition to :code:`"allure_commons.logger.AllureFileLogger"`.
75+ Provide these if the integration under test imports the logger using
76+ the :code:`from allure_commons. logger import AllureFileLogger`
77+ syntax.
7878
7979 Yields:
80- AllureMemoryLogger: an instance of the in-memory logger, where an output
81- will be collected.
80+ AllureMemoryLogger: an instance of the in-memory logger, where the
81+ output is collected.
8282
8383 """
8484
85- if path is None :
86- path = "allure_commons.logger.AllureFileLogger"
87-
8885 # Plugin context must be set first, because mock patching may cause
8986 # module loading, thus, side effects, including allure decorators evaluation
9087 # (and that requires all plugins of nested allure to already be in place).
88+ paths = ("allure_commons.logger.AllureFileLogger" ,) + paths
9189 with allure_plugin_context ():
92- with mock .patch (path ) as ReporterMock :
93- ReporterMock .return_value = AllureMemoryLogger ()
94- yield ReporterMock .return_value
90+ logger = AllureMemoryLogger ()
91+ with ExitStack () as stack :
92+ for path in paths :
93+ stack .enter_context (
94+ mock .patch (path )
95+ ).return_value = logger
96+ yield logger
9597
9698
9799class AllureFileContextValue :
@@ -336,20 +338,38 @@ class AllureFrameworkRunner:
336338 """An abstract base class for framework test runners to test allure
337339 integrations.
338340
341+ Attributes:
342+ request (FixtureRequest): an instance of the request fixture.
343+ pytester (Pytester): an instance of the pytester fixture.
344+ allure_results (AllureMemoryLogger | AllureReport): the latest collected
345+ allure results.
346+ in_memory (bool): if `True`, the next run collects the results in memory
347+ (:attr:`AllureFrameworkRunner.allure_results` is AllureMemoryLogger).
348+ Otherwise, the next run creates allure result files and collects the
349+ report from them
350+ (:attr:`AllureFrameworkRunner.allure_results` is AllureReport).
351+ *imported_logger_paths: a sequence of paths to provide to
352+ :func:`allure_in_memory_context`.
353+
339354 """
340- def __init__ (self , request : FixtureRequest , pytester : Pytester ):
355+ def __init__ (
356+ self ,
357+ request : FixtureRequest ,
358+ pytester : Pytester ,
359+ * imported_logger_paths
360+ ):
341361 self .request = request
342362 self .pytester = pytester
343363 self .allure_results = None
344364 self .in_memory = True
365+ self .imported_logger_paths = list (imported_logger_paths )
345366
346367 def _run (
347368 self ,
348369 * args ,
349370 testplan_content : dict = None ,
350371 testplan_path : PathlikeT = None ,
351372 testplan_rst_id : str = None ,
352- logger_path : str = None ,
353373 ** kwargs
354374 ) -> AllureMemoryLogger :
355375 """Runs the framework and collect the allure results.
@@ -380,7 +400,6 @@ def _run(
380400 )
381401 with altered_env (ALLURE_TESTPLAN_PATH = testplan_path ):
382402 output = self .__run_and_collect_results_in_memory (
383- logger_path ,
384403 args ,
385404 kwargs
386405 ) if self .in_memory else self .__run_and_collect_results_from_fs (
@@ -572,8 +591,8 @@ def _cache_docstring_test_result(
572591 node .stash [cache_key ] = result
573592 return result
574593
575- def __run_and_collect_results_in_memory (self , logger_path , args , kwargs ):
576- with allure_in_memory_context (logger_path ) as output :
594+ def __run_and_collect_results_in_memory (self , args , kwargs ):
595+ with allure_in_memory_context (* self . imported_logger_paths ) as output :
577596 self ._run_framework (* args , ** kwargs )
578597 return output
579598
0 commit comments