Add Django Testing framework to apps scaffold

15 views
Skip to first unread message

shawncrawley

unread,
Aug 4, 2016, 4:56:21 PM8/4/16
to Tethys Platform
I propose that we add into the tethys scaffold a 'bare-bones' framework for creating your own app tests that can then be run with a corresponding tethys command (i.e. "tethys manage test my_app.tests"). 

I wrote some tests for an app I'm working on, and I tried for a while to find out how to run my tests on the server from the command line, but couldn't. Maybe it's easier than I now think, but I just couldn't figure it out quickly enough to feel comfortable about my time use. I imagine you would know how this could be done, Nathan? 

I know that Django provides a framework for testing that is executed by running 'python manage.py test.' When I tried to run that command, I kept getting the following message:

Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database


Type 'yes' if you would like to try deleting the test database 'test_tethys_default', or 'no' to cancel:


For now I have just made my testing function a controller that I access with through a specific URL, but it would be nice to be able to test more officially through the terminal. But again, would it be possible to have the 'tethys scaffold' command create a 'tests' folder that already has the '__init__.py' and a python file with a Django test class already implemented in a very basic form to give app creators a starting point to make tests if they would like? What are your thoughts?

sdc50

unread,
Aug 4, 2016, 8:35:05 PM8/4/16
to Tethys Platform
@shawncrawley, I just recently added a testing framework to the Tethys code (see this post). It has a link to the wiki that provides instructions for how to set up and run tests. It is just a start for now, but the idea is eventually to expand it to provide an easy mechanism for app developers to develop tests for their apps as you are suggesting. 

The error you are getting when trying to run your tests is because the database user doesn't have privileges to create a new database and the Django testing framework creates a separate testing database when it runs tests. The easiest way to get around that is to change the user from tethys_default to tethys_super in your settings file (see the wiki for details).

Shawn Crawley

unread,
Aug 4, 2016, 8:49:12 PM8/4/16
to sdc50, Tethys Platform
Awesome. I knew about the testing framework you had added, but I guess I didn't think of it as applying to testing individual apps, too. This is good to know. So would I need to place my app test code in the appropriate tethys/tests directory? I'm still wondering if there could be a way to wire tethys such that each individual app can have a 'tests' folder that the tethys test command will execute. Almost like the static folder and 'tethys collectstatic' command - there is one main static folder that tethys uses, but each app has its own static folder that defines what will be moved to the tethys/static folder. Or am I understanding that wrong? Either way, I think having a tests folder generated for the app with a basic test class already written would be helpful. Because isn't there a certain format/organization that must be followed for the tests to run properly? The test class must inherit from a Django Test superclass, and so forth?

--
You received this message because you are subscribed to the Google Groups "Tethys Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tethysplatfor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tethysplatform/4f54fe69-e1f7-4a6d-abb2-2d5c2a01aff3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott Christensen

unread,
Aug 4, 2016, 10:48:02 PM8/4/16
to Shawn Crawley, Tethys Platform
The plan is definitely to have it apply to testing individual apps. We will add a testing directory to the scaffold, and add some more options to the cli to enable tests to be run for apps.  

In the meantime I'm pretty sure you can run tests in any file like this:

(tethys)$ tethys test path/to/test/file

If that doesn't work then you can try:

(tethys)$ python manage.py test path/to/test/file  

Django doesn't mandate where test files live. It just uses the unittest module to do testing which has some auto discovery tools to find tests, but you can also just point it at any file and it will run tests in that file. I would suggest creating your own test folder in your app and putting your test files there. Then run the tests by pointing at that folder. 

Only tests that use models need to inherit from the Django TestCase superclass, but you can also write tests that just use the unittest TestCase class as long as they don't need to use the database (see https://docs.djangoproject.com/en/1.9/topics/testing/overview/).
--
Scott D. Christensen, E.I.T
Research Assistant 
Brigham Young University
Reply all
Reply to author
Forward
0 new messages