Introduce new test functions to check logs

New functions log_contains, num_log_contains, num_log_has and num_log_has_re
are introduced in the conftest module to help and simplify checking:
- if logs contain a string;
- count how many messages contain a string;
- count how many messages are the given string;
- count how many messages matchs a regex.

A couple of existing tests are changed using the new functions.
This commit is contained in:
cdimauro
2021-12-26 09:49:14 +01:00
parent fbaf46901e
commit 6509c38717
3 changed files with 44 additions and 25 deletions

View File

@@ -49,6 +49,13 @@ def pytest_configure(config):
setattr(config.option, 'markexpr', 'not longrun')
def log_contains(line, logs, count=False):
# caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'spamfoobar')
# and we want to check if line ('foo', for example) is contained in the tuple.
return any(line in logger_name or line in message
for logger_name, level, message in logs.record_tuples)
def log_has(line, logs):
# caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'foobar')
# and we want to match line against foobar in the tuple
@@ -63,6 +70,27 @@ def log_has_re(line, logs):
False)
def num_log_contains(line, logs, count=False):
# caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'spamfoobar')
# and we want to check how many times line ('foo', for example) is contained in the tuples.
return sum(line in logger_name or line in message
for logger_name, level, message in logs.record_tuples)
def num_log_has(line, logs, count=False):
# caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'foobar')
# and we want to check how many times line is presente in the tuples.
return sum(line == logger_name or line == message
for logger_name, level, message in logs.record_tuples)
def num_log_has_re(line, logs, count=False):
# caplog mocker returns log as a tuple: ('freqtrade.something', logging.WARNING, 'foobar')
# and we want to check how many times line matches in the tuples.
return sum(re.match(line, logger_name) or re.match(line, message)
for logger_name, level, message in logs.record_tuples)
def get_args(args):
return Arguments(args).get_parsed_arg()