Skip to content

[WIP] Use standard Django test runner #1837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
pip install coverage requests
pip install coverage requests tox tox-gh-actions
pip install django==${{ matrix.django-version }} elasticsearch==${{ matrix.elastic-version }}
python setup.py clean build install
- name: Run test
run: coverage run setup.py test
run: tox -v
env:
DJANGO: ${{ matrix.django-version }}
14 changes: 5 additions & 9 deletions docs/running_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@ the errors persist.
To run just a portion of the tests you can use the script ``run_tests.py`` and
just specify the files or directories you wish to run, for example::

cd test_haystack
./run_tests.py whoosh_tests test_loading.py
python test_haystack/run_tests.py whoosh_tests test_loading.py

The ``run_tests.py`` script is just a tiny wrapper around the nose_ library and
any options you pass to it will be passed on; including ``--help`` to get a
list of possible options::
The ``run_tests.py`` script is just a tiny wrapper around the Django test
command and any options you pass to it will be passed on; including ``--help``
to get a list of possible options::

cd test_haystack
./run_tests.py --help

.. _nose: https://nose.readthedocs.io/en/latest/
python test_haystack/run_tests.py --help

Configuring Solr
================
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"whoosh>=2.5.4,<3.0",
"python-dateutil",
"geopy==2.0.0",
"nose",
"coverage",
"requests",
]
Expand Down
27 changes: 0 additions & 27 deletions test_haystack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +0,0 @@
import os

test_runner = None
old_config = None

os.environ["DJANGO_SETTINGS_MODULE"] = "test_haystack.settings"


import django

django.setup()


def setup():
global test_runner
global old_config

from django.test.runner import DiscoverRunner

test_runner = DiscoverRunner()
test_runner.setup_test_environment()
old_config = test_runner.setup_databases()


def teardown():
test_runner.teardown_databases(old_config)
test_runner.teardown_test_environment()
12 changes: 8 additions & 4 deletions test_haystack/elasticsearch2_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
import unittest
import warnings

from django.conf import settings

from haystack.utils import log as logging

warnings.simplefilter("ignore", Warning)


def setup():
def load_tests(loader, standard_tests, pattern):
log = logging.getLogger("haystack")
try:
import elasticsearch
Expand All @@ -29,3 +27,9 @@ def setup():
except exceptions.ConnectionError as e:
log.error("elasticsearch not running on %r" % url, exc_info=True)
raise unittest.SkipTest("elasticsearch not running on %r" % url, e)

package_tests = loader.discover(
start_dir=os.path.dirname(__file__), pattern=pattern
)
standard_tests.addTests(package_tests)
return standard_tests
12 changes: 8 additions & 4 deletions test_haystack/elasticsearch5_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
import unittest
import warnings

from django.conf import settings

from haystack.utils import log as logging

warnings.simplefilter("ignore", Warning)


def setup():
def load_tests(loader, standard_tests, pattern):
log = logging.getLogger("haystack")
try:
import elasticsearch
Expand All @@ -29,3 +27,9 @@ def setup():
except exceptions.ConnectionError as e:
log.error("elasticsearch not running on %r" % url, exc_info=True)
raise unittest.SkipTest("elasticsearch not running on %r" % url, e)

package_tests = loader.discover(
start_dir=os.path.dirname(__file__), pattern=pattern
)
standard_tests.addTests(package_tests)
return standard_tests
12 changes: 8 additions & 4 deletions test_haystack/elasticsearch7_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
import unittest
import warnings

from django.conf import settings

from haystack.utils import log as logging

warnings.simplefilter("ignore", Warning)


def setup():
def load_tests(loader, standard_tests, pattern):
log = logging.getLogger("haystack")
try:
import elasticsearch
Expand All @@ -29,3 +27,9 @@ def setup():
except exceptions.ConnectionError as e:
log.error("elasticsearch not running on %r" % url, exc_info=True)
raise unittest.SkipTest("elasticsearch not running on %r" % url, e)

package_tests = loader.discover(
start_dir=os.path.dirname(__file__), pattern=pattern
)
standard_tests.addTests(package_tests)
return standard_tests
12 changes: 8 additions & 4 deletions test_haystack/elasticsearch_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
import unittest
import warnings

from django.conf import settings

from haystack.utils import log as logging

warnings.simplefilter("ignore", Warning)


