Skip to content

Instantly share code, notes, and snippets.

@thachhoang
Forked from carljm/runner.py
Created February 8, 2014 15:56
Show Gist options
  • Select an option

  • Save thachhoang/8885808 to your computer and use it in GitHub Desktop.

Select an option

Save thachhoang/8885808 to your computer and use it in GitHub Desktop.

Revisions

  1. @carljm carljm revised this gist May 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion runner.py
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@



    class DiscoveryDjangoTestSuiteRunner(DjangoTestSuiteRunner):
    class DiscoveryRunner(DjangoTestSuiteRunner):
    """A test suite runner that uses unittest2 test discovery."""
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
    suite = None
  2. Carl Meyer revised this gist Mar 14, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion runner.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,8 @@
    package, and that package does not itself directly contain any tests, it'll do
    test discovery in all sub-modules of that package.
    This code doesn't modify the default unittest2 test discovery behavior, which only searches for tests in files named "test*.py".
    This code doesn't modify the default unittest2 test discovery behavior, which
    only searches for tests in files named "test*.py".
    """
    from django.conf import settings
  3. Carl Meyer revised this gist Mar 14, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions runner.py
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@
    package, and that package does not itself directly contain any tests, it'll do
    test discovery in all sub-modules of that package.
    This code doesn't modify the default unittest2 test discovery behavior, which only searches for tests in files named "test*.py".
    """
    from django.conf import settings
    from django.test import TestCase
  4. @carljm carljm revised this gist Mar 13, 2012. 2 changed files with 26 additions and 10 deletions.
    11 changes: 8 additions & 3 deletions runner.py
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,14 @@
    a base path specified in settings, rather than requiring all tests to be in
    ``tests`` module of an app.
    If you just run ``./manage.py test``, it'll discover and run all tests underneath the ``TEST_DISCOVERY_ROOT`` setting (a path). If you run ``./manage.py test full.dotted.path.to.test_module``, it'll run the tests in that module (you can also pass multiple modules).
    And (new in this updated version), if you give it a single dotted path to a package, and that package does not itself directly contain any tests, it'll do test discovery in all sub-modules of that package.
    If you just run ``./manage.py test``, it'll discover and run all tests
    underneath the ``TEST_DISCOVERY_ROOT`` setting (a path). If you run
    ``./manage.py test full.dotted.path.to.test_module``, it'll run the tests in
    that module (you can also pass multiple modules).
    And (new in this updated version), if you give it a single dotted path to a
    package, and that package does not itself directly contain any tests, it'll do
    test discovery in all sub-modules of that package.
    """
    from django.conf import settings
    25 changes: 18 additions & 7 deletions settings.py
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,31 @@
    """
    You need the ``BASE_PATH`` and ``TEST_DISCOVERY_ROOT`` settings in order for this test runner to work. ``BASE_PATH`` should be the directory containing your top-level package(s); in other words, the directory that should be on ``sys.path`` for your code to import. This is the directory containing ``manage.py`` in the new Django 1.4 project layout.
    You need the ``BASE_PATH`` and ``TEST_DISCOVERY_ROOT`` settings in order for
    this test runner to work.
    ``TEST_DISCOVERY_ROOT`` should be the root directory to discover tests within. You could make this the same as ``BASE_PATH`` if you want tests to be discovered anywhere in your project. If you want tests to only be discovered within, say, a top-level ``tests`` directory, you'd set ``TEST_DISCOVERY_ROOT`` as shown below.
    ``BASE_PATH`` should be the directory containing your top-level package(s); in
    other words, the directory that should be on ``sys.path`` for your code to
    import. This is the directory containing ``manage.py`` in the new Django 1.4
    project layout.
    And you need to point the ``TEST_RUNNER`` setting to the ``DiscoveryRunner`` class above.
    ``TEST_DISCOVERY_ROOT`` should be the root directory to discover tests
    within. You could make this the same as ``BASE_PATH`` if you want tests to be
    discovered anywhere in your project. If you want tests to only be discovered
    within, say, a top-level ``tests`` directory, you'd set ``TEST_DISCOVERY_ROOT``
    as shown below.
    And you need to point the ``TEST_RUNNER`` setting to the ``DiscoveryRunner``
    class above.
    """
    import os.path

    # This is correct for the Django 1.4-style project layout; for the old-style project layout
    # with ``settings.py`` and ``manage.py`` in the same directory, you'd want to only call
    # ``os.path.dirname`` once.
    # This is correct for the Django 1.4-style project layout; for the old-style
    # project layout with ``settings.py`` and ``manage.py`` in the same directory,
    # you'd want to only call ``os.path.dirname`` once.
    BASE_PATH = os.path.dirname(os.path.dirname(__file__))

    # This would be if you put all your tests within a top-level "tests" package.
    TEST_DISCOVERY_ROOT = os.path.join(BASE_PATH, "tests")

    # And this assumes you place the above ``DiscoveryRunner`` in ``tests/runner.py``.
    # This assumes you place the above ``DiscoveryRunner`` in ``tests/runner.py``.
    TEST_RUNNER = "tests.runner.DiscoveryRunner"
  5. Carl Meyer revised this gist Mar 13, 2012. 2 changed files with 46 additions and 3 deletions.
    29 changes: 26 additions & 3 deletions runner.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,39 @@
    """
    An alternative Django ``TEST_RUNNER`` which uses unittest2 test discovery from
    a base path specified in settings, rather than requiring all tests to be in
    ``tests`` module of an app.
    If you just run ``./manage.py test``, it'll discover and run all tests underneath the ``TEST_DISCOVERY_ROOT`` setting (a path). If you run ``./manage.py test full.dotted.path.to.test_module``, it'll run the tests in that module (you can also pass multiple modules).
    And (new in this updated version), if you give it a single dotted path to a package, and that package does not itself directly contain any tests, it'll do test discovery in all sub-modules of that package.
    """
    from django.conf import settings
    from django.test import TestCase
    from django.test.simple import DjangoTestSuiteRunner, reorder_suite
    from django.utils.unittest import TestCase
    from django.utils.importlib import import_module
    from django.utils.unittest.loader import defaultTestLoader



    class DiscoveryDjangoTestSuiteRunner(DjangoTestSuiteRunner):
    """A test suite runner that uses unittest2 test discovery."""
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
    suite = None
    discovery_root = settings.TEST_DISCOVERY_ROOT

    if test_labels:
    suite = defaultTestLoader.loadTestsFromNames(test_labels)
    else:
    suite = defaultTestLoader.discover(settings.TEST_DISCOVERY_ROOT)
    # if single named module has no tests, do discovery within it
    if not suite.countTestCases() and len(test_labels) == 1:
    suite = None
    discovery_root = import_module(test_labels[0]).__path__[0]

    if suite is None:
    suite = defaultTestLoader.discover(
    discovery_root,
    top_level_dir=settings.BASE_PATH,
    )

    if extra_tests:
    for test in extra_tests:
    20 changes: 20 additions & 0 deletions settings.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    """
    You need the ``BASE_PATH`` and ``TEST_DISCOVERY_ROOT`` settings in order for this test runner to work. ``BASE_PATH`` should be the directory containing your top-level package(s); in other words, the directory that should be on ``sys.path`` for your code to import. This is the directory containing ``manage.py`` in the new Django 1.4 project layout.
    ``TEST_DISCOVERY_ROOT`` should be the root directory to discover tests within. You could make this the same as ``BASE_PATH`` if you want tests to be discovered anywhere in your project. If you want tests to only be discovered within, say, a top-level ``tests`` directory, you'd set ``TEST_DISCOVERY_ROOT`` as shown below.
    And you need to point the ``TEST_RUNNER`` setting to the ``DiscoveryRunner`` class above.
    """
    import os.path

    # This is correct for the Django 1.4-style project layout; for the old-style project layout
    # with ``settings.py`` and ``manage.py`` in the same directory, you'd want to only call
    # ``os.path.dirname`` once.
    BASE_PATH = os.path.dirname(os.path.dirname(__file__))

    # This would be if you put all your tests within a top-level "tests" package.
    TEST_DISCOVERY_ROOT = os.path.join(BASE_PATH, "tests")

    # And this assumes you place the above ``DiscoveryRunner`` in ``tests/runner.py``.
    TEST_RUNNER = "tests.runner.DiscoveryRunner"
  6. Carl Meyer revised this gist Dec 9, 2011. No changes.
  7. Carl Meyer created this gist Dec 9, 2011.
    19 changes: 19 additions & 0 deletions runner.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    from django.conf import settings
    from django.test.simple import DjangoTestSuiteRunner, reorder_suite
    from django.utils.unittest import TestCase
    from django.utils.unittest.loader import defaultTestLoader



    class DiscoveryDjangoTestSuiteRunner(DjangoTestSuiteRunner):
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
    if test_labels:
    suite = defaultTestLoader.loadTestsFromNames(test_labels)
    else:
    suite = defaultTestLoader.discover(settings.TEST_DISCOVERY_ROOT)

    if extra_tests:
    for test in extra_tests:
    suite.addTest(test)

    return reorder_suite(suite, (TestCase,))