def run(self, ignore_case): """Runs a scenario, running each of its steps. Also call before_each and after_each callbacks for steps and scenario""" results = [] call_hook('before_each', 'scenario', self) def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all( self.steps, outline, run_callbacks, ignore_case) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) return ScenarioResult(self, steps_passed, steps_failed, steps_skipped, steps_undefined) if self.outlines: first = True for index, outline in enumerate(self.outlines): results.append( run_scenario(self, index, outline, run_callbacks=first)) first = False else: results.append(run_scenario(self, run_callbacks=True)) call_hook('after_each', 'scenario', self) return results
def run(self, ignore_case): """Runs a scenario, running each of its steps. Also call before_each and after_each callbacks for steps and scenario""" results = [] call_hook('before_each', 'scenario', self) def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all(self.steps, outline, run_callbacks, ignore_case) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook( 'outline', 'scenario', self, order, outline, reasons_to_fail ) return ScenarioResult( self, steps_passed, steps_failed, steps_skipped, steps_undefined ) if self.outlines: first = True for index, outline in enumerate(self.outlines): results.append(run_scenario(self, index, outline, run_callbacks=first)) first = False else: results.append(run_scenario(self, run_callbacks=True)) call_hook('after_each', 'scenario', self) return results
def start(self): """Starts the webserver thread, and waits it to be available""" call_hook('before', 'runserver', self._actual_server) try: # If we're in 1.4+, just use the built in LiveServer in django. from django.test.testcases import LiveServerTestCase os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = "%s:%s" % ( self.address, self.port, ) addrport = self.address, self.port LiveServerTestCase.setUpClass() except: if self._actual_server.should_serve_admin_media(): msg = "Preparing to serve django's admin site static files" if getattr(settings, 'LETTUCE_SERVE_ADMIN_MEDIA', False): msg += ' (as per settings.LETTUCE_SERVE_ADMIN_MEDIA=True)' print "%s..." % msg self._actual_server.start() self._actual_server.wait() addrport = self.address, self._actual_server.port if not self._actual_server.is_alive(): raise LettuceServerException( 'Lettuce could not run the builtin Django server at %s:%d"\n' 'maybe you forgot a "runserver" instance running ?\n\n' 'well if you really do not want lettuce to run the server ' 'for you, then just run:\n\n' 'python manage.py --no-server' % addrport, ) print "Django's builtin server is running at %s:%d" % addrport
def run(self): """ Find and load step definitions, and them find and load features under `base_path` specified on constructor """ started_at = datetime.now() self.loader.find_and_load_step_definitions() call_hook('before', 'all') results = [] if self.single_feature: features_files = [self.single_feature] else: features_files = self.loader.find_feature_files() if not features_files: self.output.print_no_features_found(self.loader.base_dir) return failed = False try: for filename in features_files: feature = Feature.from_file(filename) results.append(feature.run(self.scenarios)) except LettuceSyntaxError, e: sys.stderr.write(e.msg) failed = True
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): try: if self.background: self.background.run(ignore_case) all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all(self.steps, outline, run_callbacks, ignore_case, failfast=failfast) except: if failfast: call_hook('after_each', 'scenario', self) raise skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) return ScenarioResult( self, steps_passed, steps_failed, steps_skipped, steps_undefined )
def run_all(steps, outline = None, run_callbacks = False, ignore_case = True): """Runs each step in the given list of steps. Returns a tuple of five lists: - The full set of steps executed - The steps that passed - The steps that failed - The steps that were undefined - The reason for each failing step (indices matching per above) """ all_steps = [] steps_passed = [] steps_failed = [] steps_undefined = [] reasons_to_fail = [] for step in steps: if outline: step = step.solve_and_clone(outline) try: step.pre_run(ignore_case, with_outline=outline) if run_callbacks: call_hook('before_each', 'step', step) if not steps_failed and not steps_undefined: step.run(ignore_case) steps_passed.append(step) except NoDefinitionFound, e: steps_undefined.append(e.step) except:
def start(self): """ Starts the webserver and waits it to be available Chain this method up before your implementation """ call_hook('before', 'runserver', self._server)
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): all_steps = [] steps_passed = [] steps_failed = [] steps_pending = [] steps_undefined = [] reasons_to_fail = [] for step in self.steps: if outline: step = step.solve_and_clone(outline) try: step.pre_run(ignore_case, with_outline=outline) if run_callbacks: call_hook('before_each', 'step', step) if not steps_failed and not steps_undefined and not steps_pending: step.run(ignore_case) steps_passed.append(step) except NoDefinitionFound, e: steps_undefined.append(e.step) except PendingStepDefinition, e: steps_pending.append(step)
def run_scenario(almost_self, run_controller, order=-1, outline=None, run_callbacks=False): if run_controller: this_scenario_id = run_controller.get_next_scenario_id() if not run_controller.is_to_run_scenario(self, this_scenario_id): return ScenarioResult( self, [], [], [], [], True, this_scenario_id ) else: this_scenario_id = -1 all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all(self.steps, run_controller, outline, run_callbacks, ignore_case) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook( 'outline', 'scenario', self, order, outline, reasons_to_fail ) return ScenarioResult( self, steps_passed, steps_failed, steps_skipped, steps_undefined, False, this_scenario_id )
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): try: if outline: self._report_outline_hook(outline, True) if self.background: self.background.run(ignore_case) reasons_to_fail = [] all_steps, steps_passed, steps_failed, steps_undefined = Step.run_all(self.steps, outline, run_callbacks, ignore_case, failfast=failfast, display_steps=(order < 1), reasons_to_fail=reasons_to_fail) except: if outline: # Can't use "finally" here since we need to call it before after_each_scenario self._report_outline_hook(outline, False) call_hook('after_each', 'scenario', self) raise finally: if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) if outline: self._report_outline_hook(outline, False) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) return ScenarioResult( self, all_steps, steps_passed, steps_failed, steps_skipped, steps_undefined, outline )
def start(self): """Starts the webserver thread, and waits it to be available""" call_hook('before', 'runserver', self._actual_server) try: # If we're in 1.4+, just use the built in LiveServer in django. from django.test.testcases import LiveServerTestCase os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = "%s:%s" % (self.address, self.port,) addrport = self.address, self.port LiveServerTestCase.setUpClass() except: if self._actual_server.should_serve_admin_media(): msg = "Preparing to serve django's admin site static files" if getattr(settings, 'LETTUCE_SERVE_ADMIN_MEDIA', False): msg += ' (as per settings.LETTUCE_SERVE_ADMIN_MEDIA=True)' print "%s..." % msg self._actual_server.start() self._actual_server.wait() addrport = self.address, self._actual_server.port if not self._actual_server.is_alive(): raise LettuceServerException( 'Lettuce could not run the builtin Django server at %s:%d"\n' 'maybe you forgot a "runserver" instance running ?\n\n' 'well if you really do not want lettuce to run the server ' 'for you, then just run:\n\n' 'python manage.py --no-server' % addrport, ) print "Django's builtin server is running at %s:%d" % addrport
def run(self, scenarios=None, ignore_case=True, tags=None, random=False): call_hook('before_each', 'feature', self) scenarios_ran = [] if random: shuffle(self.scenarios) if isinstance(scenarios, (tuple, list)): if all(map(lambda x: isinstance(x, int), scenarios)): scenarios_to_run = scenarios else: scenarios_to_run = range(1, len(self.scenarios) + 1) for index, scenario in enumerate(self.scenarios): if scenarios_to_run and (index + 1) not in scenarios_to_run: continue if not scenario.matches_tags(tags): continue if self.background: self.background.run(ignore_case) scenarios_ran.extend(scenario.run(ignore_case)) call_hook('after_each', 'feature', self) return FeatureResult(self, *scenarios_ran)
def runContinuously(self, scenarios=None, ignore_case=False, *args, **kwargs): ''' By default Lettuce Feature.run() stops after one of its scenarios has failed. We don't want that since it would require defining features for every single scenario we want to run. This featureRunner simply ignores failing scenarios. ''' scenarios_ran = [] # If no scenarios in this feature will run, don't run the feature hooks. if not self.scenarios: return lettuce.core.FeatureResult(self) call_hook('before_each', 'feature', self) # Modified Feature.run() # We try in a loop-statement, instead of looping in a try-statement for scenario in self.scenarios: try: scenarios_ran.extend(scenario.run(ignore_case, failfast=False)) except: pass finally: call_hook('after_each', 'feature', self) if lettuce.world.exit: break return lettuce.core.FeatureResult(self, *scenarios_ran)
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): try: if self.background: self.background.run(ignore_case) reasons_to_fail = [] all_steps, steps_passed, steps_failed, steps_undefined = Step.run_all( self.steps, outline, run_callbacks, ignore_case, failfast=failfast, display_steps=(order < 1), reasons_to_fail=reasons_to_fail, ) except: call_hook("after_each", "scenario", self) raise finally: if outline: call_hook("outline", "scenario", self, order, outline, reasons_to_fail) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) return ScenarioResult(self, steps_passed, steps_failed, steps_skipped, steps_undefined)
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): try: if self.background: self.background.run(ignore_case) all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all( self.steps, outline, run_callbacks, ignore_case, failfast=failfast) except: if failfast: call_hook('after_each', 'scenario', self) raise skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) return ScenarioResult(self, steps_passed, steps_failed, steps_skipped, steps_undefined)
def stop(self, fail=False): if self._actual_server: self._actual_server.join() code = int(fail) call_hook('after', 'runserver', self._actual_server) return sys.exit(code)
def run(self, ignore_case): """Runs a scenario, running each of its steps. Also call before_each and after_each callbacks for steps and scenario""" results = [] call_hook('before_each', 'scenario', self) if self.outlines: first = True for index, outline in enumerate(self.outlines): if self.background: results.append(self.background.run_scenario(ignore_case)) results.append( self.run_scenario(ignore_case, outline=outline, order=index, run_callbacks=first)) first = False else: if self.background: results.append(self.background.run_scenario(ignore_case)) results.append(self.run_scenario(ignore_case, run_callbacks=True)) call_hook('after_each', 'scenario', self) return results
def run(self, verbosity, pattern, start_dir): """ Harvest all Lettuce features from the current application """ paths = self.get_path(start_dir, pattern) results = [] failed = False app_factory = self.app_factory @before.each_scenario def setup_scenario(feature): world.app = app_factory() world.client = world.app.test_client() @after.each_scenario def teardown_scenario(scenario): del world.client del world.app registry.call_hook('before', 'harvest', locals()) try: for path in paths: runner = Runner(path, verbosity=verbosity) result = runner.run() results.append(result) if not result or result.steps != result.steps_passed: failed = True except Exception, e: import traceback traceback.print_exc(e)
def run(self): """ Find and load step definitions, and them find and load features under `base_path` specified on constructor """ results = [] if self.single_feature: features_files = [self.single_feature] else: features_files = self.loader.find_feature_files() if self.random: random.shuffle(features_files) if not features_files: self.output.print_no_features_found(self.loader.base_dir) return # only load steps if we've located some features. # this prevents stupid bugs when loading django modules # that we don't even want to test. try: self.loader.find_and_load_step_definitions() except StepLoadingError as e: print("Error loading step definitions:\n", e) return call_hook('before', 'all') failed = False try: for filename in features_files: feature = Feature.from_file(filename) results.append( feature.run(self.scenarios, tags=self.tags, random=self.random, failfast=self.failfast)) except exceptions.LettuceSyntaxError as e: sys.stderr.write(e.msg) failed = True except: if not self.failfast: e = sys.exc_info()[1] print("Died with %s" % str(e)) traceback.print_exc() else: print("Lettuce aborted running any more tests " "because was called with the `--failfast` option") failed = True finally: total = TotalResult(results) total.output_format() call_hook('after', 'all', total) if failed: raise SystemExit(2) return total
def run(self): """ Find and load step definitions, and them find and load features under `base_path` specified on constructor """ started_at = datetime.now() self.loader.find_and_load_step_definitions() call_hook('before', 'all') results = [] if self.single_feature: features_files = [self.single_feature] else: features_files = self.loader.find_feature_files() if not features_files: self.output.print_no_features_found(self.loader.base_dir) return failed = False try: for filename in features_files: feature = Feature.from_file(filename) results.append(feature.run(self.scenarios)) except exceptions.LettuceSyntaxError, e: sys.stderr.write(e.msg) failed = True
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), 'lettuce-django.pid') if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, 'w').write(unicode(os.getpid())) bound = False max_port = 65535 connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while not bound or self.port < max_port: try: connector.connect((self.address, self.port)) self.port += 1 except socket.error: bound = True break if bound: try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) bound = True except WSGIServerException: bound = False if not bound: raise LettuceServerException( "the port %d already being used, could not start " \ "django's builtin server on it" % self.port ) handler = StopabbleHandler(WSGIHandler()) if 'django.contrib.admin' in settings.INSTALLED_APPS: admin_media_path = '' handler = AdminMediaHandler(handler, admin_media_path) print "Preparing to server django's admin site static files..." httpd.set_app(handler) global keep_running while keep_running: call_hook('before', 'handle_request', httpd, self) httpd.handle_request() call_hook('after', 'handle_request', httpd, self) if self.lock.locked(): self.lock.release()
def stop(self, fail=False): pid = self._actual_server.pid if pid: os.kill(pid, 9) code = int(fail) call_hook('after', 'runserver', self._actual_server) return sys.exit(code)
def stop(self): """ Stops the webserver Chain this method up after your implementation """ call_hook('after', 'runserver', self._server)
def _report_outline_hook(self, outline, started): """ Function called before each outline and after each outline to provide hooks :param outline: dict with examples row :type outline dict :param started: is outline started or finished :type started bool """ call_hook('before_each' if started else 'after_each', 'outline', self, outline)
def start(self): """Starts the webserver thread, and waits it to be available""" call_hook('before', 'runserver', self._actual_server) self._actual_server.setDaemon(True) self._actual_server.start() self._actual_server.wait() addrport = self.address, self._actual_server.port print "Django's builtin server is running at %s:%d" % addrport
def teardown_test_environment(self, **kwargs): # The after.harvest hook should really be called just after finishing # Lettuce tests. Now it gets run after all tests, including # non-Lettuce tests. Also, in Lettuce's "harvest" Django management # command, the list of test results is passed to the hook, and it # should probably be recreated here instead of passing an empty list. registry.call_hook('after', 'harvest', []) if self.lettuce_server: self.server.stop()
def run(self): self.lock.acquire() pidfile = os.path.join(tempfile.gettempdir(), 'lettuce-django.pid') if os.path.exists(pidfile): pid = int(open(pidfile).read()) try: os.kill(pid, 9) except OSError: pass finally: os.unlink(pidfile) open(pidfile, 'w').write(unicode(os.getpid())) connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s = connector.connect((self.address, self.port)) self.lock.release() os.kill(os.getpid(), 9) except socket.error: pass finally: self.lock.release() try: server_address = (self.address, self.port) httpd = WSGIServer(server_address, MutedRequestHandler) except WSGIServerException: raise LettuceServerException( "the port %d already being used, could not start " \ "django's builtin server on it" % self.port, ) handler = WSGIHandler() if self.should_serve_admin_media(): admin_media_path = '' handler = AdminMediaHandler(handler, admin_media_path) if self.should_serve_static_files(): handler = StaticFilesHandler(handler) httpd.set_app(handler) global keep_running while keep_running: call_hook('before', 'handle_request', httpd, self) httpd.handle_request() call_hook('after', 'handle_request', httpd, self) try: self.lock.release() except ValueError: pass
def stop(self): http = httplib.HTTPConnection(self.address, self.port) try: http.request("DELETE", "/") http.getresponse().read() except socket.error: pass finally: http.close() call_hook('after', 'runserver', self._actual_server)
def setup_test_environment(self, **kwargs): if self.lettuce_server: from lettuce.django import server self.server = server self.server.start() # The before.harvest hook should really be called just before starting # to run Lettuce tests. Now it gets run before all tests, including # non-Lettuce tests. Also, in Lettuce's "harvest" Django management # command, various local variables are passed to the hook, and those # should probably be recreated here instead of passing an empty dict. registry.call_hook('before', 'harvest', {})
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all( self.steps, outline, run_callbacks, ignore_case ) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook("outline", "scenario", self, order, outline, reasons_to_fail) return ScenarioResult(self, steps_passed, steps_failed, steps_skipped, steps_undefined)
def run(self, ignore_case, failfast=False): """Runs a scenario, running each of its steps. Also call before_each and after_each callbacks for steps and scenario""" results = [] call_hook('before_each', 'scenario', self) def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): try: if outline: self._report_outline_hook(outline, True) if self.background: self.background.run(ignore_case) reasons_to_fail = [] all_steps, steps_passed, steps_failed, steps_undefined = Step.run_all( self.steps, outline, run_callbacks, ignore_case, failfast=failfast, display_steps=(order < 1), reasons_to_fail=reasons_to_fail) except: if outline: # Can't use "finally" here since we need to call it before after_each_scenario self._report_outline_hook(outline, False) call_hook('after_each', 'scenario', self) raise finally: if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) if outline: self._report_outline_hook(outline, False) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) return ScenarioResult(self, steps_passed, steps_failed, steps_skipped, steps_undefined) if self.outlines: for index, outline in enumerate(self.outlines): results.append( run_scenario(self, index, outline, run_callbacks=True)) else: results.append(run_scenario(self, run_callbacks=True)) call_hook('after_each', 'scenario', self) return results
def stop(self, fail=False): http = httplib.HTTPConnection(self.address, self.port) try: http.request("DELETE", "/") http.getresponse().read() except socket.error: pass finally: http.close() code = int(fail) call_hook('after', 'runserver', self._actual_server) return sys.exit(code)
def run(self, ignore_case, failfast=False): """Runs a scenario, running each of its steps. Also call before_each and after_each callbacks for steps and scenario""" results = [] call_hook("before_each", "scenario", self) def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): total_attempts = 3 if "retry" in self.tags else 1 attempts = 0 while attempts < total_attempts: if attempts >= 1: call_hook("before_each", "scenario_retry", self) try: if self.background: self.background.run(ignore_case) all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all( self.steps, outline, run_callbacks, ignore_case, failfast=failfast ) except: if failfast: call_hook("after_each", "scenario", self) raise if all_steps == steps_passed: break new_steps = [] for step in self.original_steps: new_step = deepcopy(step) new_step.original_sentence = step.sentence new_step.scenario = self new_steps.append(new_step) self.steps = new_steps attempts += 1 skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook("outline", "scenario", self, order, outline, reasons_to_fail) return ScenarioResult(self, steps_passed, steps_failed, steps_skipped, steps_undefined) if self.outlines: first = True for index, outline in enumerate(self.outlines): results.append(run_scenario(self, index, outline, run_callbacks=first)) first = False else: results.append(run_scenario(self, run_callbacks=True)) call_hook("after_each", "scenario", self) return results
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): call_hook('before_each', 'scenario', self) scenario_begin_time = datetime.utcnow() try: if self.background: self.background.run(ignore_case) reasons_to_fail = [] all_steps, steps_passed, steps_failed, steps_undefined = Step.run_all(self.steps, outline, run_callbacks, ignore_case, failfast=failfast, display_steps=(order < 1), reasons_to_fail=reasons_to_fail) except: call_hook('after_each', 'scenario', self) raise finally: call_hook('after_each', 'scenario', self) if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) elapsed_time = datetime.utcnow() - scenario_begin_time return ScenarioResult( self, steps_passed, steps_failed, steps_skipped, steps_undefined, elapsed_time, outline )
def run_all( steps, outline=None, run_callbacks=False, ignore_case=True, failfast=False, display_steps=True, reasons_to_fail=None, ): """Runs each step in the given list of steps. Returns a tuple of five lists: - The full set of steps executed - The steps that passed - The steps that failed - The steps that were undefined - The reason for each failing step (indices matching per above) """ all_steps = [] steps_passed = [] steps_failed = [] steps_undefined = [] if reasons_to_fail is None: reasons_to_fail = [] for step in steps: if outline: step = step.solve_and_clone(outline, display_steps) try: step.pre_run(ignore_case, with_outline=outline) if run_callbacks: call_hook("before_each", "step", step) call_hook("before_output", "step", step) if not steps_failed and not steps_undefined: step.run(ignore_case) steps_passed.append(step) except NoDefinitionFound, e: steps_undefined.append(e.step) except Exception, e: steps_failed.append(step) reasons_to_fail.append(step.why) if failfast: raise
def run(self, ignore_case, failfast=False): """Runs a scenario, running each of its steps. Also call before_each and after_each callbacks for steps and scenario""" results = [] call_hook('before_each', 'scenario', self) def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): try: if outline: self._report_outline_hook(outline, True) if self.background: self.background.run(ignore_case) reasons_to_fail = [] all_steps, steps_passed, steps_failed, steps_undefined = Step.run_all(self.steps, outline, run_callbacks, ignore_case, failfast=failfast, display_steps=(order < 1), reasons_to_fail=reasons_to_fail) except: if outline: # Can't use "finally" here since we need to call it before after_each_scenario self._report_outline_hook(outline, False) call_hook('after_each', 'scenario', self) raise finally: if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) if outline: self._report_outline_hook(outline, False) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) return ScenarioResult( self, all_steps, steps_passed, steps_failed, steps_skipped, steps_undefined, outline ) if self.outlines: for index, outline in enumerate(self.outlines): results.append(run_scenario(self, index, outline, run_callbacks=True)) else: results.append(run_scenario(self, run_callbacks=True)) call_hook('after_each', 'scenario', self) return results
def start(self): """Starts the webserver thread, and waits it to be available""" call_hook("before", "runserver", self._actual_server) if self._actual_server.should_serve_admin_media(): msg = "Preparing to serve django's admin site static files" if getattr(settings, "LETTUCE_SERVE_ADMIN_MEDIA", False): msg += " (as per settings.LETTUCE_SERVE_ADMIN_MEDIA=True)" print "%s..." % msg self._actual_server.start() self._actual_server.wait() addrport = self.address, self._actual_server.port print "Django's builtin server is running at %s:%d" % addrport
def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False): all_steps, steps_passed, steps_failed, steps_undefined, reasons_to_fail = Step.run_all( self.steps, outline, run_callbacks, ignore_case) skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed steps_skipped = filter(skip, all_steps) if outline: call_hook('outline', 'scenario', self, order, outline, reasons_to_fail) return ScenarioResult(self, steps_passed, steps_failed, steps_skipped, steps_undefined)
def run(self): """ Find and load step definitions, and them find and load features under `base_path` specified on constructor """ results = [] if self.single_feature: features_files = [self.single_feature] else: features_files = self.loader.find_feature_files() if self.random: random.shuffle(features_files) if not features_files: self.output.print_no_features_found(self.loader.base_dir) return world.port_number = 8181 # only load steps if we've located some features. # this prevents stupid bugs when loading django modules # that we don't even want to test. try: self.loader.find_and_load_step_definitions() except StepLoadingError as e: print "Error loading step definitions:\n", e return call_hook('before', 'all') call_hook('before', 'batch') begin_time = datetime.utcnow() failed = False try: for filename in features_files: feature = Feature.from_file(filename) results.append( feature.run(self.scenarios, tags=self.tags, random=self.random, failfast=self.failfast)) except exceptions.LettuceSyntaxError as e: sys.stderr.write(e.msg) failed = True except exceptions.NoDefinitionFound, e: sys.stderr.write(e.msg) failed = True
def run_all(steps, outline=None, run_callbacks=False, ignore_case=True, failfast=False, display_steps=True, reasons_to_fail=None): """Runs each step in the given list of steps. Returns a tuple of five lists: - The full set of steps executed - The steps that passed - The steps that failed - The steps that were undefined - The reason for each failing step (indices matching per above) """ all_steps = [] steps_passed = [] steps_failed = [] steps_undefined = [] if reasons_to_fail is None: reasons_to_fail = [] for step in steps: if outline: step = step.solve_and_clone(outline, display_steps) try: step.pre_run(ignore_case, with_outline=outline) if run_callbacks: call_hook('before_each', 'step', step) call_hook('before_output', 'step', step) if not steps_failed and not steps_undefined: step.run(ignore_case) steps_passed.append(step) except NoDefinitionFound as e: steps_undefined.append(e.step) except Exception as e: steps_failed.append(step) reasons_to_fail.append(step.why) if failfast: raise finally: all_steps.append(step) call_hook('after_output', 'step', step) if run_callbacks: call_hook('after_each', 'step', step) return (all_steps, steps_passed, steps_failed, steps_undefined)
class Background(object): indentation = 2 def __init__(self, lines, feature, with_file=None, original_string=None, language=None): self.steps = map(self.add_self_to_step, Step.many_from_lines( lines, with_file, original_string)) self.feature = feature self.original_string = original_string self.language = language def add_self_to_step(self, step): step.background = self return step def run(self, ignore_case): call_hook('before_each', 'background', self) results = [] for step in self.steps: matched, step_definition = step.pre_run(ignore_case) call_hook('before_each', 'step', step) try: results.append(step.run(ignore_case)) except Exception, e: print e pass call_hook('after_each', 'step', step) call_hook('after_each', 'background', self, results) return results
def process_scenarios(scenario_queue, port_number, results, errors, interupted_event): # print "running batch with port number: {}".format(port_number) world.port_number = port_number call_hook('before', 'batch') while scenario_queue and not interupted_event.is_set(): try: scenario_to_execute = scenario_queue.pop(0) ignore_case = True result = scenario_to_execute.run(ignore_case, failfast=self.failfast) results.append(result) except pickle.PicklingError: print( "Pickling Error -- probably due to step functions having the same name." ) print() if result: print " Result: {}".format(result) import StringIO class MyPickler(pickle.Pickler): """Custom Picklier to give more detail on whats messing things up """ def save(self, obj): print(' pickling object', obj, 'of type', type(obj)) pickle.Pickler.save(self, obj) stringIO = StringIO.StringIO() pickler = MyPickler(stringIO) pickler.save(result) else: print " No Result" except Exception as e: print "Died with %s" % str(e) traceback.print_exc() errors.append(e) call_hook('after', 'batch')
def stop(self, fail=False): try: # If we're in 1.4+, just use the built in LiveServer in django. from django.test.testcases import LiveServerTestCase LiveServerTestCase.tearDownClass() code = 0 except: from traceback import print_exc print_exc() pid = self._actual_server.pid if pid: os.kill(pid, 9) code = int(fail) call_hook('after', 'runserver', self._actual_server) return code
def run(self, scenarios=None, ignore_case=True): call_hook('before_each', 'feature', self) scenarios_ran = [] if isinstance(scenarios, (tuple, list)): if all(map(lambda x: isinstance(x, int), scenarios)): scenarios_to_run = scenarios else: scenarios_to_run = range(1, len(self.scenarios) + 1) for index, scenario in enumerate(self.scenarios): if scenarios_to_run and (index + 1) not in scenarios_to_run: continue scenarios_ran.extend(scenario.run(ignore_case)) call_hook('after_each', 'feature', self) return FeatureResult(self, *scenarios_ran)
def worker_process(args): self, filename = args sys.stdout = StringIO() sys.stderr = StringIO() failed = False results = [] try: self.output call_hook('before', 'all') feature = Feature.from_file(filename) results.append( feature.run(self.scenarios, tags=self.tags, random=self.random, failfast=self.failfast)) except exceptions.LettuceSyntaxError, e: sys.stderr.write(e.msg) failed = True
def run(self, scenarios=None, ignore_case=True, tags=None, random=False, failfast=False): scenarios_ran = [] if random: shuffle(self.scenarios) scenarios_to_run = self.scenarios_to_run(self.scenarios, tags) # If no scenarios in this feature will run, don't run the feature hooks. if not scenarios_to_run: return FeatureResult(self) call_hook('before_each', 'feature', self) try: for scenario in scenarios_to_run: scenarios_ran.extend( scenario.run(ignore_case, failfast=failfast)) except: call_hook('after_each', 'feature', self) raise else: call_hook('after_each', 'feature', self) return FeatureResult(self, *scenarios_ran)
def run(self, scenarios=None, ignore_case=True, tags=None, random=False, failfast=False): scenarios_ran = [] if random: shuffle(self.scenarios) scenario_nums_to_run = None if isinstance(scenarios, (tuple, list)): if all(map(lambda x: isinstance(x, int), scenarios)): scenario_nums_to_run = scenarios def should_run_scenario(num, scenario): return scenario.matches_tags(tags) and \ (scenario_nums_to_run is None or num in scenario_nums_to_run) scenarios_to_run = [scenario for num, scenario in enumerate(self.scenarios, start=1) if should_run_scenario(num, scenario)] # If no scenarios in this feature will run, don't run the feature hooks. if not scenarios_to_run: return FeatureResult(self) call_hook('before_each', 'feature', self) try: for scenario in scenarios_to_run: scenarios_ran.extend(scenario.run(ignore_case, failfast=failfast)) except: call_hook('after_each', 'feature', self) raise else: call_hook('after_each', 'feature', self) return FeatureResult(self, *scenarios_ran)
def start(self): """Starts the webserver thread, and waits it to be available""" call_hook('before', 'runserver', self._actual_server) if self._actual_server.should_serve_admin_media(): msg = "Preparing to serve django's admin site static files" if getattr(settings, 'LETTUCE_SERVE_ADMIN_MEDIA', False): msg += ' (as per settings.LETTUCE_SERVE_ADMIN_MEDIA=True)' print "%s..." % msg self._actual_server.start() self._actual_server.wait() addrport = self.address, self._actual_server.port if not self._actual_server.is_alive(): raise LettuceServerException( 'Lettuce could not run the builtin Django server at %s:%d"\n' 'maybe you forgot a "runserver" instance running ?\n\n' 'well if you really do not want lettuce to run the server ' 'for you, then just run:\n\n' 'python manage.py --no-server' % addrport, ) print "Django's builtin server is running at %s:%d" % addrport