testRunner.py 6.89 KB
Newer Older
Marc Duez's avatar
Marc Duez committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python
"""
Runs all tests that exist in the tests directories.

The class should have a name based on the file's path, like this:
FilenameDirectory -> DefaultTasksModel

for example:
applications/app/tests/controllers/default.py
is
class DefaultController(unittest.TestCase)

BEWARE that the name is NOT in plural (controllers->Controller)

15
16
17
18
require: 
pip install unittest2
pip install unittest-xml-reporting

Marc Duez's avatar
Marc Duez committed
19
Execute with:
20
>   python web2py.py -S vidjil -M -R testRunner.py
Marc Duez's avatar
Marc Duez committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35



made using web2py slices (http://www.web2pyslices.com) 1392 and 1465 from 

Jon Vlachoyiannis
jon@emotionull.com

Nico de Groot
ndegroot0@gmail.com

"""



36

Marc Duez's avatar
Marc Duez committed
37
from gluon import current
Marc Duez's avatar
Marc Duez committed
38
import gluon
Marc Duez's avatar
Marc Duez committed
39
40
from gluon.tools import Auth
import unittest
41
import xmlrunner
Marc Duez's avatar
Marc Duez committed
42
43
44
45
46
47
48
49
import glob
import sys
import doctest
import os
import traceback
import shutil
from copy import copy

50
execfile("applications/vidjil/controllers/default.py", globals())
Marc Duez's avatar
Marc Duez committed
51
52
53


# create a test database by copying the original db
54
55
56
57
test_db = DAL('sqlite://testing1234.sqlite')

# build default database if doesn't exist
init_db()
Marc Duez's avatar
Marc Duez committed
58

Marc Duez's avatar
Marc Duez committed
59
60
61
62
63
for tablename in db.tables:  # Copy tables!
    table_copy = [copy(f) for f in db[tablename]]
    test_db.define_table(tablename, *table_copy)

db = test_db
64
auth.db = test_db
Marc Duez's avatar
Marc Duez committed
65

66

Marc Duez's avatar
Marc Duez committed
67
68
init_db(True)

69
# use a fake user
Marc Duez's avatar
Marc Duez committed
70
71
72
73
user_id = db.auth_user.insert(
    first_name='Testers',
    last_name='Inc',
    email='test@vidjil.org',
Marc Duez's avatar
Marc Duez committed
74
    password= db.auth_user.password.validate('1234')[0]
Marc Duez's avatar
Marc Duez committed
75
76
77
78
79
80
81
82
)
unique_group = db.auth_group.insert(role="user_"+str(user_id), description=" ")
db.auth_membership.insert(user_id=user_id, group_id=unique_group)

# with admin privilege
group_id = 1 #admin group
db.auth_membership.insert(user_id=user_id, group_id=group_id)

Marc Duez's avatar
Marc Duez committed
83

84
85
86
87
88
89
90
# add fake config
fake_config_id = db.config.insert(name="config_test_popipo",
                                    info="popapipapo",
                                    command="-plop",
                                    fuse_command="-plop",
                                    program="plop.cpp"
                                    )
Marc Duez's avatar
Marc Duez committed
91
92
93
94
95
96
97
98
99
100
101
102
                                    
db.auth_permission.insert(group_id = group_id,
                        name = "admin",
                        table_name = "config",
                        record_id = fake_config_id
                        )
db.auth_permission.insert(group_id = group_id,
                        name = "read",
                        table_name = "config",
                        record_id = fake_config_id
                        )
                                
103
104
105
106
107
108
109
# add fake patient
fake_patient_id = db.patient.insert(first_name="plop",
                                   last_name="plop",
                                   birth="1902-02-02",
                                   info="plop",
                                   id_label="plop",
                                   creator=user_id)
Marc Duez's avatar
Marc Duez committed
110
111
112
113
114
115
                                   
db.auth_permission.insert(group_id = group_id,
                        name = "admin",
                        table_name = "patient",
                        record_id = fake_patient_id
                        )
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

# and a fake file for this patient
fake_file_id = db.sequence_file.insert(sampling_date="1903-02-02",
                                    info="plop",
                                    pcr="plop",
                                    sequencer="plop",
                                    producer="plop",
                                    patient_id=fake_patient_id,
                                    filename="plop",
                                    provider=user_id)

# and a fake result for this file
stream = open("../../doc/analysis-example.vidjil", 'rb')
fake_result_id = db.results_file.insert(sequence_file_id = fake_file_id,
                                    config_id = fake_config_id,
                                    run_date = "2014-09-19 00:00:00",
                                    data_file = db.results_file.data_file.store(stream, "plop.data")
                                    )
Marc Duez's avatar
Marc Duez committed
134
stream.seek(0)
135
136
137
138
139
140
fake_fused_id = db.fused_file.insert(patient_id = fake_patient_id,
                                    config_id = fake_config_id,
                                    fuse_date = "2014-09-19 00:00:00",
                                    fused_file = db.fused_file.fused_file.store(stream, "plop.data")
                                    )
db.commit()
Marc Duez's avatar
Marc Duez committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187


#fake log to avoid polluting log
#
'''
def _init_log2():
    import logging

    logger = logging.getLogger('vidjil') # (request.application)
    if not logger.handlers:
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('[%(process)d] %(asctime)s %(levelname)8s - %(filename)s:%(lineno)d\t%(message)s')

        handler = logging.FileHandler('applications/vidjil/tests/vidjil-debug.log')
        handler.setLevel(logging.DEBUG)
        handler.setFormatter(formatter)
        logger.addHandler(handler) 

        handler = logging.FileHandler('applications/vidjil/tests/vidjil.log')
        handler.setLevel(logging.INFO)
        handler.setFormatter(formatter)
        logger.addHandler(handler) 

        logger.info("Creating logger")
    return MsgUserAdapter(logger, {})

log = _init_log2()
'''




suite = unittest.TestSuite()

def showfeedback():
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print '-'*60
    for line in traceback.format_exception(exc_type, exc_value,exc_traceback):
        print line[:-1]
    print '-'*60


def custom_execfile(test_file):
    try:
        sys.path.append(os.path.split(test_file)[0]) # to support imports form current folder in the testfiles
        g=copy(globals())
        execfile(test_file, g) 
188
    except (OSError,ValueError,SystemExit):
Marc Duez's avatar
Marc Duez committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
        pass # we know about the rotating logger error...
             # and SystemExit is not useful to detect
    except:
        showfeedback()
    return g

appname = 'vidjil'






# find doctests in controller
path=os.path.join('applications',appname,'controllers','*.py')
doc_test_files = glob.glob(path)
print len(doc_test_files)," controller files with possible doctests found."

# get all files with tests
test_files = glob.glob('applications/'+appname+'/tests/*/*.py')

if not len(test_files):
    raise Exception("No files found for app: " + sys.argv[2])

# Bring in all doc tests and submit them
print "Run doctests" if doc_test_files else "No doctests"
for f in  doc_test_files:
    g=custom_execfile(f)
    suite.addTest(doctest.DocFileSuite(f, globs=g,
            module_relative=False))
    
# Bring all unit tests in and their controllers/models/whatever
for test_file in test_files:
    execfile(test_file, globals())

    # Create the appropriate class name based on filename and path
    filename =  str.capitalize(test_file.split("/")[-1][:-3])
    directory =  str.capitalize(test_file.split("/")[-2][:-1])

    suite.addTest(unittest.makeSuite(globals()[filename+directory]))

230
231
#unittest.TextTestRunner(verbosity=2).run(suite)
xmlrunner.XMLTestRunner(output='test-reports', verbosity=1).run(suite)