From 3f99f38e7e16d327577454d51a4061111e3655ab Mon Sep 17 00:00:00 2001 From: Luciano Pacheco Date: Thu, 23 Feb 2012 15:25:05 +1100 Subject: [PATCH] fixed (and refactored) the Jenkins integration; before it wasn't using the Jenkins runner collection of tests to run, which caused to run all tests as the default Django, JenkinsRunner runs just tests specified in PROJECT_APPS settings.\n Refactored the addition of seltests to a method.\n Fixed the Ctrl+C in jenkins command to stop the selenium and runserver servers --- AUTHORS | 2 +- django_selenium/jenkins_runner.py | 21 ++++++++++++++++++--- django_selenium/selenium_runner.py | 30 +++++++++++++++++++----------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/AUTHORS b/AUTHORS index d5f6dc2..caa8722 100644 --- a/AUTHORS +++ b/AUTHORS @@ -2,4 +2,4 @@ Authors: Vladimir Kuznetsov, Roman Prokofyev Contributors (in the order of first commit): -Alexander Chuklin, Adrien Lemaire, Daniel Boczek +Alexander Chuklin, Adrien Lemaire, Daniel Boczek, Luciano Pacheco (lucmult) diff --git a/django_selenium/jenkins_runner.py b/django_selenium/jenkins_runner.py index 51c3fa2..4ecf4a6 100644 --- a/django_selenium/jenkins_runner.py +++ b/django_selenium/jenkins_runner.py @@ -1,4 +1,6 @@ +from django.dispatch import receiver from django_jenkins.runner import CITestSuiteRunner +from django_jenkins.signals import build_suite from django_selenium.selenium_runner import SeleniumTestRunner class JenkinsTestRunner(CITestSuiteRunner, SeleniumTestRunner): @@ -7,13 +9,26 @@ def __init__(self, **kwargs): self.selenium = True def build_suite(self, test_labels, **kwargs): - suite = SeleniumTestRunner.build_suite(self, test_labels, **kwargs) + # args and kwargs saved in instance to use in the signal below + self.test_labels = test_labels + self.build_suite_kwargs = kwargs + suite = CITestSuiteRunner.build_suite(self, test_labels, **kwargs) return suite def run_tests(self, test_labels, extra_tests=None, **kwargs): self._start_selenium() - results = super(JenkinsTestRunner, self).run_tests(test_labels, extra_tests, **kwargs) - self._stop_selenium() + try: + results = super(JenkinsTestRunner, self).run_tests(test_labels, extra_tests, **kwargs) + finally: + self._stop_selenium() return results + + +@receiver(build_suite) +def add_selenium_tests(sender, suite, **kwargs): + ''' Add the selenium test under Jenkins environment ''' + sel_suite = sender._get_seltests(sender.test_labels, **sender.build_suite_kwargs) + suite.addTest(sel_suite) + diff --git a/django_selenium/selenium_runner.py b/django_selenium/selenium_runner.py index aa67133..f4e4ed6 100755 --- a/django_selenium/selenium_runner.py +++ b/django_selenium/selenium_runner.py @@ -6,6 +6,7 @@ import unittest from django_selenium import settings +import django.test.simple from django.test.simple import reorder_suite from django.test.testcases import TestCase from django_selenium.selenium_server import start_test_server @@ -57,11 +58,11 @@ def __init__(self, **kwargs): def _is_start_selenium_server(self): return bool((settings.SELENIUM_DRIVER == 'Remote') and settings.SELENIUM_PATH) - def build_suite(self, test_labels, extra_tests=None, **kwargs): + def build_suite(self, test_labels, *args, **kwargs): suite = unittest.TestSuite() if not self.selenium_only: - suite = super(SeleniumTestRunner, self).build_suite(test_labels, extra_tests, **kwargs) + suite = super(SeleniumTestRunner, self).build_suite(test_labels, *args, **kwargs) if self.selenium: # Hack to exclude doctests from selenium-only, they are already present @@ -72,18 +73,25 @@ def build_suite(self, test_labels, extra_tests=None, **kwargs): app = get_app(label) setattr(app, 'suite', unittest.TestSuite) - # Add tests from seltests.py modules - import django.test.simple - orig_test_module = django.test.simple.TEST_MODULE - django.test.simple.TEST_MODULE = SELTEST_MODULE - try: - sel_suite = super(SeleniumTestRunner, self).build_suite(test_labels, extra_tests, **kwargs) - suite.addTest(sel_suite) - finally: - django.test.simple.TEST_MODULE = orig_test_module + + sel_suite = self._get_seltests(test_labels, *args, **kwargs) + suite.addTest(sel_suite) return reorder_suite(suite, (TestCase,)) + def _get_seltests(self, *args, **kwargs): + # Add tests from seltests.py modules + import django.test.simple + orig_test_module = django.test.simple.TEST_MODULE + django.test.simple.TEST_MODULE = SELTEST_MODULE + try: + sel_suite = DjangoTestSuiteRunner.build_suite(self, *args, **kwargs) + finally: + django.test.simple.TEST_MODULE = orig_test_module + + return sel_suite + + def _start_selenium(self): if self.selenium: