Skip to content

Use python module dynamic imports for getting app modules #7

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 2 commits into from
Apr 21, 2015
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
3 changes: 3 additions & 0 deletions dotted/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# (c) 2015 Bright Interactive Limited. All rights reserved.
# http://www.bright-interactive.com | info@bright-interactive.com
3 changes: 3 additions & 0 deletions dotted/dotted_test_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# (c) 2015 Bright Interactive Limited. All rights reserved.
# http://www.bright-interactive.com | info@bright-interactive.com
21 changes: 21 additions & 0 deletions dotted/dotted_test_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# (c) 2015 Bright Interactive Limited. All rights reserved.
# http://www.bright-interactive.com | info@bright-interactive.com
import os
from django.test.testcases import TestCase
from dotted import dotted_test_app
from test_extras.testrunners import get_coverage_files


class CoverageTests(TestCase):

def test_get_coverage_works_for_dotted_app_labels(self):
files = get_coverage_files(
['dotted.dotted_test_app'],
ignore_dirs=[],
ignore_files=['tests.py']
)

self.assertEqual(1, len(files))
expected_path = os.path.join(os.path.dirname(dotted_test_app.__file__), '__init__.py')
self.assertIn(expected_path, files)
13 changes: 13 additions & 0 deletions test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# (c) 2013 Bright Interactive Limited. All rights reserved.
# http://www.bright-interactive.com | info@bright-interactive.com
from django.test import TestCase, TransactionTestCase
import test_app
import os
from test_extras.testcases import DataPreservingTransactionTestCaseMixin
from test_app.models import Painter
from test_extras.testrunners import get_coverage_files


class TestDataPreserving(DataPreservingTransactionTestCaseMixin, TransactionTestCase):
Expand Down Expand Up @@ -35,3 +38,13 @@ def test_1_success(self):

def test_2_check(self):
self.assertTrue(HookTests.addSuccess_called)


class CoverageTests(TestCase):

def test_get_coverage_gets_the_expected_files(self):
coverage_files = get_coverage_files(['test_app'], ignore_dirs=['south_migrations', 'migrations', 'fixtures'], ignore_files=['tests.py'])
expected_coverage_file_names = ['__init__.py', 'views.py', 'models.py']
full_paths = [os.path.join(os.path.dirname(test_app.__file__), file_name) for file_name in expected_coverage_file_names]
self.assertEqual(len(full_paths), len(coverage_files))
self.assertTrue(all(path in coverage_files for path in full_paths))
Empty file added test_app_no_models/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions test_app_no_models/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.test.testcases import TestCase
import test_app_no_models
import os
from test_extras.testrunners import get_coverage_files


class CoverageTests(TestCase):

def test_get_coverage_gets_the_expected_files(self):
coverage_files = get_coverage_files(['test_app_no_models'], ignore_dirs=[], ignore_files=['tests.py'])
expected_coverage_file_names = ['__init__.py', 'views.py']
full_paths = [os.path.join(os.path.dirname(test_app_no_models.__file__), file_name) for file_name in expected_coverage_file_names]
self.assertEqual(len(full_paths), len(coverage_files))
self.assertTrue(all(path in coverage_files for path in full_paths))
1 change: 1 addition & 0 deletions test_app_no_models/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
19 changes: 11 additions & 8 deletions test_extras/testrunners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
# http://www.bright-interactive.com | info@bright-interactive.com

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils import unittest
import coverage
import django.test.simple
import os.path
import pdb
import sys
import xmlrunner
profile = None # Fool pylint about double import
try:
Expand Down Expand Up @@ -127,16 +129,17 @@ def get_coverage_files(app_labels, ignore_dirs, ignore_files):
return a list of all the python files that should be included in
a coverage report for that test.
"""
from django.db.models import get_app

ret = []
files_for_coverage = []

for app_label in app_labels:
module = get_app(app_label)
dirname = os.path.dirname(module.__file__)
ret.extend(get_coverage_files_in_directory(dirname, ignore_dirs, ignore_files))

return ret
if app_label in settings.INSTALLED_APPS:
module = __import__(app_label, globals(), locals(), [''], -1)
dirname = os.path.dirname(module.__file__)
files_for_coverage.extend(get_coverage_files_in_directory(dirname, ignore_dirs, ignore_files))
else:
raise ImproperlyConfigured('%s is not installed' % app_label)

return files_for_coverage


class ExceptionTestResultMixin(object):
Expand Down
15 changes: 12 additions & 3 deletions test_extras/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# -*- coding: utf-8 -*-
# (c) 2012 Bright Interactive Limited. All rights reserved.
# http://www.bright-interactive.com | info@bright-interactive.com
import os
from django.core.exceptions import ImproperlyConfigured

from django.test import TestCase
from django.test.utils import override_settings
import test_extras.management
from test_extras.testrunners import get_coverage_files


class TestStuff(TestCase):
def test_something(self):
self.assertEquals(2, 1 + 1)
class CoverageTests(TestCase):

@override_settings(INSTALLED_APPS=[])
def test_get_coverage_raises_error_if_app_is_not_installed(self):
with self.assertRaises(ImproperlyConfigured):
get_coverage_files(['test'], ignore_dirs=[], ignore_files=[])

2 changes: 2 additions & 0 deletions testsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
INSTALLED_APPS = (
'test_extras',
'test_app',
'test_app_no_models',
'dotted.dotted_test_app',
)

MIDDLEWARE_CLASSES = (
Expand Down