From de3b359782f9fd5558ed548a691bea809d5f5af1 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 5 Jun 2018 15:41:18 +0200 Subject: [PATCH] pytest: Use pytest fixtures for the test directory and clean it up The modern, pytest based, tests now clean up after themselves by removing directories of successful tests and the base directory if there was no failure. Signed-off-by: Christian Decker --- tests/fixtures.py | 24 ++++++++++++++++++------ tests/test_lightningd.py | 1 - 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index 1f6c3a4a9..49814f895 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -10,7 +10,6 @@ import tempfile import utils -TEST_DIR = tempfile.mkdtemp(prefix='ltests-') VALGRIND = os.getenv("NO_VALGRIND", "0") == "0" DEVELOPER = os.getenv("DEVELOPER", "0") == "1" TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1" @@ -21,17 +20,32 @@ TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1" __attempts = {} +@pytest.fixture(scope="session") +def test_base_dir(): + directory = tempfile.mkdtemp(prefix='ltests-') + print("Running tests in {}".format(directory)) + + yield directory + + if os.listdir(directory) == []: + shutil.rmtree(directory) + + @pytest.fixture -def directory(test_name): +def directory(test_base_dir, test_name): """Return a per-test specific directory. This makes a unique test-directory even if a test is rerun multiple times. """ - global TEST_DIR, __attempts + global __attempts # Auto set value if it isn't in the dict yet __attempts[test_name] = __attempts.get(test_name, 0) + 1 - yield os.path.join(TEST_DIR, "{}_{}".format(test_name, __attempts[test_name])) + directory = os.path.join(test_base_dir, "{}_{}".format(test_name, __attempts[test_name])) + + yield directory + + shutil.rmtree(directory) @pytest.fixture @@ -100,8 +114,6 @@ def node_factory(directory, test_name, bitcoind, executor): if not ok: raise Exception("At least one lightning exited with unexpected non-zero return code") - shutil.rmtree(nf.directory) - def getValgrindErrors(node): for error_file in os.listdir(node.daemon.lightning_dir): diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index 4704b83c6..ee0293288 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -32,7 +32,6 @@ VALGRIND = os.getenv("NO_VALGRIND", "0") == "0" DEVELOPER = os.getenv("DEVELOPER", "0") == "1" TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1" -print("Testing results are in {}".format(TEST_DIR)) if TEST_DEBUG: logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)