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).
This commit is contained in:
Christian Decker 2020-02-19 17:57:56 +01:00 committed by Rusty Russell
parent cf8c972883
commit 7201cb7315
3 changed files with 15 additions and 7 deletions

View file

@ -1,12 +1,13 @@
from concurrent import futures
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 os
import pytest
import re
import shutil
import sys
import tempfile
@ -28,6 +29,18 @@ def test_base_dir():
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
def directory(request, test_base_dir, test_name):
"""Return a per-test specific directory.

View file

@ -17,7 +17,6 @@ import sqlite3
import string
import struct
import subprocess
import sys
import threading
import time
@ -69,10 +68,6 @@ SLOW_MACHINE = env("SLOW_MACHINE", "0") == "1"
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):
start_time = time.time()
interval = 0.25

View file

@ -1,5 +1,5 @@
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
import pytest