Code

Opened 4 years ago

Closed 3 years ago

#12682 closed (wontfix)

testcase not loading fixtures, no error message shown

Reported by: forgacs.attila@… Owned by: nobody
Component: Testing framework Version: 1.2
Severity: Keywords: fixture testcase not loading
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

class SimpleTest(TestCase):

fixtures = ['testvectors.json', 'testusers']

  1. Verbosity is set to "2"
  2. fixtures reside in directory called "fixtures"
  3. loaddata works !
  4. using JSON fixtures.

Problem: When running test case fixtures are not loaded, no error message is shown.
In fact, the word testvectors are not even present in the log.
The only thing that the testcase wants to load is the initial_data
which is nonexistent.

Attachments (0)

Change History (5)

comment:1 Changed 4 years ago by forgacs.attila@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Got it working, there is no output about loading of fixtures.
Suggestion: At least on VERBOSITY == 2, log the loading of fixtures
to stdout.

comment:2 Changed 4 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

Fixtures are loaded at the start of *every* test in a test case. Printing log messages at *any* verbosity would be exceedingly obnoxious.

comment:3 Changed 4 years ago by anonymous

Understood, valid points there.
What about a fixture validation just like model validation,
that is done once for a complete testcase ?
Just like runserver does ?

comment:4 Changed 3 years ago by slinkp

  • Resolution wontfix deleted
  • Status changed from closed to reopened
  • Version changed from 1.1 to 1.2

Straw man patch against django 1.2.3 to print fixture errors only once per test run, works for me but it's just a quick hack:

--- testcases.py~	2011-03-08 14:54:32.819851869 -0500
+++ testcases.py	2011-03-08 17:06:15.242352630 -0500
@@ -33,6 +33,22 @@
         value = [value]
     return value
 
+class FilteringStderr(object):
+    """
+    Prints a given message to stderr only once.
+    """
+    def __init__(self):
+        self.lines_seen = set()
+
+    def write(self, data):
+        import sys
+        if data in self.lines_seen:
+            return
+        self.lines_seen.add(data)
+        sys.stderr.write(data)
+
+filtered_stderr = FilteringStderr()
+
 real_commit = transaction.commit
 real_rollback = transaction.rollback
 real_enter_transaction_management = transaction.enter_transaction_management
@@ -210,6 +226,7 @@
             transaction.rollback_unless_managed(using=conn)
 
 class TransactionTestCase(unittest.TestCase):
+
     def _pre_setup(self):
         """Performs any pre-test setup. This includes:
 
@@ -235,9 +252,13 @@
             call_command('flush', verbosity=0, interactive=False, database=db)
 
             if hasattr(self, 'fixtures'):
-                # We have to use this slightly awkward syntax due to the fact
-                # that we're using *args and **kwargs together.
-                call_command('loaddata', *self.fixtures, **{'verbosity': 0, 'database': db})
+                self._loaddata(db)
+
+
+    def _loaddata(self, db, commit=True):
+        # We have to use this slightly awkward syntax due to the fact
+        # that we're using *args and **kwargs together.
+        call_command('loaddata', *self.fixtures, **{'verbosity': 0, 'database': db, 'stderr': filtered_stderr, 'commit': commit})
 
     def _urlconf_setup(self):
         if hasattr(self, 'urls'):
@@ -506,11 +527,7 @@
 
         for db in databases:
             if hasattr(self, 'fixtures'):
-                call_command('loaddata', *self.fixtures, **{
-                                                            'verbosity': 0,
-                                                            'commit': False,
-                                                            'database': db
-                                                            })
+                self._loaddata(db, commit=False)
 
     def _fixture_teardown(self):
         if not connections_support_transactions():

comment:5 Changed 3 years ago by russellm

  • Resolution set to wontfix
  • Status changed from reopened to closed

Please don't reopen tickets that have been closed wontfix. If you want to discuss reopening a ticket, please start a thread on django-dev.

As for the proposed 'strawman' fix -- that patch requires that you store all output generated by the fixture loading process. This could get expensive, and fast.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.