Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does django not see my tests?

I've created test.py module, filled with

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from forum.models import *

class SimpleTest(TestCase):


    def setUp(self):
        u = User.objects.create_user("ak", "[email protected]", "pwd")
        Forum.objects.create(title="forum")
        Site.objects.create(domain="test.org", name="test.org")

    def content_test(self, url, values):
        """Get content of url and test that each of items in `values` list is present."""
        r = self.c.get(url)
        self.assertEquals(r.status_code, 200)
        for v in values:
            self.assertTrue(v in r.content)

    def test(self):
        self.c = Client()
        self.c.login(username="ak", password="pwd")

        self.content_test("/forum/", ['<a href="/forum/forum/1/">forum</a>'])
        ....

and placed it in folder with my application. When i run tests by

python manage.py test forum

after creating the test database i get an answer "Ran 0 tests in 0.000s"

What am i doing wrong ?

P.S. Here is my project hierarchy:

MyProj:
    forum (it's my app):
        manage.py
        models.py
        views.py
        tests.py
        ...

I renamed test.py to tests.py. Eclipse got that this module is with tests, but answer is still "Ran 0 tests in 0.000s"

like image 768
user441495 Avatar asked Sep 07 '10 14:09

user441495


People also ask

How do I run a test file in Django?

Open /catalog/tests/test_models.py.TestCase , as shown: 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 my Django site?

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.

Does Django test use Pytest?

Setting Up pytest for a Django Project The pytest-django plugin is maintained by the pytest development team. It provides useful tools for writing tests for Django projects using pytest . This is the minimum amount of configuration needed to make pytest work with your Django project.


2 Answers

After some time spent in searching I did not find anyone suggesting this, so I will share it as a late answer. In my case I have my manage.py in the root directory eg

.
...
├── dir
│   ├── project_name
│   ├── manage.py
│   ├── media
│   ├── app1
│   ├── static
│   ├── staticfiles
│   ├── templates
│   └── app2
...

so I found that the test command has the option to provide project which tests' to run. In my case I had to do this

python project_name/manage.py test ./project_name/

This successfully ran my tests.

like image 54
Zarrie Avatar answered Sep 20 '22 13:09

Zarrie


You´ll need to use the prefix test_ for each test method.

like image 40
pedro Avatar answered Sep 21 '22 13:09

pedro