Skip to content

Commit

Permalink
Merge pull request #7486 from drew2a/feature/pytest_extended_log_verbose
Browse files Browse the repository at this point in the history
Make pytest extended logging logic dependent on `--verbose` flag
  • Loading branch information
drew2a authored Jun 15, 2023
2 parents 2839bf5 + d2f4657 commit 889eb2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/tribler/core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time

import pytest
from _pytest.config import Config

from tribler.core.utilities.network_utils import default_network_utils

Expand All @@ -14,6 +15,7 @@
# was garbage collected. Without the origin tracking, it may be hard to see the test that created the task.
sys.set_coroutine_origin_tracking_depth(10)

enable_extended_logging = False
pytest_start_time = 0 # a time when the test suite started


Expand All @@ -24,6 +26,14 @@ def pytest_configure(config):
logging.getLogger('faker.factory').propagate = False


@pytest.hookimpl
def pytest_cmdline_main(config: Config):
""" Enable extended logging if the verbose option is used """
# Called for performing the main command line action.
global enable_extended_logging # pylint: disable=global-statement
enable_extended_logging = config.option.verbose > 0


@pytest.hookimpl
def pytest_collection_finish(session):
""" Save the start time of the test suite execution"""
Expand All @@ -40,7 +50,8 @@ def pytest_runtest_protocol(item, log=True, nextitem=None):
yield
duration = time.time() - start_time
total = time.time() - pytest_start_time
print(f' in {duration:.3f}s ({total:.1f}s in total)', end='')
if enable_extended_logging:
print(f' in {duration:.3f}s ({total:.1f}s in total)', end='')


@pytest.fixture
Expand Down
13 changes: 12 additions & 1 deletion src/tribler/gui/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import time

import pytest
from _pytest.config import Config

enable_extended_logging = False
pytest_start_time = 0 # a time when the test suite started


Expand All @@ -15,6 +17,14 @@ def pytest_configure(config): # pylint: disable=unused-argument
logging.getLogger('PyQt5.uic').propagate = False


@pytest.hookimpl
def pytest_cmdline_main(config: Config):
""" Enable extended logging if the verbose option is used """
# Called for performing the main command line action.
global enable_extended_logging # pylint: disable=global-statement
enable_extended_logging = config.option.verbose > 0


def pytest_addoption(parser):
parser.addoption('--guitests', action='store_true', dest="guitests",
default=False, help="enable longrundecorated tests")
Expand Down Expand Up @@ -49,4 +59,5 @@ def pytest_runtest_protocol(item, log=True, nextitem=None):
yield
duration = time.time() - start_time
total = time.time() - pytest_start_time
print(f' in {duration:.3f}s ({total:.1f}s in total)', end='')
if enable_extended_logging:
print(f' in {duration:.3f}s ({total:.1f}s in total)', end='')

0 comments on commit 889eb2c

Please sign in to comment.