Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Django tests with nosetests

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?

like image 990
milos Avatar asked Mar 23 '18 11:03

milos


People also ask

What is Django nose?

django-nose provides all the goodness of nose in your Django tests, like: Testing just your apps by default, not all the standard ones that happen to be in INSTALLED_APPS. Running the tests in one or more specific modules (or apps, or classes, or folders, or just running a specific test)

Does Django test use Pytest?

pytest-django Documentation. pytest-django is a plugin for pytest that provides a set of useful tools for testing Django applications and projects.

How do I run test py?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.


1 Answers

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
like image 70
John Moutafis Avatar answered Sep 27 '22 17:09

John Moutafis