4 ===== |
4 ===== |
5 |
5 |
6 Unit tests |
6 Unit tests |
7 ---------- |
7 ---------- |
8 |
8 |
9 The *CubicWeb* framework provides the `CubicWebTC` test base class in |
9 The *CubicWeb* framework provides the |
10 the module `cubicweb.devtools.testlib`. |
10 :class:`cubicweb.devtools.testlib.CubicWebTC` test base class . |
11 |
11 |
12 Tests shall be put into the mycube/test directory. Additional test |
12 Tests shall be put into the mycube/test directory. Additional test |
13 data shall go into mycube/test/data. |
13 data shall go into mycube/test/data. |
14 |
14 |
15 It is much advised to write tests concerning entities methods, hooks |
15 It is much advised to write tests concerning entities methods, |
16 and operations, security. The CubicWebTC base class has convenience |
16 actions, hooks and operations, security. The |
17 methods to help test all of this. |
17 :class:`~cubicweb.devtools.testlib.CubicWebTC` base class has |
18 |
18 convenience methods to help test all of this. |
19 .. note:: |
19 |
20 |
20 In the realm of views, automatic tests check that views are valid |
21 In the realm of views, there is not much to do but check that the |
21 XHTML. See :ref:`automatic_views_tests` for details. Since 3.9, bases |
22 views are valid XHTML. See :ref:`automatic_views_tests` for |
22 for web functional testing using `windmill |
23 details. Integration of CubicWeb tests with UI testing tools such as |
23 <http://www.getwindmill.com/>`_ are set. See test cases in |
24 `selenium`_ are currently under invesitgation. |
24 cubicweb/web/test/windmill and python wrapper in |
25 |
25 cubicweb/web/test_windmill/ if you want to use this in your own cube. |
26 .. _selenium: http://seleniumhq.org/projects/ide/ |
26 |
27 |
27 |
28 Most unit tests need a live database to work against. This is achieved |
28 Most unit tests need a live database to work against. This is achieved |
29 by CubicWeb using automatically sqlite (bundled with Python, see |
29 by CubicWeb using automatically sqlite (bundled with Python, see |
30 http://docs.python.org/library/sqlite3.html) as a backend. |
30 http://docs.python.org/library/sqlite3.html) as a backend. |
31 |
31 |
75 'SK subkeyword_of K WHERE C name "classif1", K eid %s' |
75 'SK subkeyword_of K WHERE C name "classif1", K eid %s' |
76 % self.kw1.eid).get_entity(0,0) |
76 % self.kw1.eid).get_entity(0,0) |
77 self.kw1.set_relations(subkeyword_of=kw3) |
77 self.kw1.set_relations(subkeyword_of=kw3) |
78 self.assertRaises(ValidationError, self.commit) |
78 self.assertRaises(ValidationError, self.commit) |
79 |
79 |
80 The test class defines a `setup_database` method which populates the |
80 The test class defines a :meth:`setup_database` method which populates the |
81 database with initial data. Each test of the class runs with this |
81 database with initial data. Each test of the class runs with this |
82 pre-populated database. |
82 pre-populated database. A commit is done automatically after the |
|
83 :meth:`setup_database` call. You don't have to call it explicitely. |
83 |
84 |
84 The test case itself checks that an Operation does it job of |
85 The test case itself checks that an Operation does it job of |
85 preventing cycles amongst Keyword entities. |
86 preventing cycles amongst Keyword entities. |
|
87 |
|
88 .. note:: |
|
89 |
|
90 :meth:`commit` method is not called automatically in test_XXX |
|
91 methods. You have to call it explicitely if needed (notably to test |
|
92 operations). It is a good practice to call :meth:`clear_all_caches` |
|
93 on entities after a commit to avoid request cache effects. |
86 |
94 |
87 You can see an example of security tests in the |
95 You can see an example of security tests in the |
88 :ref:`adv_tuto_security`. |
96 :ref:`adv_tuto_security`. |
89 |
97 |
90 It is possible to have these tests run continuously using `apycot`_. |
98 It is possible to have these tests run continuously using `apycot`_. |
147 Email notifications tests |
155 Email notifications tests |
148 ````````````````````````` |
156 ````````````````````````` |
149 |
157 |
150 When running tests potentially generated e-mails are not really sent |
158 When running tests potentially generated e-mails are not really sent |
151 but is found in the list `MAILBOX` of module |
159 but is found in the list `MAILBOX` of module |
152 `cubicweb.devtools.testlib`. |
160 :mod:`cubicweb.devtools.testlib`. |
153 |
161 |
154 You can test your notifications by analyzing the contents of this list, which |
162 You can test your notifications by analyzing the contents of this list, which |
155 contains objects with two attributes: |
163 contains objects with two attributes: |
156 |
164 |
157 * `recipients`, the list of recipients |
165 * `recipients`, the list of recipients |