Browse Source

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 <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
de3b359782
  1. 24
      tests/fixtures.py
  2. 1
      tests/test_lightningd.py

24
tests/fixtures.py

@ -10,7 +10,6 @@ import tempfile
import utils import utils
TEST_DIR = tempfile.mkdtemp(prefix='ltests-')
VALGRIND = os.getenv("NO_VALGRIND", "0") == "0" VALGRIND = os.getenv("NO_VALGRIND", "0") == "0"
DEVELOPER = os.getenv("DEVELOPER", "0") == "1" DEVELOPER = os.getenv("DEVELOPER", "0") == "1"
TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1" TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1"
@ -21,17 +20,32 @@ TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1"
__attempts = {} __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 @pytest.fixture
def directory(test_name): def directory(test_base_dir, test_name):
"""Return a per-test specific directory. """Return a per-test specific directory.
This makes a unique test-directory even if a test is rerun multiple times. 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 # Auto set value if it isn't in the dict yet
__attempts[test_name] = __attempts.get(test_name, 0) + 1 __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 @pytest.fixture
@ -100,8 +114,6 @@ def node_factory(directory, test_name, bitcoind, executor):
if not ok: if not ok:
raise Exception("At least one lightning exited with unexpected non-zero return code") raise Exception("At least one lightning exited with unexpected non-zero return code")
shutil.rmtree(nf.directory)
def getValgrindErrors(node): def getValgrindErrors(node):
for error_file in os.listdir(node.daemon.lightning_dir): for error_file in os.listdir(node.daemon.lightning_dir):

1
tests/test_lightningd.py

@ -32,7 +32,6 @@ VALGRIND = os.getenv("NO_VALGRIND", "0") == "0"
DEVELOPER = os.getenv("DEVELOPER", "0") == "1" DEVELOPER = os.getenv("DEVELOPER", "0") == "1"
TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1" TEST_DEBUG = os.getenv("TEST_DEBUG", "0") == "1"
print("Testing results are in {}".format(TEST_DIR))
if TEST_DEBUG: if TEST_DEBUG:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

Loading…
Cancel
Save