Browse Source

Change testrunner to pytest

rafalp 6 years ago
parent
commit
ec3541e649

+ 3 - 4
dev

@@ -72,8 +72,7 @@ intro() {
     echo
     echo
     echo "Testing:"
     echo "Testing:"
     echo
     echo
-    echo "    ${BOLD}test${NORMAL}              run tests suite."
-    echo "    ${BOLD}test module${NORMAL}       run tests suite in specified python module, eg. misago.users."
+    echo "    ${BOLD}test${NORMAL}              run tests suite using pytest."
     echo
     echo
     echo "Translations:"
     echo "Translations:"
     echo
     echo
@@ -197,7 +196,7 @@ rebuild() {
 
 
 # Run tests suite
 # Run tests suite
 test() {
 test() {
-    docker-compose run --rm misago python runtests.py $1
+    docker-compose run --rm misago pytest "${@:2}"
 }
 }
 
 
 # Make messages
 # Make messages
@@ -284,7 +283,7 @@ if [[ $1 ]]; then
     elif [[ $1 = "rebuild" ]]; then
     elif [[ $1 = "rebuild" ]]; then
         rebuild $@
         rebuild $@
     elif [[ $1 = "test" ]]; then
     elif [[ $1 = "test" ]]; then
-        test $2
+        test $@
     elif [[ $1 = "makemessages" ]]; then
     elif [[ $1 = "makemessages" ]]; then
         makemessages ${2:-en}
         makemessages ${2:-en}
     elif [[ $1 = "makemessages_in_docker" ]]; then
     elif [[ $1 = "makemessages_in_docker" ]]; then

+ 3 - 3
misago/core/tests/test_errorpages.py

@@ -76,7 +76,7 @@ class ErrorPageViewsTests(TestCase):
         self.assertContains(response, "Banned in auth!", status_code=403)
         self.assertContains(response, "Banned in auth!", status_code=403)
 
 
 
 
-def test_request(url):
+def create_request(url):
     request = RequestFactory().get(url)
     request = RequestFactory().get(url)
     request.cache_versions = get_cache_versions()
     request.cache_versions = get_cache_versions()
     request.settings = DynamicSettings(request.cache_versions)
     request.settings = DynamicSettings(request.cache_versions)
@@ -90,8 +90,8 @@ def test_request(url):
 @override_settings(ROOT_URLCONF='misago.core.testproject.urlswitherrorhandlers')
 @override_settings(ROOT_URLCONF='misago.core.testproject.urlswitherrorhandlers')
 class CustomErrorPagesTests(TestCase):
 class CustomErrorPagesTests(TestCase):
     def setUp(self):
     def setUp(self):
-        self.misago_request = test_request(reverse('misago:index'))
-        self.site_request = test_request(reverse('raise-403'))
+        self.misago_request = create_request(reverse('misago:index'))
+        self.site_request = create_request(reverse('raise-403'))
 
 
     def test_shared_403_decorator(self):
     def test_shared_403_decorator(self):
         """shared_403_decorator calls correct error handler"""
         """shared_403_decorator calls correct error handler"""

+ 3 - 3
misago/core/tests/test_exceptionhandler_middleware.py

@@ -12,7 +12,7 @@ from misago.users.models import AnonymousUser
 from misago.core.middleware import ExceptionHandlerMiddleware
 from misago.core.middleware import ExceptionHandlerMiddleware
 
 
 
 
-def test_request():
+def create_request():
     request = RequestFactory().get(reverse('misago:index'))
     request = RequestFactory().get(reverse('misago:index'))
     request.cache_versions = get_cache_versions()
     request.cache_versions = get_cache_versions()
     request.settings = DynamicSettings(request.cache_versions)
     request.settings = DynamicSettings(request.cache_versions)
@@ -28,10 +28,10 @@ class ExceptionHandlerMiddlewareTests(TestCase):
         """Middleware returns HttpResponse for supported exception"""
         """Middleware returns HttpResponse for supported exception"""
         middleware = ExceptionHandlerMiddleware()
         middleware = ExceptionHandlerMiddleware()
         exception = Http404()
         exception = Http404()
-        assert middleware.process_exception(test_request(), exception)
+        assert middleware.process_exception(create_request(), exception)
 
 
     def test_middleware_returns_none_for_non_supported_exception(self):
     def test_middleware_returns_none_for_non_supported_exception(self):
         """Middleware returns None for non-supported exception"""
         """Middleware returns None for non-supported exception"""
         middleware = ExceptionHandlerMiddleware()
         middleware = ExceptionHandlerMiddleware()
         exception = TypeError()
         exception = TypeError()
-        assert middleware.process_exception(test_request(), exception) is None
+        assert middleware.process_exception(create_request(), exception) is None

+ 0 - 24
misago/users/tests/test_commands.py

@@ -1,24 +0,0 @@
-from django.contrib.auth import get_user_model
-from django.core.management import call_command
-from django.test import TestCase
-
-
-UserModel = get_user_model()
-
-
-class CreateSuperUserTests(TestCase):
-    def test_createsuperuser(self):
-        """createsuperuser creates user account in perfect conditions"""
-        opts = {
-            'username': 'Boberson',
-            'email': 'bob@test.com',
-            'password': 'Pass.123',
-            'verbosity': 0,
-        }
-
-        call_command('createsuperuser', **opts)
-
-        user = UserModel.objects.get(username=opts['username'])
-        self.assertEqual(user.username, opts['username'])
-        self.assertEqual(user.email, opts['email'])
-        self.assertTrue(user.check_password(opts['password']))

+ 19 - 23
misago/users/tests/test_createsuperuser.py

@@ -2,33 +2,29 @@ from io import StringIO
 
 
 from django.contrib.auth import get_user_model
 from django.contrib.auth import get_user_model
 from django.core.management import call_command
 from django.core.management import call_command
-from django.test import TestCase
 
 
+User = get_user_model()
 
 
-UserModel = get_user_model()
 
 
+def test_superuser_is_created_if_input_is_valid(db):
+    out = StringIO()
 
 
-class CreateSuperuserTests(TestCase):
-    def test_valid_input_creates_superuser(self):
-        """command creates superuser"""
-        out = StringIO()
+    call_command(
+        "createsuperuser",
+        interactive=False,
+        username="test",
+        email="test@example.com",
+        password="password",
+        stdout=out,
+    )
 
 
-        call_command(
-            "createsuperuser",
-            interactive=False,
-            username="joe",
-            email="joe@somewhere.org",
-            password="Pass.123",
-            stdout=out,
-        )
+    command_output = out.getvalue().splitlines()[-1].strip()
+    user = User.objects.order_by('-id')[:1][0]
 
 
-        new_user = UserModel.objects.order_by('-id')[:1][0]
+    assert command_output == (
+        "Superuser #%s has been created successfully." % user.pk
+    )
 
 
-        self.assertEqual(
-            out.getvalue().splitlines()[-1].strip(),
-            'Superuser #%s has been created successfully.' % new_user.pk,
-        )
-
-        self.assertEqual(new_user.username, 'joe')
-        self.assertEqual(new_user.email, 'joe@somewhere.org')
-        self.assertTrue(new_user.check_password("Pass.123"))
+    assert user.username == "test"
+    assert user.email == "test@example.com"
+    assert user.check_password("password")

+ 4 - 0
pytest.ini

@@ -0,0 +1,4 @@
+[pytest]
+DJANGO_SETTINGS_MODULE = devproject.test_settings
+testpaths = misago
+python_files = tests.py test_*.py *_tests.py

+ 2 - 0
requirements.in

@@ -11,6 +11,8 @@ markdown<2.7
 misago-social-auth-app-django
 misago-social-auth-app-django
 pillow<4.2
 pillow<4.2
 psycopg2-binary<2.8
 psycopg2-binary<2.8
+pytest
+pytest-django
 pytz
 pytz
 requests<3
 requests<3
 unidecode<1
 unidecode<1

+ 11 - 1
requirements.txt

@@ -4,6 +4,8 @@
 #
 #
 #    pip-compile --output-file requirements.txt requirements.in
 #    pip-compile --output-file requirements.txt requirements.in
 #
 #
+atomicwrites==1.2.1       # via pytest
+attrs==18.2.0             # via pytest
 beautifulsoup4==4.6.3
 beautifulsoup4==4.6.3
 bleach==2.1.4
 bleach==2.1.4
 certifi==2018.10.15       # via requests
 certifi==2018.10.15       # via requests
@@ -14,21 +16,29 @@ django-mptt==0.8.7
 django==1.11.16
 django==1.11.16
 djangorestframework==3.6.4
 djangorestframework==3.6.4
 faker==0.8.18
 faker==0.8.18
+funcsigs==1.0.2           # via pytest
 html5lib==0.999999999
 html5lib==0.999999999
 idna==2.7                 # via requests
 idna==2.7                 # via requests
 ipaddress==1.0.22         # via faker
 ipaddress==1.0.22         # via faker
 markdown==2.6.11
 markdown==2.6.11
 misago-social-auth-app-django==2.1.0
 misago-social-auth-app-django==2.1.0
+more-itertools==4.3.0     # via pytest
 oauthlib==2.1.0           # via requests-oauthlib, social-auth-core
 oauthlib==2.1.0           # via requests-oauthlib, social-auth-core
 olefile==0.46             # via pillow
 olefile==0.46             # via pillow
+pathlib2==2.3.3           # via pytest, pytest-django
 pillow==4.1.1
 pillow==4.1.1
+pluggy==0.8.0             # via pytest
 psycopg2-binary==2.7.5
 psycopg2-binary==2.7.5
+py==1.7.0                 # via pytest
 pyjwt==1.6.4              # via social-auth-core
 pyjwt==1.6.4              # via social-auth-core
+pytest-django==3.4.4
+pytest==4.0.2
 python-dateutil==2.7.5    # via faker
 python-dateutil==2.7.5    # via faker
 pytz==2018.7
 pytz==2018.7
 requests-oauthlib==1.0.0  # via social-auth-core
 requests-oauthlib==1.0.0  # via social-auth-core
 requests==2.20.0
 requests==2.20.0
-six==1.11.0               # via bleach, faker, html5lib, misago-social-auth-app-django, python-dateutil, social-auth-core
+scandir==1.9.0            # via pathlib2
+six==1.11.0               # via bleach, faker, html5lib, misago-social-auth-app-django, more-itertools, pathlib2, pytest, python-dateutil, social-auth-core
 social-auth-core==1.7.0   # via misago-social-auth-app-django
 social-auth-core==1.7.0   # via misago-social-auth-app-django
 sqlparse==0.2.4           # via django-debug-toolbar
 sqlparse==0.2.4           # via django-debug-toolbar
 text-unidecode==1.2       # via faker
 text-unidecode==1.2       # via faker

+ 0 - 36
runtests.py

@@ -1,36 +0,0 @@
-#!/usr/bin/env python
-import os
-import sys
-
-
-def runtests():
-    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "devproject.test_settings")
-    try:
-        from django import setup
-        from django.core.management import call_command
-    except ImportError:
-        # The above import may fail for some other reason. Ensure that the
-        # issue is really that Django is missing to avoid masking other
-        # exceptions on Python 2.
-        try:
-            import django
-        except ImportError:
-            raise ImportError(
-                "Couldn't import Django. Are you sure it's installed and "
-                "available on your PYTHONPATH environment variable? Did you "
-                "forget to activate a virtual environment?"
-            )
-        raise
-
-    setup()
-
-    modules = sys.argv[1:]
-    if "test" in modules:
-        modules.remove("test")
-
-    exit_code = call_command("test", *modules, verbosity=1, noinput=True)
-    sys.exit(exit_code)
-
-
-if __name__ == '__main__':
-    runtests()

+ 1 - 1
setup.py

@@ -38,7 +38,7 @@ setup(
     install_requires=REQUIREMENTS,
     install_requires=REQUIREMENTS,
     packages=find_packages(exclude=EXCLUDE_FROM_PACKAGES),
     packages=find_packages(exclude=EXCLUDE_FROM_PACKAGES),
     include_package_data=True,
     include_package_data=True,
-    test_suite="runtests.runtests",
+    test_suite="pytest",
     classifiers=[
     classifiers=[
         'Development Status :: 4 - Beta',
         'Development Status :: 4 - Beta',
         'Environment :: Web Environment',
         'Environment :: Web Environment',