forked from nsacyber/WALKOFF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testapps.py
58 lines (51 loc) · 2.08 KB
/
testapps.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
import unittest
import os
import sys
import argparse
import importlib
from core.helpers import list_apps
from core.config.paths import apps_path
def cmd_line():
parser = argparse.ArgumentParser("Test Apps")
parser.add_argument('-a', '--apps', nargs='*', type=str, required=False,
help='List of apps for which you would like to test')
parser.add_argument('-A', '--all', action='store_true', help='Test all apps')
args = parser.parse_args()
return args
def get_tests(app_name):
tests_path = os.path.join(apps_path, app_name, 'tests')
if os.path.isdir(tests_path):
test_files = [os.path.splitext(f)[0]
for f in os.listdir(tests_path) if (os.path.isfile(os.path.join(tests_path, f))
and f.endswith('.py')
and f != '__init__.py')]
test_modules = [importlib.import_module('apps.{0}.tests.{1}'.format(app_name, test_module))
for test_module in test_files]
return test_modules
else:
print('App {0} has no test directory!'.format(app_name))
def test_app(app_name):
print('Testing app: {0}'.format(app))
test_modules = get_tests(app_name)
if test_modules:
suite = unittest.TestSuite()
suite.addTests([unittest.TestLoader().loadTestsFromModule(test_module)
for test_module in test_modules])
return unittest.TextTestRunner(verbosity=1).run(suite).wasSuccessful()
elif test_modules is None or len(test_modules) == 0:
print("App {0} has no tests. Don't be that person. Write your tests.".format(app_name))
return True
if __name__ == '__main__':
cmd_args = cmd_line()
all_apps = list_apps()
ret = True
if cmd_args.all:
for app in all_apps:
ret &= test_app(app)
else:
for app in cmd_args.apps:
if app in all_apps:
ret &= test_app(app)
else:
print('App {0} not found!'.format(app))
sys.exit(not ret)