Browse Source

pytest: Configure logging in a fixture to match stdout capturing

pytest captures the output by monkey patching out `sys.stdout`. This may
conflict with our use of `sys.stdout` when configuring logging, resulting in
the "Write to closed file" issue that is spamming the logs. By making the
logging configuration a fixture hopefully we always use the correct
stdout (after pytest has monkey-patched it).
travis-debug
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
7201cb7315
  1. 15
      contrib/pyln-testing/pyln/testing/fixtures.py
  2. 5
      contrib/pyln-testing/pyln/testing/utils.py
  3. 2
      tests/fixtures.py

15
contrib/pyln-testing/pyln/testing/fixtures.py

@ -1,12 +1,13 @@
from concurrent import futures from concurrent import futures
from pyln.testing.db import SqliteDbProvider, PostgresDbProvider from pyln.testing.db import SqliteDbProvider, PostgresDbProvider
from pyln.testing.utils import NodeFactory, BitcoinD, ElementsD, env, DEVELOPER, LightningNode from pyln.testing.utils import NodeFactory, BitcoinD, ElementsD, env, DEVELOPER, LightningNode, TEST_DEBUG
import logging import logging
import os import os
import pytest import pytest
import re import re
import shutil import shutil
import sys
import tempfile import tempfile
@ -28,6 +29,18 @@ def test_base_dir():
shutil.rmtree(directory) shutil.rmtree(directory)
@pytest.fixture(autouse=True)
def setup_logging():
logger = logging.getLogger()
before_handlers = list(logger.handlers)
if TEST_DEBUG:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
yield
logger.handlers = before_handlers
@pytest.fixture @pytest.fixture
def directory(request, test_base_dir, test_name): def directory(request, test_base_dir, test_name):
"""Return a per-test specific directory. """Return a per-test specific directory.

5
contrib/pyln-testing/pyln/testing/utils.py

@ -17,7 +17,6 @@ import sqlite3
import string import string
import struct import struct
import subprocess import subprocess
import sys
import threading import threading
import time import time
@ -69,10 +68,6 @@ SLOW_MACHINE = env("SLOW_MACHINE", "0") == "1"
TIMEOUT = int(env("TIMEOUT", 180 if SLOW_MACHINE else 60)) TIMEOUT = int(env("TIMEOUT", 180 if SLOW_MACHINE else 60))
if TEST_DEBUG:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
def wait_for(success, timeout=TIMEOUT): def wait_for(success, timeout=TIMEOUT):
start_time = time.time() start_time = time.time()
interval = 0.25 interval = 0.25

2
tests/fixtures.py

@ -1,5 +1,5 @@
from utils import DEVELOPER, TEST_NETWORK # noqa: F401,F403 from utils import DEVELOPER, TEST_NETWORK # noqa: F401,F403
from pyln.testing.fixtures import directory, test_base_dir, test_name, chainparams, node_factory, bitcoind, teardown_checks, db_provider, executor # noqa: F401,F403 from pyln.testing.fixtures import directory, test_base_dir, test_name, chainparams, node_factory, bitcoind, teardown_checks, db_provider, executor, setup_logging # noqa: F401,F403
from pyln.testing import utils from pyln.testing import utils
import pytest import pytest

Loading…
Cancel
Save