Testing

Whenever you write a new line of code, you also potentially add new bugs. To build better and more reliable applications, you should test your code using both functional and unit tests.

Acceptance Tests

Acceptance tests check the integration of all layers of your application (from the request and routing to the final response).

Your first Acceptance Test

Acceptance tests are simple test_*.py files that typically live in the tests/acceptance directory of your application. If you want to test the pages handled by your HelloWorldController class, start by creating a new test_hello_world_controller.py file with a class that extends a special apy.testing.ApplicationTestCase class.

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from apy.testing import ApplicationTestCase


class HelloWorldTestCase(ApplicationTestCase):
    def setUp(self):
        self.boot()

    def test_hello_world_text(self):
        response = self.request('GET', 'hello/world')
        self.assertEqual('Hello World!', response.get_content())

To run your acceptance tests, the class will configure your application with the self.boot(application_environment='test', environment_variable_of_config_dir='APPLICATION_CONFIG_DIR') method.

You will set your application config dir in a system environment variable (by default is APPLICATION_CONFIG_DIR)

By default your test will look for a settings_test.yml (environment test) config file in the config path, but you can customize it in the first parameter of the self.boot().

The following method

self.request(
    method,
    path=None,
    query=None,
    data=None,
    files=None,
    headers=None,
    host='',
    protocol='http',
    remote_ip=None,
    version=None
)

allows you to do requests to your application, so this method returns an instance of apy.http.Response.