2

I've been running unit tests (test.py) on my Django app for ages with no trouble. In my last session I was tinkering with my MYSQL database and models. I have been dropping and recreating the DB, deleting the migrations file and making migrations from scratch while I experiment with the models.

Now, the unittests won't run.

Traceback (most recent call last):
  File "/opt/pycharm-2017.2.1/helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
  File "/usr/lib/python3.5/unittest/main.py", line 93, in __init__
    self.parseArgs(argv)
  File "/usr/lib/python3.5/unittest/main.py", line 140, in parseArgs
    self.createTests()
  File "/usr/lib/python3.5/unittest/main.py", line 147, in createTests
    self.module)
  File "/usr/lib/python3.5/unittest/loader.py", line 219, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python3.5/unittest/loader.py", line 219, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python3.5/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "/home/danny/PycharmProjects/AskArby/deals/tests.py", line 3, in <module>
    from deals.models import Retailer
  File "/home/danny/PycharmProjects/AskArby/deals/models.py", line 5, in <module>
    class Retailer(models.Model):
  File "/home/danny/PycharmProjects/AskArby/deals/models.py", line 6, in Retailer
    name = models.CharField(max_length=200)
  File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1061, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 172, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

PyCharm also tells me no tests were found.

This is the beginning of models.py:

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Retailer(models.Model):
    name = models.CharField(max_length=200)
    base_url = models.URLField(max_length=500)
    currency = models.CharField(max_length=3)
    #css parameters for finding deals and subfields in HTML
    deal_container_css = models.CharField(max_length=300)
    title_css = models.CharField(max_length=300)
    price_css = models.CharField(max_length=300)
    old_price_css = models.CharField(max_length=300, null=True, blank=True)
    brand_css = models.CharField(max_length=300, null=True, blank=True)
    image_css = models.CharField(max_length=300, null=True, blank=True)
    description_css = models.CharField(max_length=300, null=True, blank=True)
    #for things that look like deal containers but aren't
    exclude_css = models.CharField(max_length=300, null=True, blank=True)
    shipping_css = models.CharField(max_length=300, null=True, blank=True)
    product_model_css = models.CharField(max_length=300, null=True, blank=True)

tests.py:

from django.test import TestCase
import unittest
from deals.models import Retailer

class RetailerTests(TestCase):
    fixtures = ['initial_data.json']

class TestRetailer(unittest.TestCase):

    def setUp(self):
        self.bestbuy = Retailer(Retailer.objects.get(pk=1))

    def test_name(self):
        self.assertEqual(self.bestbuy.name, 'Best Buy')

I'm using Pycharm, running runserver as a manage.py command. I have tried setting DJANGO_SETTINGS_MODULE from the command line, and it echos back the right answer: AskArby.settings. I try to call settings.configure() and it tells me settings are already configured.

Also, when I run runserver I get this warning:

?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DIRS.
deals.Marketplace.listing: (fields.W340) null has no effect on ManyToManyField.

Why is my app seemingly failing to run my unit tests in test.py?

2
  • Can you explain the procedure how you run the test.py Commented Sep 20, 2017 at 7:43
  • I was using Pycharm's built-in 'run' function. When I use python manage.py test from the shell, the tests run fine. So it seems that Pycharm is the problem. Perhaps I will try creating a new Pycharm project and importing my files to it. Commented Sep 20, 2017 at 15:37

3 Answers 3

3

if u run from ide and not from comandline,pls add thses code before your run method:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[your appname].settings")
import django
django.setup()

good luck to u!

Sign up to request clarification or add additional context in comments.

Comments

0

Today I ran some test modules in PyCharm that have been run hundreds of times before and instead I got the Django ImproperlyConfigured error.

I found PyCharm wasn't correctly detecting that files were Django tests rather than regular Python files (it shows a different icon in the run tab for Django tests which includes the Django logo). I ran them a few times and eventually PyCharm got bored of annoying me (this took about 30 minutes or so) and detected them correctly. Don't ask me what was going on. I didn't restart the IDE. I just ran various test modules a few times and poked around. So -- this problem is not always user error, sometimes it is a bug in the IDE. Unfortunately that means I do not have a fix. Maybe JetBrains do?

Comments

0

I had exactly the same issue and came here looking for an answer. I think the problem is that you are actually basing your TestRetailer test case on Python unittest.TestCase, not django.test.TestCase.

Your code begins...

from django.test import TestCase
import unittest
from deals.models import Retailer
...

class TestRetailer(unittest.TestCase):
...

So your test class for TestRetailer is actually based on the standard Python TestCase. That matters, as the Django TestCase includes some Django specific initiatialisation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.