Skip to content

Automated Unit Testing

Jeremy Rutledge edited this page Nov 11, 2015 · 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. Test Database Does Not Exist

nosetests --verbosity=2
Creating test database for alias 'default' ('test_test_sfdb')...
Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/south/management/commands/test.py", line 8, in handle
    super(Command, self).handle(*args, **kwargs)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django_nose/runner.py", line 200, in run_tests
    result = self.run_suite(nose_argv)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django_nose/runner.py", line 147, in run_suite
    addplugins=plugins_to_add)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/nose/core.py", line 118, in __init__
    **extra_args)
  File "/usr/lib/python2.7/unittest/main.py", line 95, in __init__
    self.runTests()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/nose/core.py", line 197, in runTests
    result = self.testRunner.run(self.test)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/nose/core.py", line 50, in run
    wrapper = self.config.plugins.prepareTest(test)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 99, in __call__
    return self.call(*arg, **kw)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 167, in simple
    result = meth(*arg, **kw)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django_nose/plugin.py", line 75, in prepareTest
    self.old_names = self.runner.setup_databases()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django_nose/runner.py", line 383, in setup_databases
    return super(NoseTestSuiteRunner, self).setup_databases()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/test/runner.py", line 107, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/test/runner.py", line 279, in setup_databases
    verbosity, autoclobber=not interactive)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 326, in create_test_db
    self._create_test_db(verbosity, autoclobber)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 387, in _create_test_db
    cursor = self.connection.cursor()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 159, in cursor
    cursor = util.CursorWrapper(self._cursor(), self)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 129, in _cursor
    self.ensure_connection()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 124, in ensure_connection
    self.connect()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 124, in ensure_connection
    self.connect()
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 112, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 116, in get_new_connection
    return Database.connect(**conn_params)
  File "/opt/secondfunnel/venv/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
OperationalError: FATAL:  database "test_sfdb" does not exist```

All you have to do is create the database in the postgres user.

```$ sudo su postgres
postgres@vagrant-ubuntu-trusty-64:/opt/secondfunnel/app$ psql
psql (9.3.10)
Type "help" for help.

postgres=# CREATE DATABASE test_sfdb;
CREATE DATABASE```

Clone this wiki locally