[Django]-What is the simplest way to make manage.py test app.TestCase work again after replacing tests.py with tests/ directory?

2👍

I just do this tutorial.

Edit: after django 1.6, the test discovery mechanism changed. You just have to create a folder tests with an __init__.py file inside, and put your test files there.
Your test files should match test*.py pattern.

2👍

In your example, if you run manage.py test app.TestBananas then you can run that specific test.

You can get everything working by making sure all your tests are imported into __init__.py but when you have lots of tests this becomes difficult to manage. If you want to run the tests in PyCharm then django-nose isn’t an option.

To make this easier we can have the test suite automatically find all tests in the tests package. Just put this in __init__.py (Be sure to replace “appname”):

def suite():   
    return unittest.TestLoader().discover("appname.tests", pattern="*.py")

This still won’t allow us to run specific tests. To do that you’ll need to add this code at the top of __init__.py:

import pkgutil
import unittest

for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
    module = loader.find_module(module_name).load_module(module_name)
    for name in dir(module):
        obj = getattr(module, name)
        if isinstance(obj, type) and issubclass(obj, unittest.case.TestCase):
            exec ('%s = obj' % obj.__name__)

Now you can run all your tests via manage.py test app or specific ones via manage.py test app.TestApples

0👍

I use django-nose! Don’t be scared of packages.

0👍

Did you try renaming your test files to “test_foo.py”, instead of “foo.py”, for example?

Leave a comment