Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Tests all modules in import module test
Browse files Browse the repository at this point in the history
nutti committed Jun 27, 2020
1 parent bd2e8b3 commit 93a3d0b
Showing 16 changed files with 150 additions and 270 deletions.
10 changes: 0 additions & 10 deletions tests/python/fake_bpy_module_test/__init__.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/aud_test.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/bgl_test.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/blf_test.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/bmesh_test.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/bpy_extras_test.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/bpy_test.py

This file was deleted.

20 changes: 0 additions & 20 deletions tests/python/fake_bpy_module_test/common.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/freestyle_test.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/gpu_extras_test.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests/python/fake_bpy_module_test/gpu_test.py

This file was deleted.

16 changes: 0 additions & 16 deletions tests/python/fake_bpy_module_test/mathutils_test.py

This file was deleted.

109 changes: 109 additions & 0 deletions tests/python/import_module_test/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
import sys
import argparse
import unittest
import glob
import re
import shutil
import inspect

from typing import List


TESTS_TEMPLATE_FILE = "template.py.tpl"
GENERATED_TESTS_DIR = "generated_tests"


class ImportModuleTestConfig:
def __init__(self):
self.modules_path = ""


def parse_options(config: ImportModuleTestConfig):
usage = "Usage: python {} [-p <modules_path>]".format(__file__)
parser = argparse.ArgumentParser(usage)
parser.add_argument("-p", dest="modules_path", type=str, help="fake-bpy-module path")

args = parser.parse_args()
if args.modules_path:
config.modules_path = args.modules_path


def generate_tests(config: ImportModuleTestConfig) -> list:
# Search modules to test.
files = glob.glob("{}/*".format(config.modules_path), recursive=False)
module_names = [os.path.splitext(os.path.basename(f))[0] for f in files]

# Load template.
script_dir = os.path.dirname(__file__)
with open("{}/{}".format(script_dir, TESTS_TEMPLATE_FILE), "r") as f:
template_content = f.readlines()

# Generate test codes.
tests_dir = "{}/{}".format(script_dir, GENERATED_TESTS_DIR)
os.makedirs(tests_dir, exist_ok=False)
init_file = open("{}/__init__.py".format(tests_dir), "w")

def replace_template_content(content: List[str], module_name: str) -> List[str]:
output = []
for line in content:
line = re.sub(r"<%% CLASS_NAME %%>",
"{}ImportTest".format(re.sub(r"_(.)", lambda x: x.group(1).upper(), module_name.capitalize())),
line)
line = re.sub(r"<%% MODULE_NAME %%>", module_name, line)
output.append(line)
return output

for mod_name in module_names:
test_codes = replace_template_content(template_content, mod_name)
with open("{}/{}_test.py".format(tests_dir, mod_name), "w") as f:
f.writelines(test_codes)
init_file.write("from . import {}_test\n".format(mod_name))
init_file.close()

# Load generated modules.
# After this time, we can delete generated test codes.
sys.path.append(os.path.dirname(__file__))
exec("import {}".format(GENERATED_TESTS_DIR))

# Get test cases.
generated_tests_package = sys.modules[GENERATED_TESTS_DIR]
tests_modules = [m[1] for m in inspect.getmembers(generated_tests_package, inspect.ismodule)]
test_cases = []
for m in tests_modules:
test_cases.extend([m[1] for m in inspect.getmembers(m, inspect.isclass)])

# Delete generated test codes.
shutil.rmtree(tests_dir)

return test_cases


def run_tests(test_cases: list) -> bool:
suite = unittest.TestSuite()
for case in test_cases:
suite.addTest(unittest.makeSuite(case))
ret = unittest.TextTestRunner().run(suite).wasSuccessful()

return ret


def main():
# Parse options.
config = ImportModuleTestConfig()
parse_options(config)

# Add testee module.
path = os.path.abspath(config.modules_path)
sys.path.append(path)

# Generate tests.
test_cases = generate_tests(config)

# Run tests.
ret = run_tests(test_cases)
sys.exit(not ret)


if __name__ == "__main__":
main()
39 changes: 39 additions & 0 deletions tests/python/import_module_test/template.py.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
import unittest
import os


LOG_DIR = "import_module_test.log"

class <%% CLASS_NAME %%>(unittest.TestCase):

module_name = "<%% MODULE_NAME %%>"

@classmethod
def setUpClass(cls):
if cls.module_name is None:
raise ValueError("module_name must set")

cls.log_dir = "{}/{}".format(LOG_DIR, cls.module_name)
os.makedirs(cls.log_dir, exist_ok=True)

filename = "{}/<%% CLASS_NAME %%>.log".format(cls.log_dir)
cls.file_ = open(filename, "w")

@classmethod
def tearDownClass(cls):
cls.file_.close()

def setUp(self):
self.log("========== Test: {} ==========".format(self.id()))

import <%% MODULE_NAME %%>

def tearDown(self):
sys.modules.pop(self.module_name)

def log(self, message):
self.__class__.file_.write(message + "\n")

def test_nothing(self):
self.log("Test nothing")
Loading

0 comments on commit 93a3d0b

Please sign in to comment.