1

I just moved tests.py file to a new directory called tests, then I added __init__.py file therein, but as I run the test python manage.py test it says ran 0 tests in 0.000s. How to solve it? I don't know how to show my files like most do, but here is an image!

enter image description here

This app is also added in settings.py Thanks

edit: this is a sample of test_models.py

from django.test import TestCase

# Create your tests here.
from django.test import TestCase
from django.urls import reverse
from board.models import Board
from board.views import board_topics


class HomeTest(TestCase):
    def home_test_case(self):
        url = reverse('home')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)


class BoardTest(TestCase):
    def setup(self):
        Board.objects.create(title='Hello world',
                             description='My first Django app!')

    def board_topics_status_code(self):
        url = reverse('board_topics', kwargs={id: 1})
        response = self.client.get(url)
        return self.assertEqual(response.status_code, 200)

    def board_topics_status_code_not_found(self):
        url = reverse('board_topics', kwargs={id: 123})
        response = self.client.get(url)
        return assertEqual(response.status_code, 200)

    def resolve_url_to_specific_fun(self):
        url = resolve('board/1/')
        return assertEqual(view.func, board_topics)


def HomeTests(TestCase):
    def setUp(self):
        self.board = Board.objects.create(
            title='Django', description='Django Desc')
        url = reverse('home')
        self.response = self.client.get(url)

    def home_test_view_status_code(self):
        self.assertEqual(self.response.status_code, 200)

    def home_test_func(self):
        view = resolve('/')
        self.assertEqual(view.func, home)

    def test_home_contains_link_to_topics_page(self):
        board_topics_url = reverse(
            'board_topics', kwargs={'id': self.board.pk})
        self.assertContains(self.response, 'href={0}'.format(board_topics_url))
Mir Stephen
  • 1,758
  • 4
  • 23
  • 54

2 Answers2

1

All your tests must have the test_ prefix attached. Rename all your tests adding that to the name.

For example:

def test_board_topics_status_code(self):
    url = reverse('board_topics', kwargs={id: 1})
    response = self.client.get(url)
    return self.assertEqual(response.status_code, 200)

Also, you need to change def HomeTests(TestCase): to class HomeTests(TestCase):, that's why your last test is named correctly but it is still not being discovered.

gdef_
  • 1,888
  • 9
  • 17
1

There are 2 ways you can keep your tests in your Project. I prefer the 1st one.

1. Your development code contains all of your tests. This way it's easier to add a new test when you write development code and your tests now ships with your development code.

Project
├── __init__.py
├── api
│   ├── v1
│   │   ├── tests
│   │   │   ├── __init__.py
│   │   │   ├── test_serializers.py
│   │   │   └── test_views.py
│   │   ├── __init__.py
│   │   ├── serializers.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── v2
│   │   ├── tests
│   │   │   ├── __init__.py
│   │   │   ├── test_serializers.py
│   │   │   └── test_views.py
│   │   ├── __init__.py
│   │   ├── serializers.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── __init__.py
│   ├── serializers.py
│   └── urls.py
├── models
│   ├── tests
│   │   ├── __init__.py
│   │   ├── test_data_structures.py
│   │   ├── test_miscellaneous_models.py
│   │   ├── test_models.py
│   ├── __init__.py
│   ├── models.py
│   ├── data_structures.py
│   └── miscellaneous_models.py
├── resume_handler
│   ├── tests
│   │   ├── __init__.py
│   │   ├── test_handlers.py
│   │   ├── test_managers.py
│   │   ├── test_parsers.py
│   │   ├── test_uploaders.py
│   │   └── test_validators.py
│   ├── __init__.py
│   ├── handlers.py
│   ├── managers.py
│   ├── parsers.py
│   ├── uploaders.py
│   └── validators.py
├── tasks
│   ├── tests
│   │   ├── __init__.py
│   │   └── test_tasks.py
│   ├── __init__.py
│   ├── general.py
│   └── model_tasks.py
├── tests
│   └── test_utils.py
└── utils.py

2. Another way is to put a test folder separate from the project folder. This test folder maintains the same hierarchy as of the project folder. This keeps the test code separate from the development code.

Project
├── api
│   ├── v1
│   │   └── more code files ...
│   ├── v2
│   │   └── more code files ...
│   └── v3
│       └── more code files ...
├── choices
├── constants
├── models
│   ├── data_filters
│   ├── querysets
│   └── more code files ...
├── resume_builder
│   └── more code files ...
├── resume_handler
│   └── more code files ...
├── tasks
│   └── more code files ...
└── more code files ...

Now in the same directory in which the Project folder is present create a test folder which maintains the same hierarchy but only contain the corresponding tests files.

test
├── api
│   ├── v1
│   │   └── test files ...
│   ├── v2
│   │   └── test files ...
│   └── v3
│       └── test files ...
├── choices
├── constants
├── models
│   ├── data_filters
│   ├── querysets
│   └── test files ...
├── resume_builder
│   └── test files ...
├── resume_handler
│   └── test files ...
├── tasks
│   └── test files ...
└── test files ...
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33