I am unable to run my unit tests

Hi there,

Whenever I run my unit tests, I use this command python manage.py test, but my tests are not running, I get the following message:

Found 0 test(s).
System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

NOTE: I am using MySQL Database instead of the default SQLite. Does this have to do anything with the unit tests anyway?

I have following hierarchy of my project files and folders:

--[PROJECT-NAME]
------apps
----------[APP-NAME]
--------------migrations
--------------templates
--------------models.py
--------------test.py
--------------urls.py
--------------views.py
------[PROJECT-NAME]
----------settings.py
----------urls.py
------manage.py

And this is my tests.py file

from django.test import TestCase
from apps.accounts.models import User, Invitation

class TestModels(TestCase):    
    def test_sending_password_reset_email(self):
        user = User.objects.create(
            login = "testuser@test.com",
            email = "testuser@test.com",
            password = "TestPassword1!"
        )

        email_sent = user.send_password_reset_email()

        self.assertTrue(email_sent)

    def test_accepting_invitation(self):
        user = User.objects.create(
            login = "testuser@test.com",
            email = "testuser@test.com",
            password = "TestPassword1!"
        )

        invitation = Invitation.objects.create(created_by = user)

        accepted = invitation.accept(user, "testinguser@test.com", "TestingPassword1!")

        self.assertTrue(accepted)

    def test_cancelling_invitation(self):
        user = User.objects.create(
            login = "testuser@test.com",
            email = "testuser@test.com",
            password = "TestPassword1!"
        )

        invitation = Invitation.objects.create(created_by = user)

        invitation.cancel()

        self.assertTrue(invitation.is_canceled)

    def test_sending_invite_user_email(self):
        user = User.objects.create(
            login = "testuser@test.com",
            email = "testuser@test.com",
            password = "TestPassword1!"
        )

        invitation = Invitation.objects.create(created_by = user)

        message = invitation.send_invite_user_email()

        self.assertEqual(message, "Success")

and these are the function signatures in my models.py:

def send_password_reset_email(self) -> bool:
    .
    .
    .

def accept(
        self,
        user: User,
        login: str,
        password: str
) -> bool:
    .
    .
    .

def cancel(self):
    .
    .
    .

def send_invite_user_email(self) -> str:
    .
    .
    .

Anyone knows what wrong I am doing? Why my unit tests are not running?

Hello @khaw235

Are you able to run? :

python manage.py runserver

without complains?

@Rigo-Villalta yes, I am able to run the server, but I get the following warning though:

WARNINGS:
?: (staticfiles.W004) The directory 'D:\Projects\meistery\projects\pricing_password_management_poc\inviteandresetpass\static' in the STATICFILES_DIRS setting does not exist.

@Rigo-Villalta I solved this warning by creating a folder named static in the project directory, but I am still unable to get any test run, it shows same message Ran 0 tests

try to run the test file directly:

python manage.py test apps/app_name/test.py

And please post the result here.

@Rigo-Villalta I ran the command as you said and I got following Traceback:

python manage.py test apps/accounts/tests.py
Traceback (most recent call last):
  File "D:\Projects\meistery\projects\pricing_password_management_poc\inviteandresetpass\manage.py", line 22, in <module>
    main()
  File "D:\Projects\meistery\projects\pricing_password_management_poc\inviteandresetpass\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\management\commands\test.py", line 24, in run_from_argv    super().run_from_argv(argv)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv        
    self.execute(*args, **cmd_options)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\management\base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\core\management\commands\test.py", line 68, in handle       
    failures = test_runner.run_tests(test_labels)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\test\runner.py", line 994, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\test\runner.py", line 847, in build_suite
    tests = self.load_tests_for_label(label, discover_kwargs)
  File "D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\test\runner.py", line 809, in load_tests_for_label
    raise RuntimeError(
RuntimeError: One of the test labels is a path to a file: 'apps/accounts/tests.py', which is not supported. Use a dotted module name or path to a directory instead.

Sorry, my mistake.

Try this:

python manage.py test apps/accounts/
1 Like

I got this now:

Found 4 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\__init__.py:1534: RuntimeWarning: DateTimeField Invitation.accepted_at received a naive datetime (2022-07-14 00:47:42.187885) while time zone support is active.
  warnings.warn(
..D:\Projects\meistery\venvs\inviteandresetpass\lib\site-packages\django\db\models\fields\__init__.py:1534: RuntimeWarning: DateTimeField Invitation.expiration_date received a naive datetime (2022-07-15 00:47:42.308024) while time zone support is active.
  warnings.warn(
..
----------------------------------------------------------------------
Ran 4 tests in 1.127s

OK
Destroying test database for alias 'default'...

OK, its working now. So I have two questions:

  • Why does not it run with python manage.py test? And how can I make these tests run with this command?
  • Are these tests passed? I mean my functions whom I am testing are correct?

I think you havenā€™t add ā€œappsā€ to your path. See this

Yes they are correct, but you have a warning. I think the problem is that you are using datetime.now in your models, that is not the best practice, read this.

1 Like

I added ā€˜appsā€™ to the path following the method told in the link by adding sys.path.append(str(BASE_DIR / 'apps')), but I still get Ran 0 tests on running python manage.py test command.

That StackOverflow answer was very old. On recent Django versions, you donā€™t need that to add the apps to the path yourself. If BASE_DIR is pointing to the right location and the app is listed in INSTALLED_APPS, django discovers the app automatically.

One thing to try is to rename your test.py file to tests.py.

Another idea, because you have your apps under an apps folder, is to ensure itā€™s being listed correctly.

Make sure you are using the full name on settings.py and apps.py:

#settings.py

INSTALLED_APPS = [
   ...
   "apps.myapp",
   ...
]
#app.py
from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = "apps.myapp"
1 Like