@@ -30,52 +30,49 @@ def pytest_addoption(parser):
3030@pytest .fixture (name = "printer" , scope = "function" )
3131def printer (request ):
3232 """pytest plugin to print test progress steps in verbose mode"""
33- return _provide_printer (request )
33+ return create_printer (request )
3434
3535
3636@pytest .fixture (scope = "session" , name = "printer_session" )
3737def printer_session (request ):
38- return _provide_printer (request )
38+ return create_printer (request )
3939
4040
41- class State (object ):
42- def __init__ (self , print_relative_time ):
43- self .first_call = True
44- self .start_datetime = datetime .now () if print_relative_time else None
45- self .print_relative_time = print_relative_time
41+ def create_printer (request ):
42+ if request .config .getoption ("pytest_print_on" ) or request .config .getoption ("verbose" ) > 0 :
43+ terminal_reporter = request .config .pluginmanager .getplugin ("terminalreporter" )
44+ if terminal_reporter is not None :
45+ state = State (request .config .getoption ("pytest_print_relative_time" ), terminal_reporter )
46+ return state .print
4647
47- @property
48- def elapsed (self ):
49- if self .start_datetime is None :
50- return None
51- return (datetime .now () - self .start_datetime ).total_seconds ()
48+ return no_op
5249
53- __slots__ = ("first_call" , "start_datetime" , "print_relative_time" )
5450
51+ # noinspection PyUnusedLocal
52+ def no_op (msg ):
53+ """Do nothing"""
5554
56- def _provide_printer (request ):
57- if request .config .getoption ("pytest_print_on" ) and request .config .getoption ("verbose" ) > 0 :
58- terminal_reporter = request .config .pluginmanager .getplugin ("terminalreporter" )
59- if terminal_reporter is not None :
60- state = State (request .config .getoption ("pytest_print_relative_time" ))
61-
62- def _print (msg ):
63- if state .first_call : # in case of the first call we don't have a new empty line, print it
64- state .first_call = False
65- terminal_reporter .write ("\n " )
6655
67- terminal_reporter .write ("\t " )
56+ class State (object ):
57+ def __init__ (self , print_relative , reporter ):
58+ self ._reporter = reporter
59+ self ._start = datetime .now () if print_relative else None
60+ self ._print_relative = print_relative
6861
69- if state .print_relative_time :
70- terminal_reporter .write (str (state .elapsed ))
71- terminal_reporter .write ("\t " )
62+ @property
63+ def elapsed (self ):
64+ if self ._start is None :
65+ return None
66+ return (datetime .now () - self ._start ).total_seconds ()
7267
73- terminal_reporter .write (msg )
74- terminal_reporter .write ("\n " )
68+ def print (self , msg ):
69+ msg = "\t {}{}" .format ("{}\t " .format (self .elapsed ) if self ._print_relative else "" , msg )
70+ self ._reporter .write_line (msg )
7571
76- return _print
72+ def __repr__ (self ):
73+ return "{}(print_relative={}, reporter={!r})" .format (type (self ).__name__ , self ._print_relative , self ._reporter )
7774
78- return lambda * args : None
75+ __slots__ = ( "_start" , "_print_relative" , "_reporter" )
7976
8077
8178__all__ = ("__version__" ,)
0 commit comments