blob: 1f11206b7d30980495744e09f920e52166e126d1 [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001import sys
2import imp
3import os
4
5helpers_dir = os.getenv("PYCHARM_HELPERS_DIR", sys.path[0])
6if sys.path[0] != helpers_dir:
7 sys.path.insert(0, helpers_dir)
8
9from tcunittest import TeamcityTestRunner
10from nose_helper import TestLoader, ContextSuite
11from pycharm_run_utils import import_system_module
12from pycharm_run_utils import adjust_sys_path
13from pycharm_run_utils import debug, getModuleName, PYTHON_VERSION_MAJOR
14
15adjust_sys_path()
16
17os = import_system_module("os")
18re = import_system_module("re")
19
20modules = {}
21
22def loadSource(fileName):
23 baseName = os.path.basename(fileName)
24 moduleName = os.path.splitext(baseName)[0]
25
26 # for users wanted to run unittests under django
27 #because of django took advantage of module name
28 settings_file = os.getenv('DJANGO_SETTINGS_MODULE')
29 if settings_file and moduleName == "models":
30 baseName = os.path.realpath(fileName)
31 moduleName = ".".join((baseName.split(os.sep)[-2], "models"))
32
33 if moduleName in modules and len(sys.argv[1:-1]) == 1: # add unique number to prevent name collisions
34 cnt = 2
35 prefix = moduleName
36 while getModuleName(prefix, cnt) in modules:
37 cnt += 1
38 moduleName = getModuleName(prefix, cnt)
39 debug("/ Loading " + fileName + " as " + moduleName)
40 module = imp.load_source(moduleName, fileName)
41 modules[moduleName] = module
42 return module
43
44def walkModules(modulesAndPattern, dirname, names):
45 modules = modulesAndPattern[0]
46 pattern = modulesAndPattern[1]
47 prog_list = [re.compile(pat.strip()) for pat in pattern.split(',')]
48 for name in names:
49 for prog in prog_list:
50 if name.endswith(".py") and prog.match(name):
51 modules.append(loadSource(os.path.join(dirname, name)))
52
53def loadModulesFromFolderRec(folder, pattern = "test.*"):
54 modules = []
55 if PYTHON_VERSION_MAJOR == 3:
56 prog_list = [re.compile(pat.strip()) for pat in pattern.split(',')]
57 for root, dirs, files in os.walk(folder):
58 for name in files:
59 for prog in prog_list:
60 if name.endswith(".py") and prog.match(name):
61 modules.append(loadSource(os.path.join(root, name)))
62 else: # actually for jython compatibility
63 os.path.walk(folder, walkModules, (modules, pattern))
64
65 return modules
66
67testLoader = TestLoader()
68all = ContextSuite()
69pure_unittest = False
70
71def setLoader(module):
72 global testLoader, all
73 try:
74 module.__getattribute__('unittest2')
75 import unittest2
76
77 testLoader = unittest2.TestLoader()
78 all = unittest2.TestSuite()
79 except:
80 pass
81
82if __name__ == "__main__":
83 arg = sys.argv[-1]
84 if arg == "true":
85 import unittest
86
87 testLoader = unittest.TestLoader()
88 all = unittest.TestSuite()
89 pure_unittest = True
90
91 options = {}
92 for arg in sys.argv[1:-1]:
93 arg = arg.strip()
94 if len(arg) == 0:
95 continue
96
97 if arg.startswith("--"):
98 options[arg[2:]] = True
99 continue
100
101 a = arg.split("::")
102 if len(a) == 1:
103 # From module or folder
104 a_splitted = a[0].split(";")
105 if len(a_splitted) != 1:
106 # means we have pattern to match against
Tor Norbye0f831a72014-04-25 07:38:52 -0700107 if a_splitted[0].endswith(os.path.sep):
Tor Norbye3a2425a2013-11-04 10:16:08 -0800108 debug("/ from folder " + a_splitted[0] + ". Use pattern: " + a_splitted[1])
109 modules = loadModulesFromFolderRec(a_splitted[0], a_splitted[1])
110 else:
111 if a[0].endswith("/"):
112 debug("/ from folder " + a[0])
113 modules = loadModulesFromFolderRec(a[0])
114 else:
115 debug("/ from module " + a[0])
116 modules = [loadSource(a[0])]
117
118 for module in modules:
119 all.addTests(testLoader.loadTestsFromModule(module))
120
121 elif len(a) == 2:
122 # From testcase
123 debug("/ from testcase " + a[1] + " in " + a[0])
124 module = loadSource(a[0])
125 setLoader(module)
126
127 if pure_unittest:
128 all.addTests(testLoader.loadTestsFromTestCase(getattr(module, a[1])))
129 else:
130 all.addTests(testLoader.loadTestsFromTestClass(getattr(module, a[1])),
131 getattr(module, a[1]))
132 else:
133 # From method in class or from function
134 debug("/ from method " + a[2] + " in testcase " + a[1] + " in " + a[0])
135 module = loadSource(a[0])
136 setLoader(module)
137
138 if a[1] == "":
139 # test function, not method
140 all.addTest(testLoader.makeTest(getattr(module, a[2])))
141 else:
142 testCaseClass = getattr(module, a[1])
143 try:
144 all.addTest(testCaseClass(a[2]))
145 except:
146 # class is not a testcase inheritor
147 all.addTest(
148 testLoader.makeTest(getattr(testCaseClass, a[2]), testCaseClass))
149
150 debug("/ Loaded " + str(all.countTestCases()) + " tests")
151 TeamcityTestRunner().run(all, **options)