~yolanda.robla/nova/precise-security

« back to all changes in this revision

Viewing changes to doc/source/devref/unit_tests.rst

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-02 10:56:30 UTC
  • mto: (79.1.1 precise-proposed)
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: package-import@ubuntu.com-20111202105630-tjly1gpmdh533s0q
Tags: upstream-2012.1~e2~20111202.11641
ImportĀ upstreamĀ versionĀ 2012.1~e2~20111202.11641

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Unit Tests
 
2
==========
 
3
 
 
4
Nova contains a suite of unit tests, in the nova/tests directory.
 
5
 
 
6
Any proposed code change will be automatically rejected by the OpenStack
 
7
Jenkins server [#f1]_ if the change causes unit test failures.
 
8
 
 
9
Running the tests
 
10
-----------------
 
11
Run the unit tests by doing::
 
12
 
 
13
    ./run_tests.sh
 
14
 
 
15
This script is a wrapper around the `nose`_ testrunner and the `pep8`_ checker.
 
16
 
 
17
.. _nose: http://code.google.com/p/python-nose/
 
18
.. _pep8: https://github.com/jcrocholl/pep8
 
19
 
 
20
Flags
 
21
-----
 
22
 
 
23
The ``run_tests.sh`` script supports several flags. You can view a list of
 
24
flags by doing::
 
25
 
 
26
    run_tests.sh -h
 
27
 
 
28
This will show the following help information::
 
29
 
 
30
    Usage: ./run_tests.sh [OPTION]...
 
31
    Run Nova's test suite(s)
 
32
 
 
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
 
45
 
 
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.
 
49
 
 
50
.. _nose options documentation: http://readthedocs.org/docs/nose/en/latest/usage.html#options
 
51
 
 
52
Running a subset of tests
 
53
-------------------------
 
54
 
 
55
Instead of running all tests, you can specify an individual directory, file,
 
56
class, or method that contains test code.
 
57
 
 
58
To run the tests in the ``nova/tests/scheduler`` directory::
 
59
 
 
60
    ./run_tests.sh scheduler
 
61
 
 
62
To run the tests in the ``nova/tests/test_libvirt.py`` file::
 
63
 
 
64
    ./run_tests.sh test_libvirt
 
65
 
 
66
To run the tests in the `HostStateTestCase` class in
 
67
``nova/tests/test_libvirt.py``::
 
68
 
 
69
    ./run_tests.sh test_libvirt:HostStateTestCase
 
70
 
 
71
To run the `ToPrimitiveTestCase.test_dict` test method in
 
72
``nova/tests/test_utils.py``::
 
73
 
 
74
    ./run_tests.sh test_utils:ToPrimitiveTestCase.test_dict
 
75
 
 
76
 
 
77
Suppressing logging output when tests fail
 
78
------------------------------------------
 
79
 
 
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.
 
85
 
 
86
You can suppress the logging output by calling ``run_tests.sh`` with the nose
 
87
flag::
 
88
 
 
89
    --nologcapture
 
90
 
 
91
Virtualenv
 
92
----------
 
93
 
 
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.
 
97
 
 
98
If you wish to recreate the virtualenv, call ``run_tests.sh`` with the flag::
 
99
 
 
100
    -f, --force
 
101
 
 
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
 
105
virtualenv.
 
106
 
 
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::
 
112
 
 
113
    -s, --no-site packages
 
114
 
 
115
If you do not wish to use a virtualenv at all, use the flag::
 
116
 
 
117
    -N, --no-virtual-env
 
118
 
 
119
Database
 
120
--------
 
121
 
 
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::
 
127
 
 
128
    -n, --no-recreate-db
 
129
 
 
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.
 
133
 
 
134
 
 
135
.. rubric:: Footnotes
 
136
 
 
137
.. [#f1] See :doc:`jenkins`.
 
138
 
 
139
.. [#f2] See :doc:`development.environment` for more details about the use of
 
140
   virtualenv.
 
141
 
 
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