Run Django tests with nosetests
Asked Answered
M

2

9

My python apps testing is performed on the remote server with command nosetests. I cannot modify the way tests are started nor can I add options to it. I have Django app with tests, but tests are not working properly.

My project structure:

project
├── README.md
├── setup.py
├── mysite
│   ├── blog
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
|   |   ├── ...
│   ├── db.sqlite3
│   ├── manage.py
│   ├── mysite
│   │   ├── __init__.py
│   │   ├── settings.py
|   |   ├── ...

Command nosetests is executed in project directory. I want it to properly run tests.py which has 2 Django testcases. I tried creating tests directory in project root and invoke tests with DiscoverRunner):

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
test_dir = os.path.dirname(os.path.dirname(__file__)) # one level up
sys.path.insert(0, os.path.join(test_dir, 'mysite'))

class ServerTest(unittest.TestCase):
    def test_runtests(self):
        django.setup()
        self.test_runner = DiscoverRunner(verbosity=1, interactive=True, failfast=False)
        failures = self.test_runner.run_tests(['mysite'])
        self.assertEqual(failures, 0)

It works but the problem is all the tests are considered as a single test and wrong reports are produced by the server.

Another solution: if I add empty __init__.py to project/mysite nose discovers tests.py but the tests fail because 'Apps are not loaded yet' which probably means I have to invoke django.setup() earlier but I don't know how to do it. I found a plugin for the nose which does it but I cannot install plugins or alter options on the remote machine.

Any ideas how to make any of my approaches solve the problem?

Manx answered 23/3, 2018 at 11:8 Comment(10)
do the tests work correctly if you try to run them locally through python manage.py test?Basilisk
@Basilisk yes. They also work correctly if I run them with DiscoveryRunner.run_tests, but they count as one test thenManx
Can u use virtualenv on the remote machine?Cogon
Are you able to run them using nosetest locally?Rockey
Are you able to add files to the root of the project?Deadpan
Cannot use virtualenv, @JACKZHANGManx
Yes, I can run them locally, but only as single test @TarunLalwaniManx
Yes, I can add files @DeadpanManx
Hey @milos, did you solve your problem? Was any of the answers helpful?Brimful
Hey, sorry, I missed your answer, @JohnMoutafisManx
B
7

Firsts things first, you should install and configure django-nose if you haven't done so already:

pip install django-nose

Then add it on your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'django_nose'
)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'  

Now for the nosetests command to run correctly, you can create a folder named tests with the following structure:

project
├── mysite
│   ├── blog
│   │   ├── __init__.py
│   │   ├── models.py
|   |   ├── ...
│   ├── mysite
│   │   ├── __init__.py
│   │   ├── settings.py
|   |   ├── ...
│   ├── tests
│   │   ├── __init__.py
│   │   ├── test_a_whole_class_of_methods.py  
│   │   ├── test_a_whole_view.py  
│   │   ├── test_one_of_my_methods.py
│   │   ├── test_another_one_of_my_methods.py  
|   |   ├── ...
│   ├── db.sqlite3
│   ├── manage.py
├── README.md
├── setup.py

DON'T FORGET THE __init__.py FILE INSIDE THE tests FOLDER!!!

Now when you run nosetests from the root of your project, it will run every test in the tests folder:

~ $ cd path/to/project
path/to/project $ nosetests
Brimful answered 3/4, 2018 at 8:0 Comment(6)
It tries to run every test in tests folder but gives "Apps are not yet loaded" exceptionManx
I assume that is because nosetests just tries to run tests as python files skipping django initializationManx
@milos This seems to be a problem similar to these: #35087050, reddit.com/r/django/comments/7mbk79/… ?Brimful
No, it's quite different to my opinion. We should somehow run django.setup() before tests or something like that for tests to work properly. You suggested to change settings.py in your answer (INSTALLED_APPS and TEST_RUNNER) but I don't think nosetests uses these changes it just runs filesManx
@milos If you want to use django.setup(), do it at the beginning of a test script, but because I am using django-nose, I am pretty sure that the cause is someplace else in your code...Brimful
How nosetests command knows it should use django-nose? I mean it just sees a bunch of tests that needs to be ranManx
D
0

Since you can add files to the root of your project, you could try adding a setup.cfg file there that nose will look for when executing, and in it specify where to look for tests:

# setup.cfg
[nosetests]
where=mysite/blog/

(See the documentation for what parameters you can put here).

I'm not sure that this will work (it is possible that the command that starts nose has already specified a different configuration file to use), but it seems worth a shot.

Deadpan answered 3/4, 2018 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.