-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathharvest.py
153 lines (123 loc) · 6.07 KB
/
harvest.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
# <Lettuce - Behaviour Driven Development for python>
# Copyright (C) <2010-2012> Gabriel Falcão <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
from optparse import make_option
from django.conf import settings
from django.core.management.base import BaseCommand
from django.test.utils import setup_test_environment
from django.test.utils import teardown_test_environment
from lettuce import Runner
from lettuce import registry
from lettuce.django.server import Server
from lettuce.django import harvest_lettuces
from lettuce.django.server import LettuceServerException
class Command(BaseCommand):
help = u'Run lettuce tests all along installed apps'
args = '[PATH to feature file or folder]'
requires_model_validation = False
option_list = BaseCommand.option_list[1:] + (
make_option('-v', '--verbosity', action='store', dest='verbosity', default='4',
type='choice', choices=map(str, range(5)),
help='Verbosity level; 0=no output, 1=only dots, 2=only scenario names, 3=colorless output, 4=normal output (colorful)'),
make_option('-a', '--apps', action='store', dest='apps', default='',
help='Run ONLY the django apps that are listed here. Comma separated'),
make_option('-A', '--avoid-apps', action='store', dest='avoid_apps', default='',
help='AVOID running the django apps that are listed here. Comma separated'),
make_option('-S', '--no-server', action='store_true', dest='no_server', default=False,
help="will not run django's builtin HTTP server"),
make_option('-P', '--port', type='int', dest='port',
help="the port in which the HTTP server will run at"),
make_option('-d', '--debug-mode', action='store_true', dest='debug', default=False,
help="when put together with builtin HTTP server, forces django to run with settings.DEBUG=True"),
make_option('-s', '--scenarios', action='store', dest='scenarios', default=None,
help='Comma separated list of scenarios to run'),
make_option("-t", "--tag",
dest="tags",
type="str",
action='append',
default=None,
help='Tells lettuce to run the specified tags only; '
'can be used multiple times to define more tags'
'(prefixing tags with "-" will exclude them and '
'prefixing with "~" will match approximate words)'),
make_option('--with-xunit', action='store_true', dest='enable_xunit', default=False,
help='Output JUnit XML test results to a file'),
make_option('--xunit-file', action='store', dest='xunit_file', default=None,
help='Write JUnit XML to this file. Defaults to lettucetests.xml'),
)
def stopserver(self, failed=False):
raise SystemExit(int(failed))
def get_paths(self, args, apps_to_run, apps_to_avoid):
if args:
for path, exists in zip(args, map(os.path.exists, args)):
if not exists:
sys.stderr.write("You passed the path '%s', but it does not exist.\n" % path)
sys.exit(1)
else:
paths = args
else:
paths = harvest_lettuces(apps_to_run, apps_to_avoid) # list of tuples with (path, app_module)
return paths
def handle(self, *args, **options):
setup_test_environment()
settings.DEBUG = options.get('debug', False)
verbosity = int(options.get('verbosity', 4))
apps_to_run = tuple(options.get('apps', '').split(","))
apps_to_avoid = tuple(options.get('avoid_apps', '').split(","))
run_server = not options.get('no_server', False)
tags = options.get('tags', None)
server = Server(port=options['port'])
paths = self.get_paths(args, apps_to_run, apps_to_avoid)
if run_server:
try:
server.start()
except LettuceServerException as e:
raise SystemExit(e)
os.environ['SERVER_NAME'] = server.address
os.environ['SERVER_PORT'] = str(server.port)
failed = False
registry.call_hook('before', 'harvest', locals())
results = []
try:
for path in paths:
app_module = None
if isinstance(path, tuple) and len(path) is 2:
path, app_module = path
if app_module is not None:
registry.call_hook('before_each', 'app', app_module)
runner = Runner(path, options.get('scenarios'), verbosity,
enable_xunit=options.get('enable_xunit'),
xunit_filename=options.get('xunit_file'),
tags=tags)
result = runner.run()
if app_module is not None:
registry.call_hook('after_each', 'app', app_module, result)
results.append(result)
if not result or result.steps != result.steps_passed:
failed = True
except SystemExit as e:
failed = e.code
except Exception as e:
failed = True
import traceback
traceback.print_exc(e)
finally:
registry.call_hook('after', 'harvest', results)
server.stop(failed)
teardown_test_environment()
raise SystemExit(int(failed))