-
Notifications
You must be signed in to change notification settings - Fork 4
/
testing.py
67 lines (58 loc) · 1.79 KB
/
testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from os.path import join, dirname, basename
import logging
import unittest
from io import StringIO
from contextlib import contextmanager
import sverchok
import megapolis
try:
import coverage
coverage_available = True
except ImportError:
megapolis.logger.info("Coverage module is not installed")
coverage_available = False
@contextmanager
def coverage_report():
if not coverage_available:
yield None
else:
try:
cov = coverage.Coverage()
cov.start()
yield cov
finally:
cov.stop()
cov.save()
cov.html_report()
def get_tests_path():
sv_ex_init = megapolis.__file__
return join(dirname(sv_ex_init), "tests")
def run_all_tests(pattern=None):
if pattern is None:
pattern = "*_tests.py"
tests_path = get_tests_path()
log_handler = logging.FileHandler(join(tests_path, "sverchok_tests.log"), mode='w')
logging.getLogger().addHandler(log_handler)
try:
loader = unittest.TestLoader()
suite = loader.discover(start_dir = tests_path, pattern = pattern)
buffer = StringIO()
runner = unittest.TextTestRunner(stream = buffer, verbosity=2)
with coverage_report():
result = runner.run(suite)
megapolis.logger.info("Test cases result:\n%s", buffer.getvalue())
return result
finally:
logging.getLogger().removeHandler(log_handler)
if __name__ == "__main__":
import sys
try:
#register()
result = run_all_tests()
if not result.wasSuccessful():
# We have to raise an exception for Blender to exit with specified exit code.
raise Exception("Some tests failed")
sys.exit(0)
except Exception as e:
print(e)
sys.exit(1)