Added functionality to log_file_verbose. However, the default value does not work.

This commit is contained in:
Ben Nguyen Tran 2024-04-28 13:32:33 -04:00
parent db507ed468
commit 6e937c0aaf
5 changed files with 24 additions and 7 deletions

View File

@ -208,6 +208,7 @@ option names are:
If you need to record the whole test suite logging calls to a file, you can pass If you need to record the whole test suite logging calls to a file, you can pass
``--log-file=/path/to/log/file``. This log file is opened in write mode by default which ``--log-file=/path/to/log/file``. This log file is opened in write mode by default which
means that it will be overwritten at each run tests session. means that it will be overwritten at each run tests session.
You can specify the level of verbosity of the log file by passing ```--log-file-verbose=1``
If you'd like the file opened in append mode instead, then you can pass ``--log-file-mode=a``. If you'd like the file opened in append mode instead, then you can pass ``--log-file-mode=a``.
Note that relative paths for the log-file location, whether passed on the CLI or declared in a Note that relative paths for the log-file location, whether passed on the CLI or declared in a
config file, are always resolved relative to the current working directory. config file, are always resolved relative to the current working directory.

View File

@ -2107,7 +2107,7 @@ All the command-line flags can be obtained by running ``pytest --help``::
--log-cli-date-format=LOG_CLI_DATE_FORMAT --log-cli-date-format=LOG_CLI_DATE_FORMAT
Log date format used by the logging module Log date format used by the logging module
--log-file=LOG_FILE Path to a file when logging will be written to --log-file=LOG_FILE Path to a file when logging will be written to
--log-file-verbose Log file verbosity --log-file-verbose={0,1} Log file verbose
--log-file-mode={w,a} --log-file-mode={w,a}
Log file open mode Log file open mode
--log-file-level=LOG_FILE_LEVEL --log-file-level=LOG_FILE_LEVEL
@ -2208,6 +2208,8 @@ All the command-line flags can be obtained by running ``pytest --help``::
log_cli_date_format (string): log_cli_date_format (string):
Default value for --log-cli-date-format Default value for --log-cli-date-format
log_file (string): Default value for --log-file log_file (string): Default value for --log-file
log_file_verbose (int):
Default value for --log-file-verbose
log_file_mode (string): log_file_mode (string):
Default value for --log-file-mode Default value for --log-file-mode
log_file_level (string): log_file_level (string):

View File

@ -1576,6 +1576,7 @@ class Config:
``paths``, ``pathlist``, ``args`` and ``linelist`` : empty list ``[]`` ``paths``, ``pathlist``, ``args`` and ``linelist`` : empty list ``[]``
``bool`` : ``False`` ``bool`` : ``False``
``string`` : empty string ``""`` ``string`` : empty string ``""``
``int`` : ``0``
If neither the ``default`` nor the ``type`` parameter is passed If neither the ``default`` nor the ``type`` parameter is passed
while registering the configuration through while registering the configuration through
@ -1643,6 +1644,8 @@ class Config:
return value return value
elif type == "bool": elif type == "bool":
return _strtobool(str(value).strip()) return _strtobool(str(value).strip())
elif type is "int":
return int(value)
elif type == "string": elif type == "string":
return value return value
elif type is None: elif type is None:

View File

@ -180,7 +180,7 @@ class Parser:
name: str, name: str,
help: str, help: str,
type: Optional[ type: Optional[
Literal["string", "paths", "pathlist", "args", "linelist", "bool"] Literal["string", "paths", "pathlist", "args", "linelist", "bool", "int"]
] = None, ] = None,
default: Any = NOT_SET, default: Any = NOT_SET,
) -> None: ) -> None:
@ -190,7 +190,7 @@ class Parser:
Name of the ini-variable. Name of the ini-variable.
:param type: :param type:
Type of the variable. Can be: Type of the variable. Can be:
* ``int``: an integer
* ``string``: a string * ``string``: a string
* ``bool``: a boolean * ``bool``: a boolean
* ``args``: a list of strings, separated as in a shell * ``args``: a list of strings, separated as in a shell
@ -215,7 +215,7 @@ class Parser:
The value of ini-variables can be retrieved via a call to The value of ini-variables can be retrieved via a call to
:py:func:`config.getini(name) <pytest.Config.getini>`. :py:func:`config.getini(name) <pytest.Config.getini>`.
""" """
assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool") assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool", "int")
if default is NOT_SET: if default is NOT_SET:
default = get_ini_default_for_type(type) default = get_ini_default_for_type(type)
@ -224,7 +224,7 @@ class Parser:
def get_ini_default_for_type( def get_ini_default_for_type(
type: Optional[Literal["string", "paths", "pathlist", "args", "linelist", "bool"]], type: Optional[Literal["string", "paths", "pathlist", "args", "linelist", "bool", "int"]],
) -> Any: ) -> Any:
""" """
Used by addini to get the default value for a given ini-option type, when Used by addini to get the default value for a given ini-option type, when
@ -236,6 +236,8 @@ def get_ini_default_for_type(
return [] return []
elif type == "bool": elif type == "bool":
return False return False
elif type is "int":
return 0
else: else:
return "" return ""

View File

@ -301,8 +301,9 @@ def pytest_addoption(parser: Parser) -> None:
) )
add_option_ini( add_option_ini(
"--log-file-verbose", "--log-file-verbose",
dest="log_file_verbosity", dest="log_file_verbose",
default=None, default=0,
type="int",
help="Log file verbose", help="Log file verbose",
) )
add_option_ini( add_option_ini(
@ -845,6 +846,14 @@ class LoggingPlugin:
@hookimpl(wrapper=True) @hookimpl(wrapper=True)
def pytest_runtest_setup(self, item: nodes.Item) -> Generator[None, None, None]: def pytest_runtest_setup(self, item: nodes.Item) -> Generator[None, None, None]:
if self.log_file_verbose:
old_log_file_formatter = self.log_file_handler.formatter
self.log_file_handler.setFormatter(logging.Formatter())
self.log_file_handler.emit(logging.LogRecord('N/A', logging.WARNING, 'N/A', 0, f"Running at {item.nodeid}", None, None))
self.log_file_handler.setFormatter(old_log_file_formatter)
self.log_cli_handler.set_when("setup") self.log_cli_handler.set_when("setup")
empty: Dict[str, List[logging.LogRecord]] = {} empty: Dict[str, List[logging.LogRecord]] = {}