Loading django/test/utils.py +48 −0 Original line number Diff line number Diff line Loading @@ -493,3 +493,51 @@ def extend_sys_path(*paths): yield finally: sys.path = _orig_sys_path @contextmanager def captured_output(stream_name): """Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO. Note: This function and the following ``captured_std*`` are copied from CPython's ``test.support`` module.""" orig_stdout = getattr(sys, stream_name) setattr(sys, stream_name, six.StringIO()) try: yield getattr(sys, stream_name) finally: setattr(sys, stream_name, orig_stdout) def captured_stdout(): """Capture the output of sys.stdout: with captured_stdout() as stdout: print("hello") self.assertEqual(stdout.getvalue(), "hello\n") """ return captured_output("stdout") def captured_stderr(): """Capture the output of sys.stderr: with captured_stderr() as stderr: print("hello", file=sys.stderr) self.assertEqual(stderr.getvalue(), "hello\n") """ return captured_output("stderr") def captured_stdin(): """Capture the input to sys.stdin: with captured_stdin() as stdin: stdin.write('hello\n') stdin.seek(0) # call test code that consumes from sys.stdin captured = input() self.assertEqual(captured, "hello") """ return captured_output("stdin") tests/bash_completion/tests.py +7 −10 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ import unittest from django.apps import apps from django.core.management import ManagementUtility from django.utils.six import StringIO from django.test.utils import captured_stdout class BashCompletionTests(unittest.TestCase): Loading @@ -20,12 +20,8 @@ class BashCompletionTests(unittest.TestCase): def setUp(self): self.old_DJANGO_AUTO_COMPLETE = os.environ.get('DJANGO_AUTO_COMPLETE') os.environ['DJANGO_AUTO_COMPLETE'] = '1' self.output = StringIO() self.old_stdout = sys.stdout sys.stdout = self.output def tearDown(self): sys.stdout = self.old_stdout if self.old_DJANGO_AUTO_COMPLETE: os.environ['DJANGO_AUTO_COMPLETE'] = self.old_DJANGO_AUTO_COMPLETE else: Loading Loading @@ -53,11 +49,12 @@ class BashCompletionTests(unittest.TestCase): def _run_autocomplete(self): util = ManagementUtility(argv=sys.argv) with captured_stdout() as stdout: try: util.autocomplete() except SystemExit: pass return self.output.getvalue().strip().split('\n') return stdout.getvalue().strip().split('\n') def test_django_admin_py(self): "django_admin.py will autocomplete option flags" Loading tests/contenttypes_tests/tests.py +7 −13 Original line number Diff line number Diff line # -*- coding: utf-8 -*- from __future__ import unicode_literals import sys from django.apps.registry import Apps, apps from django.contrib.contenttypes.fields import ( GenericForeignKey, GenericRelation Loading @@ -12,9 +10,8 @@ from django.contrib.contenttypes.models import ContentType from django.core import checks from django.db import connections, models, router from django.test import TestCase from django.test.utils import override_settings from django.test.utils import captured_stdout, override_settings from django.utils.encoding import force_str from django.utils.six import StringIO from .models import Author, Article, SchemeIncludedURL Loading Loading @@ -369,11 +366,6 @@ class UpdateContentTypesTests(TestCase): self.before_count = ContentType.objects.count() ContentType.objects.create(name='fake', app_label='contenttypes_tests', model='Fake') self.app_config = apps.get_app_config('contenttypes_tests') self.old_stdout = sys.stdout sys.stdout = StringIO() def tearDown(self): sys.stdout = self.old_stdout def test_interactive_true(self): """ Loading @@ -381,8 +373,9 @@ class UpdateContentTypesTests(TestCase): stale contenttypes. """ management.input = lambda x: force_str("yes") with captured_stdout() as stdout: management.update_contenttypes(self.app_config) self.assertIn("Deleting stale content type", sys.stdout.getvalue()) self.assertIn("Deleting stale content type", stdout.getvalue()) self.assertEqual(ContentType.objects.count(), self.before_count) def test_interactive_false(self): Loading @@ -390,8 +383,9 @@ class UpdateContentTypesTests(TestCase): non-interactive mode of update_contenttypes() shouldn't delete stale content types. """ with captured_stdout() as stdout: management.update_contenttypes(self.app_config, interactive=False) self.assertIn("Stale content types remain.", sys.stdout.getvalue()) self.assertIn("Stale content types remain.", stdout.getvalue()) self.assertEqual(ContentType.objects.count(), self.before_count + 1) Loading tests/i18n/test_compilation.py +2 −6 Original line number Diff line number Diff line Loading @@ -3,7 +3,6 @@ import os import shutil import stat import sys import unittest import gettext as gettext_module Loading @@ -11,6 +10,7 @@ from django.core.management import call_command, CommandError, execute_from_comm from django.core.management.utils import find_command from django.test import SimpleTestCase from django.test import override_settings from django.test.utils import captured_stderr, captured_stdout from django.utils import translation from django.utils.translation import ugettext from django.utils.encoding import force_text Loading Loading @@ -145,15 +145,11 @@ class ExcludedLocaleCompilationTests(MessageCompilationTests): self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale')) def test_command_help(self): old_stdout, old_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = StringIO(), StringIO() try: with captured_stdout(), captured_stderr(): # `call_command` bypasses the parser; by calling # `execute_from_command_line` with the help subcommand we # ensure that there are no issues with the parser itself. execute_from_command_line(['django-admin', 'help', 'compilemessages']) finally: sys.stdout, sys.stderr = old_stdout, old_stderr def test_one_locale_excluded(self): call_command('compilemessages', exclude=['it'], stdout=StringIO()) Loading tests/i18n/test_extraction.py +2 −6 Original line number Diff line number Diff line Loading @@ -5,7 +5,6 @@ import io import os import re import shutil import sys import time from unittest import SkipTest, skipUnless import warnings Loading @@ -16,6 +15,7 @@ from django.core.management import execute_from_command_line from django.core.management.utils import find_command from django.test import SimpleTestCase from django.test import override_settings from django.test.utils import captured_stderr, captured_stdout from django.utils.encoding import force_text from django.utils._os import upath from django.utils import six Loading Loading @@ -632,15 +632,11 @@ class ExcludedLocaleExtractionTests(ExtractorTests): self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale')) def test_command_help(self): old_stdout, old_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = StringIO(), StringIO() try: with captured_stdout(), captured_stderr(): # `call_command` bypasses the parser; by calling # `execute_from_command_line` with the help subcommand we # ensure that there are no issues with the parser itself. execute_from_command_line(['django-admin', 'help', 'makemessages']) finally: sys.stdout, sys.stderr = old_stdout, old_stderr def test_one_locale_excluded(self): management.call_command('makemessages', exclude=['it'], stdout=StringIO()) Loading Loading
django/test/utils.py +48 −0 Original line number Diff line number Diff line Loading @@ -493,3 +493,51 @@ def extend_sys_path(*paths): yield finally: sys.path = _orig_sys_path @contextmanager def captured_output(stream_name): """Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO. Note: This function and the following ``captured_std*`` are copied from CPython's ``test.support`` module.""" orig_stdout = getattr(sys, stream_name) setattr(sys, stream_name, six.StringIO()) try: yield getattr(sys, stream_name) finally: setattr(sys, stream_name, orig_stdout) def captured_stdout(): """Capture the output of sys.stdout: with captured_stdout() as stdout: print("hello") self.assertEqual(stdout.getvalue(), "hello\n") """ return captured_output("stdout") def captured_stderr(): """Capture the output of sys.stderr: with captured_stderr() as stderr: print("hello", file=sys.stderr) self.assertEqual(stderr.getvalue(), "hello\n") """ return captured_output("stderr") def captured_stdin(): """Capture the input to sys.stdin: with captured_stdin() as stdin: stdin.write('hello\n') stdin.seek(0) # call test code that consumes from sys.stdin captured = input() self.assertEqual(captured, "hello") """ return captured_output("stdin")
tests/bash_completion/tests.py +7 −10 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ import unittest from django.apps import apps from django.core.management import ManagementUtility from django.utils.six import StringIO from django.test.utils import captured_stdout class BashCompletionTests(unittest.TestCase): Loading @@ -20,12 +20,8 @@ class BashCompletionTests(unittest.TestCase): def setUp(self): self.old_DJANGO_AUTO_COMPLETE = os.environ.get('DJANGO_AUTO_COMPLETE') os.environ['DJANGO_AUTO_COMPLETE'] = '1' self.output = StringIO() self.old_stdout = sys.stdout sys.stdout = self.output def tearDown(self): sys.stdout = self.old_stdout if self.old_DJANGO_AUTO_COMPLETE: os.environ['DJANGO_AUTO_COMPLETE'] = self.old_DJANGO_AUTO_COMPLETE else: Loading Loading @@ -53,11 +49,12 @@ class BashCompletionTests(unittest.TestCase): def _run_autocomplete(self): util = ManagementUtility(argv=sys.argv) with captured_stdout() as stdout: try: util.autocomplete() except SystemExit: pass return self.output.getvalue().strip().split('\n') return stdout.getvalue().strip().split('\n') def test_django_admin_py(self): "django_admin.py will autocomplete option flags" Loading
tests/contenttypes_tests/tests.py +7 −13 Original line number Diff line number Diff line # -*- coding: utf-8 -*- from __future__ import unicode_literals import sys from django.apps.registry import Apps, apps from django.contrib.contenttypes.fields import ( GenericForeignKey, GenericRelation Loading @@ -12,9 +10,8 @@ from django.contrib.contenttypes.models import ContentType from django.core import checks from django.db import connections, models, router from django.test import TestCase from django.test.utils import override_settings from django.test.utils import captured_stdout, override_settings from django.utils.encoding import force_str from django.utils.six import StringIO from .models import Author, Article, SchemeIncludedURL Loading Loading @@ -369,11 +366,6 @@ class UpdateContentTypesTests(TestCase): self.before_count = ContentType.objects.count() ContentType.objects.create(name='fake', app_label='contenttypes_tests', model='Fake') self.app_config = apps.get_app_config('contenttypes_tests') self.old_stdout = sys.stdout sys.stdout = StringIO() def tearDown(self): sys.stdout = self.old_stdout def test_interactive_true(self): """ Loading @@ -381,8 +373,9 @@ class UpdateContentTypesTests(TestCase): stale contenttypes. """ management.input = lambda x: force_str("yes") with captured_stdout() as stdout: management.update_contenttypes(self.app_config) self.assertIn("Deleting stale content type", sys.stdout.getvalue()) self.assertIn("Deleting stale content type", stdout.getvalue()) self.assertEqual(ContentType.objects.count(), self.before_count) def test_interactive_false(self): Loading @@ -390,8 +383,9 @@ class UpdateContentTypesTests(TestCase): non-interactive mode of update_contenttypes() shouldn't delete stale content types. """ with captured_stdout() as stdout: management.update_contenttypes(self.app_config, interactive=False) self.assertIn("Stale content types remain.", sys.stdout.getvalue()) self.assertIn("Stale content types remain.", stdout.getvalue()) self.assertEqual(ContentType.objects.count(), self.before_count + 1) Loading
tests/i18n/test_compilation.py +2 −6 Original line number Diff line number Diff line Loading @@ -3,7 +3,6 @@ import os import shutil import stat import sys import unittest import gettext as gettext_module Loading @@ -11,6 +10,7 @@ from django.core.management import call_command, CommandError, execute_from_comm from django.core.management.utils import find_command from django.test import SimpleTestCase from django.test import override_settings from django.test.utils import captured_stderr, captured_stdout from django.utils import translation from django.utils.translation import ugettext from django.utils.encoding import force_text Loading Loading @@ -145,15 +145,11 @@ class ExcludedLocaleCompilationTests(MessageCompilationTests): self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale')) def test_command_help(self): old_stdout, old_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = StringIO(), StringIO() try: with captured_stdout(), captured_stderr(): # `call_command` bypasses the parser; by calling # `execute_from_command_line` with the help subcommand we # ensure that there are no issues with the parser itself. execute_from_command_line(['django-admin', 'help', 'compilemessages']) finally: sys.stdout, sys.stderr = old_stdout, old_stderr def test_one_locale_excluded(self): call_command('compilemessages', exclude=['it'], stdout=StringIO()) Loading
tests/i18n/test_extraction.py +2 −6 Original line number Diff line number Diff line Loading @@ -5,7 +5,6 @@ import io import os import re import shutil import sys import time from unittest import SkipTest, skipUnless import warnings Loading @@ -16,6 +15,7 @@ from django.core.management import execute_from_command_line from django.core.management.utils import find_command from django.test import SimpleTestCase from django.test import override_settings from django.test.utils import captured_stderr, captured_stdout from django.utils.encoding import force_text from django.utils._os import upath from django.utils import six Loading Loading @@ -632,15 +632,11 @@ class ExcludedLocaleExtractionTests(ExtractorTests): self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale')) def test_command_help(self): old_stdout, old_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = StringIO(), StringIO() try: with captured_stdout(), captured_stderr(): # `call_command` bypasses the parser; by calling # `execute_from_command_line` with the help subcommand we # ensure that there are no issues with the parser itself. execute_from_command_line(['django-admin', 'help', 'makemessages']) finally: sys.stdout, sys.stderr = old_stdout, old_stderr def test_one_locale_excluded(self): management.call_command('makemessages', exclude=['it'], stdout=StringIO()) Loading