9
votes

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?

2
do the tests work correctly if you try to run them locally through python manage.py test?BartDur
@BartDur yes. They also work correctly if I run them with DiscoveryRunner.run_tests, but they count as one test thenmilos
Can u use virtualenv on the remote machine?JACK ZHANG
Are you able to run them using nosetest locally?Tarun Lalwani
Are you able to add files to the root of the project?solarissmoke

2 Answers

7
votes

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
0
votes

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.