4
Nova contains a suite of unit tests, in the nova/tests directory.
6
Any proposed code change will be automatically rejected by the OpenStack
7
Jenkins server [#f1]_ if the change causes unit test failures.
11
Run the unit tests by doing::
15
This script is a wrapper around the `nose`_ testrunner and the `pep8`_ checker.
17
.. _nose: http://code.google.com/p/python-nose/
18
.. _pep8: https://github.com/jcrocholl/pep8
23
The ``run_tests.sh`` script supports several flags. You can view a list of
28
This will show the following help information::
30
Usage: ./run_tests.sh [OPTION]...
31
Run Nova's test suite(s)
33
-V, --virtual-env Always use virtualenv. Install automatically if not present
34
-N, --no-virtual-env Don't use virtualenv. Run tests in local environment
35
-s, --no-site-packages Isolate the virtualenv from the global Python environment
36
-r, --recreate-db Recreate the test database (deprecated, as this is now the default).
37
-n, --no-recreate-db Don't recreate the test database.
38
-x, --stop Stop running tests after the first error or failure.
39
-f, --force Force a clean re-build of the virtual environment. Useful when dependencies have been added.
40
-p, --pep8 Just run pep8
41
-P, --no-pep8 Don't run pep8
42
-c, --coverage Generate coverage report
43
-h, --help Print this usage message
44
--hide-elapsed Don't print the elapsed time for each test along with slow test list
46
Because ``run_tests.sh`` is a wrapper around nose, it also accepts the same
47
flags as nosetests. See the `nose options documentation`_ for details about
48
these additional flags.
50
.. _nose options documentation: http://readthedocs.org/docs/nose/en/latest/usage.html#options
52
Running a subset of tests
53
-------------------------
55
Instead of running all tests, you can specify an individual directory, file,
56
class, or method that contains test code.
58
To run the tests in the ``nova/tests/scheduler`` directory::
60
./run_tests.sh scheduler
62
To run the tests in the ``nova/tests/test_libvirt.py`` file::
64
./run_tests.sh test_libvirt
66
To run the tests in the `HostStateTestCase` class in
67
``nova/tests/test_libvirt.py``::
69
./run_tests.sh test_libvirt:HostStateTestCase
71
To run the `ToPrimitiveTestCase.test_dict` test method in
72
``nova/tests/test_utils.py``::
74
./run_tests.sh test_utils:ToPrimitiveTestCase.test_dict
77
Suppressing logging output when tests fail
78
------------------------------------------
80
By default, when one or more unit test fails, all of the data sent to the
81
logger during the failed tests will appear on standard output, which typically
82
consists of many lines of texts. The logging output can make it difficult to
83
identify which specific tests have failed, unless your terminal has a large
84
scrollback buffer or you have redirected output to a file.
86
You can suppress the logging output by calling ``run_tests.sh`` with the nose
94
By default, the tests use the Python packages installed inside a
95
virtualenv [#f2]_. (This is equivalent to using the ``-V, --virtualenv`` flag).
96
If the virtualenv does not exist, it will be created the first time the tests are run.
98
If you wish to recreate the virtualenv, call ``run_tests.sh`` with the flag::
102
Recreating the virtualenv is useful if the package dependencies have changed
103
since the virtualenv was last created. If the ``tools/pip-requires`` or
104
``tools/install_venv.py`` files have changed, it's a good idea to recreate the
107
By default, the unit tests will see both the packages in the virtualenv and
108
the packages that have been installed in the Python global environment. In
109
some cases, the packages in the Python global environment may cause a conflict
110
with the packages in the virtualenv. If this occurs, you can isolate the
111
virtualenv from the global environment by using the flag::
113
-s, --no-site packages
115
If you do not wish to use a virtualenv at all, use the flag::
122
Some of the unit tests make queries against an sqlite database [#f3]_. By
123
default, the test database (``tests.sqlite``) is deleted and recreated each
124
time ``run_tests.sh`` is invoked (This is equivalent to using the
125
``-r, --recreate-db`` flag). To reduce testing time if a database already
126
exists it can be reused by using the flag::
130
Reusing an existing database may cause tests to fail if the schema has
131
changed. If any files in the ``nova/db/sqlalchemy`` have changed, it's a good
132
idea to recreate the test database.
135
.. rubric:: Footnotes
137
.. [#f1] See :doc:`jenkins`.
139
.. [#f2] See :doc:`development.environment` for more details about the use of
142
.. [#f3] There is an effort underway to use a fake DB implementation for the
143
unit tests. See https://lists.launchpad.net/openstack/msg05604.html