Django 1.8 - How do I test a specific file inside a tests folder?
Asked Answered
C

3

6

This is my directory:

CMSApp/tests/test_page.py
CMSApp/tests/test_user.py
CMSApp/models.py
CMSApp/views.py

I want to test test_page.py only. I can do this:

python manage.py test CMSApp/tests

But that will test both test_page.py and test_user.py. When I try

python manage.py test CMSApp/tests/test_page

It says:

No module named CMSApp/tests/test_page

And when I try:

python manage.py test CMSApp/tests/test_page.py it says NoneType object is not iterable.

Ceporah answered 27/10, 2015 at 21:34 Comment(5)
Possible duplicate of Running a specific test case in Django when your app has a tests directoryPassion
what you get when you try without the extension .py of test_page at the end?Janssen
@Janssen It says No module named CMSApp/tests/test_page.Ceporah
i think the problem could be the name of the folder 'tests' because you could test by doing : python manage.py test CMSAPP.<other_folder>.test_page.tests the last 'tests' is a built-in function which could be confuse with your folder name :testsJanssen
@Janssen tests is the recommended name of the test module. See docs.djangoproject.com/en/1.8/topics/testing/overview/…Deckle
B
12
python manage.py test CMSApp.tests.test_page

You need to have __init__.py in tests directory to make it a module.

Bennettbenni answered 27/10, 2015 at 21:46 Comment(0)
S
1

There are several ways how to run only specific test(s), as explained in documentation.

# Run all the tests in the animals.tests module
$ ./manage.py test animals.tests

# Run all the tests found within the 'animals' package
$ ./manage.py test animals

# Run just one test case
$ ./manage.py test animals.tests.AnimalTestCase

# Run just one test method
$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak

# You can also provide a path to a directory to discover tests below that directory:

$ ./manage.py test animals/

# 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"

I found especially convenient the use of --pattern parameter, where you could just supply the unique part of test file name. For example in your situation you could just write:

python manage.py test --pattern "*test_page*"

This way you don't even have to look up the whole path to your test file.

Seagirt answered 9/3, 2019 at 7:33 Comment(0)
P
0
python manage.py test tests.main.testMainPage.MainPageTests

Where tests and main are folders and need to have the init.py file, testMainPage is a file within main and MainPageTests is a class of this file.

tree view:

tests
├── __init__.py
├── main
│   ├── __init__.py
│   ├── testMainPage.py

The class MainPageTests will hold all your tests example:

class MainPageTests(TestCase):
    def test_my_view(self):
    pass
Passion answered 27/10, 2015 at 21:59 Comment(3)
My file structure does not have a main folder inside tests. How can I accomplish the task without the main folder inside tests?Ceporah
@user2719875 yes it is possbile, you will run python manage.py test tests.testMainPage.MainPageTestsPassion
Does it work for you? When I do python manage.py test tests.test_user.UserTests it gives an error saying AttributeError: 'module' object has no attribute 'test_user'.Ceporah

© 2022 - 2024 — McMap. All rights reserved.