Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django run all tests at once

I am using Django==1.7c2 and I wan't to say to Django find and run all tests in my project. I just created a folder with __init__.py which is named tests instead of putting all in tests.py. Here is my file structure;

my_project/apps/my_app/
├── __init__.py
├── tests
    ├── __init__.py
    ├── my_first_test.py
    ├── my_second_test.py
├── ....
├── ....
└── ....

and my __init__.py in tests file is like;

from my_first_test import *
from my_second_test import *

I thought that Django would recognise my test folder initially, but when I run just this command;

python manage.py test

There is no test case that Django find to run.

$ python manage.py test
Creating test database for alias 'default'...

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

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

I must run the tests for specific application in my project like;

python manage.py test my_project.apps.my_app.tests

It results;

----------------------------------------------------------------------
Ran 7 tests in 1.432s

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

I really don't like it, because I need to run all tests in all application in my project at once instead of running them one by one. I don't know what am I missing.

Is there a way to say Django find and run all test at once in all applications in the project?

Thank You!

#Edited

I have the apps in INSTALLED_APPS in my setting.py

like image 694
Ahmet DAL Avatar asked Sep 17 '14 08:09

Ahmet DAL


People also ask

How do I run tests PY in Django?

Open /catalog/tests/test_models.py.from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.

How do I test a Django project?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

How do I run test PY?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.

How do you write unit test cases in Django?

Writing tests Django's unit tests use a Python standard library module: unittest . This module defines tests using a class-based approach. When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.


1 Answers

Try this:

$ ./manage.py test --pattern="*_test.py"

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”.

Note

By the way you don't need import TestCases in __init__.py file if you are using standard test discovery (Django start using built-in test discovery since 1.6)

like image 147
Alex Lisovoy Avatar answered Sep 22 '22 00:09

Alex Lisovoy