0

I am new to Django Unit testing and am trying to write unit test for a simple function which is like below:

utils.py
#some import statements
from django.conf import settings

if platform.system() == 'Linux':
    cx_Oracle.init_oracle_client(lib_dir=settings.GAP_DB_LIB_DIR)
else:
    cx_Oracle.init_oracle_client(lib_dir=settings.LOCAL_DB_LIB_DIR)
db_password = settings.DB_PASS
db_user = settings.DB_USER
db_connection_dsn = settings.DB_CONNECTION_DSN

def project(repo):
    pieces = parse.urlsplit(repo)
    return pieces.path.rstrip('/').split('/')[-1]

I am writing unit tests in test.py fie as below

from django.test import TestCase
from common import utils
from utils import project
from django.test import override_settings

# Create your tests here.
class UtilsTest(TestCase):
    def setUp(self):
        self.repo = "http://test/testrepo"

    @override_settings(LOCAL_DB_LIB_DIR="c:/testdir")
    def test_project(self):
        response = utils.project(self.repo)
        self.assertEqual(response,"testrepo")

When I run tests am getting below error: AttributeError: 'Settings' object has no attribute 'LOCAL_DB_LIB_DIR' Please help me where I am doing wrong and correct my coding. Thanks in advance.

  • You have to note that your import is outside of your test method `from common import utils` and given that you have code running in `utils.py` outside any function as soon as the file is imported that code is going to be run. – Abdul Aziz Barkat Oct 21 '22 at 08:38
  • Both utils.py and test.py are present in same app which is common – Susmitha Thallapally Oct 21 '22 at 08:40
  • Did I even say anything about that? The code obviously runs top to bottom therefore before your `override_settings` call ever happens your import is run, once the file is imported `cx_Oracle.init_oracle_client(lib_dir=settings.LOCAL_DB_LIB_DIR)` is run and you get the error... – Abdul Aziz Barkat Oct 21 '22 at 08:43
  • Tried moving override_settings import statement to top and tried I got same error. – Susmitha Thallapally Oct 21 '22 at 09:07

0 Answers0