From d5eed758fbb8cab96785f0a53c5ea208d8e7e715 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Wed, 10 Jun 2020 17:06:06 +0100
Subject: [PATCH] testutils: Move most test setup to be once per class

The result of this setup will be the same every time, so there isn't
much point in repeating it. When we add tests with time-consuming setup
like running steam-runtime-system-info, we'll only want to do that once.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 tests/testutils.py | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/tests/testutils.py b/tests/testutils.py
index 6fe711e7b..e85584b8f 100644
--- a/tests/testutils.py
+++ b/tests/testutils.py
@@ -80,33 +80,56 @@ class BaseTest(unittest.TestCase):
     Base class with some useful test setup.
     """
 
-    def setUp(self) -> None:
-        self.G_TEST_SRCDIR = os.getenv(
+    G_TEST_BUILDDIR = ''
+    G_TEST_SRCDIR = ''
+    artifacts = ''
+    tmpdir = None       # type: tempfile.TemporaryDirectory
+    top_builddir = ''
+    top_srcdir = ''
+
+    @classmethod
+    def setUpClass(cls) -> None:
+        cls.G_TEST_SRCDIR = os.getenv(
             'G_TEST_SRCDIR',
             os.path.abspath(os.path.dirname(__file__)),
         )
-        self.top_srcdir = os.path.dirname(self.G_TEST_SRCDIR)
-        self.G_TEST_BUILDDIR = os.getenv(
+        cls.top_srcdir = os.path.dirname(cls.G_TEST_SRCDIR)
+        cls.G_TEST_BUILDDIR = os.getenv(
             'G_TEST_BUILDDIR',
             os.path.abspath(
                 os.path.join(os.path.dirname(__file__), '..', '_build'),
             ),
         )
-        self.top_builddir = os.path.dirname(self.G_TEST_BUILDDIR)
+        cls.top_builddir = os.path.dirname(cls.G_TEST_BUILDDIR)
 
-        self.tmpdir = tempfile.TemporaryDirectory()
-        self.addCleanup(self.tmpdir.cleanup)
+        cls.tmpdir = tempfile.TemporaryDirectory()
 
         artifacts = os.getenv('AUTOPKGTEST_ARTIFACTS')
 
         if artifacts is not None:
-            self.artifacts = os.path.abspath(artifacts)
+            cls.artifacts = os.path.abspath(artifacts)
         else:
-            self.artifacts = self.tmpdir.name
+            cls.artifacts = cls.tmpdir.name
+
+    def setUp(self) -> None:
+        cls = self.__class__
+        self.G_TEST_BUILDDIR = cls.G_TEST_BUILDDIR
+        self.G_TEST_SRCDIR = cls.G_TEST_SRCDIR
+        self.artifacts = cls.artifacts
+        self.top_builddir = cls.top_builddir
+        self.top_srcdir = cls.top_srcdir
+
+        # Class and each test get separate temp directories
+        self.tmpdir = tempfile.TemporaryDirectory()
+        self.addCleanup(self.tmpdir.cleanup)
 
     def tearDown(self) -> None:
         pass
 
+    @classmethod
+    def tearDownClass(cls) -> None:
+        cls.tmpdir.cleanup()
+
 
 def test_main():
     logging.basicConfig(level=logging.DEBUG)
-- 
GitLab