Skip to content

Automated Unit Testing

Nhat Le edited this page Jan 11, 2016 · 10 revisions

Introduction

We use nose to run our tests. We use mock to create fake objects that can be consumed by objects / methods we are actually testing.

Running tests

coverage run --source=. manage.py test -v 2 --settings=secondfunnel.settings.nosetests_runner

Debug

1. Can't create test database

(venv)vagrant@vagrant-ubuntu-trusty-64:/opt/secondfunnel/app$ python manage.py test apps/assets
nosetests apps/assets --verbosity=1
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_sfdb', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: database "test_sfdb" does not exist

Give the vagrant database user the CREATEDB permission.

postgres@vagrant-ubuntu-trusty-64:/opt/secondfunnel/app$ psql -h localhost
postgres=# ALTER USER sf CREATEDB;
ALTER ROLE
postgres=# \q

Note: if you get a Password: prompt, [[see this about setting a password for postgres user|Database:-Postgres#setting-a-postgres-user-password]].

2. Can't delete test database

(venv)vagrant@vagrant-ubuntu-trusty-64:/opt/secondfunnel/app$ python manage.py test apps/assets
nosetests apps/assets --verbosity=1
Creating test database for alias 'default'...
Got an error creating the test database: database "test_sfdb" already exists

Type 'yes' if you would like to try deleting the test database 'test_sfdb', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: database "test_sfdb" is being accessed by other users
DETAIL:  There is 1 other session using the database.

You will need to manually drop the test_sfdb database. This involves a) connecting to another database (sfdb), terminating any remaining sessions to test_sfdb. Then dropping test_sfdb.

postgres@vagrant-ubuntu-trusty-64:/opt/secondfunnel/app$ psql sfdb
sfdb=# SELECT * FROM pg_stat_activity;
 datid | datname   |  pid  | usesysid | usename  | application_name | ... 
-------+-----------+-------+----------+----------+------------------+----
 16385 | test_sfdb | 11778 |       10 | postgres | psql             | ...
 16385 | sfdb      | 11994 |       10 | postgres | psql             | ...
 16385 | sfdb      | 11691 |    16384 | sf       |                  | ...
(3 rows)
sfdb=# SELECT pg_terminate_backend(11778); -- pid from last command
 pg_terminate_backend 
----------------------
 t
(1 row)
sfdb=# DROP DATABASE test_sfdb;
DROP DATABASE
sfdb=# \q

More info

3. Permission denied error at the end

Ran 146 tests in 17.107s

OK
Destroying test database for alias 'default' ('test_sfdb')...
Traceback (most recent call last):
  - Removed to save space -
IOError: [Errno 13] Permission denied: '/opt/secondfunnel/app/.coverage'

To fix this, exit out of vagrant ssh or open a new terminal. cd to the parent folder of SecondFunnel directory, and run the command:

sudo chmod -R 777 secondfunnel

Go back to the VM and navigate to /opt/secondfunnel and run ls -ll. Check that the permissions for the folder app is drwxrwxrwx. Rerun the tests to make sure the error's gone.