Testing API¶
Last Updated: November 18, 2016
Manually testing your app can be very time consuming, especially when modifying a simple line of code usually warrants retesting everything. To help automate and streamline the testing of your app, Tethys Platform provides you with a great starting point by providing the following:
A
tests
directory with atests.py
script within your app's default scaffold that contains well-commented sample testing code.The Testing API which provides a helpful test class for setting up your app's testing environment.
Writing Tests¶
Tests should be written in a separate python script that is contained somewhere within your app's scaffold. By default, a tests
directory already exists in the app-level directory and contains a tests.py
script. Unless you have a good reason not to, it would be best to start writing your test code here.
As an example, if wanting to automate the testing of a the map controller in the "My First App" from the tutorials, the tests.py
script might be modified to look like the following:
from tethys_sdk.testing import TethysTestCase
from ..app import MyFirstApp
class MapControllerTestCase(TethysTestCase):
def set_up(self):
self.create_test_persistent_stores_for_app(MyFirstApp)
self.create_test_user(username="joe", email="joe@some_site.com", password="secret")
self.c = self.get_test_client()
def tear_down(self):
self.destroy_test_persistent_stores_for_app(MyFirstApp)
def test_success_and_context(self):
self.c.force_login(self.user)
response = self.c.get('/apps/my-first-app/map/')
# Check that the response returned successfully
self.assertEqual(response.status_code, 200)
# Check that the response returned the context variable
self.assertIsNotNone(response.context['map_options'])
Tethys Platform leverages the native Django testing framework (which leverages the unittests Python module) to make writing tests for your app much simpler. While Tethys Platform encapsulates most of what is needed in its Testing API, it may still be necessary to refer to the Django and Python documentation for additional help while writing tests. Refer to their documentation here:
https://docs.djangoproject.com/en/1.9/topics/testing/overview/#writing-tests
https://docs.python.org/2.7/library/unittest.html#module-unittest
Running Tests¶
To run any tests at an app level:
Open a terminal
- Enter the Tethys Platform python environment:
$ . /usr/lib/tethys/bin/activate
- Enter app-level
tethys test
command.
(tethys)$ tethys test -f tethys_apps.tethysapp.<app_name(required)>.<folder_name>.<file_name>.<class_name>.<function_name>
More specifically:
- To run all tests across an app:
Test command:
(tethys)$ tethys test -f tethys_apps.tethysapp.<app_name>
- To run all tests within specific directory of an app:
Test command:
(tethys)$ tethys test -f tethys_apps.tethysapp.<app_name>.<folder_name>
And so forth... Thus, you can hone in on the exact tests that you want to run.
Note
Remember to append either
-c
or-C
if you would like a coverage report at the end of the testing printed in your terminal, or opened in your browser as an interactive HTML page, respectively.
API Documentation¶
-
class
tethys_apps.base.testing.testing.
TethysTestCase
(methodName='runTest')¶ This class inherits from the Django TestCase class and is itself the class that is should be inherited from when creating test case classes within your app. Note that every specific test written within your custom class inheriting from this class must begin with the word "test" or it will not be executed during testing.
-
static
create_test_persistent_stores_for_app
(app_class)¶ Creates temporary persistent store databases for this app to be used in testing.
- Parameters
app_class -- The app class from the app's app.py module
- Returns
None
-
static
create_test_superuser
(username, password, email=None)¶ Creates and returns a temporary superuser to be used in testing
- Parameters
username (string) -- The username for the temporary test user
password (string) -- The password for the temporary test user
email (string) -- The email address for the temporary test user
- Returns
User object
-
static
create_test_user
(username, password, email=None)¶ Creates and returns temporary user to be used in testing
- Parameters
username (string) -- The username for the temporary test user
password (string) -- The password for the temporary test user
email (string) -- The email address for the temporary test user
- Returns
User object
-
static
destroy_test_persistent_stores_for_app
(app_class)¶ Destroys the temporary persistent store databases for this app that were used in testing.
- Parameters
app_class -- The app class from the app's app.py module
- Returns
None
-
static
get_test_client
()¶ Returns a Client object to be used to mimic a browser in testing
- Returns
Client object
-
set_up
()¶ This method is to be overridden by the custom test case classes that inherit from the TethysTestCase class and is used to perform any set up that is applicable to every test function that is defined within the custom test class
- Returns
None
-
tear_down
()¶ This method is to be overridden by the custom test case classes that inherit from the TethysTestCase class and is used to perform any tear down that is applicable to every test function that is defined within the custom test class. It is often used in conjunction with the "set_up" function to tear down what was setup therein.
- Returns
None
-
static