59๐
The behavior has changed in Django 1.6, so there is no longer a need to create a package. Just name your files test*.py
.
When you run your tests, the default behavior of the test utility is
to find all the test cases (that is, subclasses of unittest.TestCase)
in any file whose name begins with test, automatically build a test
suite out of those test cases, and run that suite.
From Django 1.6 documentation,
Test discovery is based on the unittest moduleโs built-in test
discovery. By default, this will discover tests in any file named
โtest*.pyโ under the current working directory.
Previous behavior, from Django 1.5 documentation:
When you run your tests, the default behavior of the test utility is
to find all the test cases (that is, subclasses of unittest.TestCase)
in models.py and tests.py, automatically build a test suite out of
those test cases, and run that suite.There is a second way to define the test suite for a module: if you
define a function called suite() in either models.py or tests.py, the
Django test runner will use that function to construct the test suite
for that module. This follows the suggested organization for unit
tests. See the Python documentation for more details on how to
construct a complex test suite.
139๐
Note that this approach is no longer valid from Django 1.6, see this post.
You can create tests
folder with ___init___.py
inside (so that it becomes a package). Then you add your split test .py files there and import all of them in ___init___.py
.
I.e: Substitute the test.py
file with a module that looks and acts like the file:
Create a tests
Directory under the app in question
app app\models.py app\views.py app\tests app\tests\__init__.py app\tests\bananas.py app\tests\apples.py
Import the submodules into app\tests\__init__.py
:
from bananas import *
from apples import *
Now you can use ./manage.py as if they were all in a single file:
./manage.py test app.some_test_in_bananas
- [Django]-No handlers could be found for logger
- [Django]-Django Rest Framework โ Authentication credentials were not provided
- [Django]-Django-Bower + Foundation 5 + SASS, How to configure?
29๐
The answer as stated by Tomasz is correct. However, it can become tedious to ensure that the imports in __init__.py
match your file structure.
To automatically detect all tests in the folder you can add this in __init__.py
:
import unittest
def suite():
return unittest.TestLoader().discover("appname.tests", pattern="*.py")
This will allow you to run ./manage.py test appname
but wonโt handle running specific tests. To do that you can use this code (also in __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
- [Django]-How do I run tests against a Django data migration?
- [Django]-What is the SQL ''LIKE" equivalent on Django ORM queries?
- [Django]-Django render_to_string missing information
25๐
Just make your directory structure like this:
myapp/
__init__.py
tests/
__init__.py
test_one.py
test_two.py
...
...
And python manage.py test myapp
will work as expected.
- [Django]-How to get the domain name of my site within a Django template?
- [Django]-How to automate createsuperuser on django?
- [Django]-How to use MySQLdb with Python and Django in OSX 10.6?
5๐
http://docs.python.org/library/unittest.html#organizing-tests talks about splitting the files into modules, and the section right above it has an example.
- [Django]-Django โ filtering on foreign key properties
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-Handle `post_save` signal in celery
4๐
With Django 2.2 a simple and fairly good solution could be to create a test
folder inside an app, and you can put your related test_...py
files into, just add __init__.py
to the test
folder.
- [Django]-How to run own daemon processes with Django?
- [Django]-How does Django's nested Meta class work?
- [Django]-Django admin and MongoDB, possible at all?
3๐
No need to code anything in init.
Just create a subdirectory in your app. Only requirement is not to call it tests*
For exemple
app/
app/__init_.py
app/serializers.py
app/testing/
app/testing/__init__.py
app/testing/tests_serializers.py
- [Django]-Why does django run everything twice?
- [Django]-Django Installed Apps Location
- [Django]-How to save pillow image object to Django ImageField?
1๐
If you have a more complicated setup, or donโt want to use from ... import *
-type statements, you can define a function called suite
in your tests.py (or tests/__init__.py), which returns an instance of unittest.TestSuite
.
- [Django]-Django-allauth social account connect to existing account on login
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-When saving, how can you check if a field has changed?
1๐
I have two files. One is tests.py
and another is test_api.py
. I can run these individually as below.
manage.py test companies.tests
manage.py test companies.test_api
Refer @osaโs response about file naming convention.
- [Django]-What is a NoReverseMatch error, and how do I fix it?
- [Django]-Python Django Gmail SMTP setup
- [Django]-Django Template Language: Using a for loop with else
0๐
I think ./manage.py test
simply does running all the tests trick (in django >= 1.7).
If your organizing tests is about grouping and cherrypicking and you are fan of nose
use django nose:
python manage.py test another.test:TestCase.test_method
If you know nose, then you know how to โwildcardโ much nicer over all your files.
PS
It is just a better practice. Hope that helps. The answer was borrowed from here: Running a specific test case in Django when your app has a tests directory
- [Django]-What is reverse()?
- [Django]-Celery discover tasks in files with other filenames
- [Django]-How can I create a deep clone of a DB object in Django?
0๐
In django you can use below comman or can check documentation. Also using this command will pick up files with pattern you provide not just test*.py
or test_*
.py.
Documentation
You can specify a custom filename pattern match using the -p (or โpattern) option, if your test files are named differently from the test*.py pattern:
$ ./manage.py test --pattern="tests_*.py"
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-Are there any plans to officially support Django with IIS?
- [Django]-Row level permissions in django
0๐
Just create different test files with tests_name in your app
Say you have following test files:
tests_admins.py
tests_staff.py
tests_others.py
# will run both test files
(venv)..yourapp$./manage.py test --keepdb -v 2 appname
- [Django]-Django development server reload takes too long
- [Django]-Django Reverse with arguments '()' and keyword arguments '{}' not found
- [Django]-Why am I getting this error in Django?
0๐
Or in Windows, if you do not want to create a package (i.e folder with __init__.py
) and just want to create a folder called "Tests" and this folder contains the test files then to run tests in cmd just enter
python manage.py test your_app_name/Tests
Since a path is expected
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
- [Django]-Pytest.mark.parametrize with django.test.SimpleTestCase
- [Django]-Django create userprofile if does not exist