unittests: use setUpClass instead of setUp for class constant data

for test attributes that are class constant. This reduces the work that
must be done for each test case, allowing some of the setup work to be
done once for the entire class.
pull/13552/head
Dylan Baker 4 months ago committed by Eli Schwartz
parent e808aa161c
commit 4b76aabe3a
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 67
      unittests/baseplatformtests.py

@ -47,50 +47,53 @@ class BasePlatformTests(TestCase):
prefix = '/usr' prefix = '/usr'
libdir = 'lib' libdir = 'lib'
def setUp(self): @classmethod
super().setUp() def setUpClass(cls) -> None:
self.maxDiff = None super().setUpClass()
cls.maxDiff = None
src_root = str(PurePath(__file__).parents[1]) src_root = str(PurePath(__file__).parents[1])
self.src_root = src_root cls.src_root = src_root
# Get the backend # Get the backend
self.backend_name = os.environ.get('MESON_UNIT_TEST_BACKEND', 'ninja') cls.backend_name = os.environ.get('MESON_UNIT_TEST_BACKEND', 'ninja')
backend_type = 'vs' if self.backend_name.startswith('vs') else self.backend_name backend_type = 'vs' if cls.backend_name.startswith('vs') else cls.backend_name
self.backend = getattr(Backend, backend_type) cls.backend = getattr(Backend, backend_type)
self.meson_args = ['--backend=' + self.backend_name] cls.meson_args = ['--backend=' + cls.backend_name]
self.meson_native_files = [] cls.meson_command = python_command + [get_meson_script()]
self.meson_cross_files = [] cls.setup_command = cls.meson_command + ['setup'] + cls.meson_args
self.meson_command = python_command + [get_meson_script()] cls.mconf_command = cls.meson_command + ['configure']
self.setup_command = self.meson_command + ['setup'] + self.meson_args cls.mintro_command = cls.meson_command + ['introspect']
self.mconf_command = self.meson_command + ['configure'] cls.wrap_command = cls.meson_command + ['wrap']
self.mintro_command = self.meson_command + ['introspect'] cls.rewrite_command = cls.meson_command + ['rewrite']
self.wrap_command = self.meson_command + ['wrap']
self.rewrite_command = self.meson_command + ['rewrite']
# Backend-specific build commands # Backend-specific build commands
self.build_command, self.clean_command, self.test_command, self.install_command, \ cls.build_command, cls.clean_command, cls.test_command, cls.install_command, \
self.uninstall_command = get_backend_commands(self.backend) cls.uninstall_command = get_backend_commands(cls.backend)
# Test directories # Test directories
self.common_test_dir = os.path.join(src_root, 'test cases/common') cls.common_test_dir = os.path.join(src_root, 'test cases/common')
self.python_test_dir = os.path.join(src_root, 'test cases/python') cls.python_test_dir = os.path.join(src_root, 'test cases/python')
self.rust_test_dir = os.path.join(src_root, 'test cases/rust') cls.rust_test_dir = os.path.join(src_root, 'test cases/rust')
self.vala_test_dir = os.path.join(src_root, 'test cases/vala') cls.vala_test_dir = os.path.join(src_root, 'test cases/vala')
self.framework_test_dir = os.path.join(src_root, 'test cases/frameworks') cls.framework_test_dir = os.path.join(src_root, 'test cases/frameworks')
self.unit_test_dir = os.path.join(src_root, 'test cases/unit') cls.unit_test_dir = os.path.join(src_root, 'test cases/unit')
self.rewrite_test_dir = os.path.join(src_root, 'test cases/rewrite') cls.rewrite_test_dir = os.path.join(src_root, 'test cases/rewrite')
self.linuxlike_test_dir = os.path.join(src_root, 'test cases/linuxlike') cls.linuxlike_test_dir = os.path.join(src_root, 'test cases/linuxlike')
self.objc_test_dir = os.path.join(src_root, 'test cases/objc') cls.objc_test_dir = os.path.join(src_root, 'test cases/objc')
self.objcpp_test_dir = os.path.join(src_root, 'test cases/objcpp') cls.objcpp_test_dir = os.path.join(src_root, 'test cases/objcpp')
self.darwin_test_dir = os.path.join(src_root, 'test cases/darwin') cls.darwin_test_dir = os.path.join(src_root, 'test cases/darwin')
# Misc stuff # Misc stuff
if self.backend is Backend.ninja: if cls.backend is Backend.ninja:
self.no_rebuild_stdout = ['ninja: no work to do.', 'samu: nothing to do'] cls.no_rebuild_stdout = ['ninja: no work to do.', 'samu: nothing to do']
else: else:
# VS doesn't have a stable output when no changes are done # VS doesn't have a stable output when no changes are done
# XCode backend is untested with unit tests, help welcome! # XCode backend is untested with unit tests, help welcome!
self.no_rebuild_stdout = [f'UNKNOWN BACKEND {self.backend.name!r}'] cls.no_rebuild_stdout = [f'UNKNOWN BACKEND {cls.backend.name!r}']
os.environ['COLUMNS'] = '80' os.environ['COLUMNS'] = '80'
os.environ['PYTHONIOENCODING'] = 'utf8' os.environ['PYTHONIOENCODING'] = 'utf8'
def setUp(self):
super().setUp()
self.meson_native_files = []
self.meson_cross_files = []
self.new_builddir() self.new_builddir()
def change_builddir(self, newdir): def change_builddir(self, newdir):

Loading…
Cancel
Save