def setup():
def load_tests(loader, standard_tests, pattern):
log = logging.getLogger("haystack")
try:
import elasticsearch
Expand Down Expand Up @@ -36,3 +34,9 @@ def setup():
% settings.HAYSTACK_CONNECTIONS["elasticsearch"]["URL"],
e,
)

package_tests = loader.discover(
start_dir=os.path.dirname(__file__), pattern=pattern
)
standard_tests.addTests(package_tests)
return standard_tests
24 changes: 6 additions & 18 deletions test_haystack/multipleindex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
from django.apps import apps

import haystack
from haystack.signals import RealtimeSignalProcessor
import os

from ..utils import check_solr

_old_sp = None


def setup():
def load_tests(loader, standard_tests, pattern):
check_solr()
global _old_sp
config = apps.get_app_config("haystack")
_old_sp = config.signal_processor
config.signal_processor = RealtimeSignalProcessor(
haystack.connections, haystack.connection_router
package_tests = loader.discover(
start_dir=os.path.dirname(__file__), pattern=pattern
)


def teardown():
config = apps.get_app_config("haystack")
config.signal_processor.teardown()
config.signal_processor = _old_sp
standard_tests.addTests(package_tests)
return standard_tests
21 changes: 19 additions & 2 deletions test_haystack/multipleindex/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.apps import apps
from django.db import models

from haystack import connections
from haystack import connection_router, connections
from haystack.exceptions import NotHandled
from haystack.query import SearchQuerySet
from haystack.signals import BaseSignalProcessor
from haystack.signals import BaseSignalProcessor, RealtimeSignalProcessor

from ..whoosh_tests.testcases import WhooshTestCase
from .models import Bar, Foo
Expand Down Expand Up @@ -191,6 +192,22 @@ def teardown(self):


class SignalProcessorTestCase(WhooshTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
config = apps.get_app_config("haystack")
cls._old_sp = config.signal_processor
config.signal_processor = RealtimeSignalProcessor(
connections, connection_router
)

@classmethod
def tearDown(cls):
config = apps.get_app_config("haystack")
config.signal_processor.teardown()
config.signal_processor = cls._old_sp
super().tearDown()

def setUp(self):
super().setUp()

Expand Down
21 changes: 7 additions & 14 deletions test_haystack/run_tests.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#!/usr/bin/env python
import os
import sys
from os.path import abspath, dirname

import nose
import django
from django.core.management import call_command


def run_all(argv=None):
sys.exitfunc = lambda: sys.stderr.write("Shutting down....\n")
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
os.environ["DJANGO_SETTINGS_MODULE"] = "test_haystack.settings"
django.setup()

# always insert coverage when running tests through setup.py
if argv is None:
argv = [
"nosetests",
"--with-coverage",
"--cover-package=haystack",
"--cover-erase",
"--verbose",
]

nose.run_exit(argv=argv, defaultTest=abspath(dirname(__file__)))
call_command("test", sys.argv[1:])


if __name__ == "__main__":
Expand Down
11 changes: 7 additions & 4 deletions test_haystack/solr_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import warnings

warnings.simplefilter("ignore", Warning)
import os

from ..utils import check_solr


def setup():
def load_tests(loader, standard_tests, pattern):
check_solr()
package_tests = loader.discover(
start_dir=os.path.dirname(__file__), pattern=pattern
)
standard_tests.addTests(package_tests)
return standard_tests
9 changes: 8 additions & 1 deletion test_haystack/spatial/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import os

from ..utils import check_solr


def setup():
def load_tests(loader, standard_tests, pattern):
check_solr()
package_tests = loader.discover(
start_dir=os.path.dirname(__file__), pattern=pattern
)
standard_tests.addTests(package_tests)
return standard_tests
8 changes: 6 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
[tox]
envlist =
docs
py35-django2.2-es{1.x,2.x,5.x,7.x}
py{36,37,38,39,310,py}-django{2.2,3.0,3.1,3.2,4.0}-es{1.x,2.x,5.x,7.x}


[testenv]
commands =
python test_haystack/solr_tests/server/wait-for-solr
python {toxinidir}/setup.py test
coverage run {toxinidir}/test_haystack/run_tests.py
deps =
pysolr>=3.7.0
whoosh>=2.5.4,<3.0
python-dateutil
geopy==2.0.0
coverage
requests
django2.2: Django>=2.2,<3.0
django3.0: Django>=3.0,<3.1
Expand Down