doc/book/en/devrepo/testing.rst
author Stephanie Marcu <stephanie.marcu@logilab.fr>
Wed, 15 Sep 2010 11:00:55 +0200
branchstable
changeset 6243 c0ee416040fa
parent 6242 d9ebb391782c
child 6301 d9d6bdd814ba
permissions -rw-r--r--
[book - #1251780] add a section to explain how to write tests to test which actions are visible
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
5266
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
     2
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
Tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
=====
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
Unit tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
     9
The *CubicWeb* framework provides the `CubicWebTC` test base class in
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    10
the module `cubicweb.devtools.testlib`.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    11
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    12
Tests shall be put into the mycube/test directory. Additional test
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    13
data shall go into mycube/test/data.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    14
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    15
It is much advised to write tests concerning entities methods, hooks
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    16
and operations, security. The CubicWebTC base class has convenience
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    17
methods to help test all of this.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    18
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    19
.. note::
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    20
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    21
  In the realm of views, there is not much to do but check that the
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    22
  views are valid XHTML.  See :ref:`automatic_views_tests` for
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    23
  details. Integration of CubicWeb tests with UI testing tools such as
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    24
  `selenium`_ are currently under invesitgation.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    25
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    26
.. _selenium: http://seleniumhq.org/projects/ide/
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    28
Most unit tests need a live database to work against. This is achieved
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    29
by CubicWeb using automatically sqlite (bundled with Python, see
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    30
http://docs.python.org/library/sqlite3.html) as a backend.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    31
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    32
The database is stored in the mycube/test/tmpdb,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    33
mycube/test/tmpdb-template files. If it does not (yet) exists, it will
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    34
be built automatically when the test suit starts.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    35
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    36
.. warning::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    38
  Whenever the schema changes (new entities, attributes, relations)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    39
  one must delete these two files. Changes concerned only with entity
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    40
  or relation type properties (constraints, cardinalities,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    41
  permissions) and generally dealt with using the
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    42
  `sync_schema_props_perms()` fonction of the migration environment
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    43
  need not a database regeneration step.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    44
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    45
Unit test by example
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    46
````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    48
We start with an example extracted from the keyword cube (available
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    49
from http://www.cubicweb.org/project/cubicweb-keyword).
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    50
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    51
.. sourcecode:: python
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    52
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    53
    from cubicweb.devtools.testlib import CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    54
    from cubicweb import ValidationError
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    55
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    56
    class ClassificationHooksTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    57
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    58
        def setup_database(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    59
            req = self.request()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    60
            group_etype = req.execute('Any X WHERE X name "CWGroup"').get_entity(0,0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    61
            c1 = req.create_entity('Classification', name=u'classif1',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    62
                                   classifies=group_etype)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    63
            user_etype = req.execute('Any X WHERE X name "CWUser"').get_entity(0,0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    64
            c2 = req.create_entity('Classification', name=u'classif2',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    65
                                   classifies=user_etype)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    66
            self.kw1 = req.create_entity('Keyword', name=u'kwgroup', included_in=c1)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    67
            self.kw2 = req.create_entity('Keyword', name=u'kwuser', included_in=c2)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    69
        def test_cannot_create_cycles(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    70
            # direct obvious cycle
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    71
            self.assertRaises(ValidationError, self.kw1.set_relations,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    72
                              subkeyword_of=self.kw1)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    73
            # testing indirect cycles
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    74
            kw3 = self.execute('INSERT Keyword SK: SK name "kwgroup2", SK included_in C, '
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    75
                               'SK subkeyword_of K WHERE C name "classif1", K eid %s'
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    76
                               % self.kw1.eid).get_entity(0,0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    77
            self.kw1.set_relations(subkeyword_of=kw3)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    78
            self.assertRaises(ValidationError, self.commit)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    80
The test class defines a `setup_database` method which populates the
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    81
database with initial data. Each test of the class runs with this
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    82
pre-populated database.
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    83
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    84
The test case itself checks that an Operation does it job of
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    85
preventing cycles amongst Keyword entities.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    86
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
    87
You can see an example of security tests in the
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
    88
:ref:`adv_tuto_security`.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    89
5266
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
    90
It is possible to have these tests run continuously using `apycot`_.
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
    91
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
    92
.. _apycot: http://www.logilab.org/project/apycot
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    93
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    94
Managing connections or users
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    95
+++++++++++++++++++++++++++++
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    96
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    97
Since unit tests are done with the SQLITE backend and this does not
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    98
support multiple connections at a time, you must be careful when
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    99
simulating security, changing users.
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   100
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   101
By default, tests run with a user with admin privileges. This
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   102
user/connection must never be closed.
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   103
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   104
Before a self.login, one has to release the connection pool in use
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   105
with a self.commit, self.rollback or self.close.
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   106
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   107
The `login` method returns a connection object that can be used as a
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   108
context manager:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   109
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   110
.. sourcecode:: python
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   111
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   112
   with self.login('user1') as user:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   113
       req = user.req
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   114
       req.execute(...)
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   115
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   116
On exit of the context manager, either a commit or rollback is issued,
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   117
which releases the connection.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   118
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   119
When one is logged in as a normal user and wants to switch back to the
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   120
admin user without committing, one has to use
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   121
self.restore_connection().
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   122
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   123
Usage with restore_connection:
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   124
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   125
.. sourcecode:: python
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   126
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   127
    # execute using default admin connection
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   128
    self.execute(...)
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   129
    # I want to login with another user, ensure to free admin connection pool
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   130
    # (could have used rollback but not close here
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   131
    # we should never close defaut admin connection)
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   132
    self.commit()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   133
    cnx = self.login('user')
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   134
    # execute using user connection
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   135
    self.execute(...)
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   136
    # I want to login with another user or with admin user
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   137
    self.commit();  cnx.close()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   138
    # restore admin connection, never use cnx = self.login('admin'), it will return
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   139
    # the default admin connection and one may be tempted to close it
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   140
    self.restore_connection()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   141
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   142
.. warning::
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   143
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   144
   Do not use the references kept to the entities created with a
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   145
   connection from another !
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   146
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
Email notifications tests
6065
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   148
`````````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   149
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   150
When running tests potentially generated e-mails are not really sent
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   151
but is found in the list `MAILBOX` of module
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   152
`cubicweb.devtools.testlib`.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   153
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   154
You can test your notifications by analyzing the contents of this list, which
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   155
contains objects with two attributes:
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   156
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   157
* `recipients`, the list of recipients
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
* `msg`, object email.Message
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   159
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   160
Let us look at simple example from the ``blog`` cube.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   161
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   162
.. sourcecode:: python
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   163
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   164
    from cubicweb.devtools.testlib import CubicWebTC, MAILBOX
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   165
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   166
    class BlogTestsCubicWebTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   167
        """test blog specific behaviours"""
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   168
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   169
        def test_notifications(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   170
            req = self.request()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   171
            cubicweb_blog = req.create_entity('Blog', title=u'cubicweb',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   172
                                description=u'cubicweb is beautiful')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   173
            blog_entry_1 = req.create_entity('BlogEntry', title=u'hop',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   174
                                             content=u'cubicweb hop')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   175
            blog_entry_1.set_relations(entry_of=cubicweb_blog)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   176
            blog_entry_2 = req.create_entity('BlogEntry', title=u'yes',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   177
                                             content=u'cubicweb yes')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   178
            blog_entry_2.set_relations(entry_of=cubicweb_blog)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   179
            self.assertEquals(len(MAILBOX), 0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   180
            self.commit()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   181
            self.assertEquals(len(MAILBOX), 2)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   182
            mail = MAILBOX[0]
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   183
            self.assertEquals(mail.subject, '[data] hop')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   184
            mail = MAILBOX[1]
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   185
            self.assertEquals(mail.subject, '[data] yes')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   186
6243
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   187
Visible actions tests
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   188
`````````````````````
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   189
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   190
It is easy to write unit tests to test actions which are visible to
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   191
user or to a category of users. Let's take an example in the
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   192
`conference cube`_.
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   193
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   194
.. _`conference cube`: http://www.cubicweb.org/project/cubicweb-conference
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   195
.. sourcecode:: python
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   196
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   197
    class ConferenceActionsTC(CubicWebTC):
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   198
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   199
        def setup_database(self):
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   200
            self.conf = self.create_entity('Conference',
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   201
                                           title=u'my conf',
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   202
                                           url_id=u'conf',
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   203
                                           start_on=date(2010, 1, 27),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   204
                                           end_on = date(2010, 1, 29),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   205
                                           call_open=True,
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   206
                                           reverse_is_chair_at=chair,
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   207
                                           reverse_is_reviewer_at=reviewer)
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   208
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   209
        def test_admin(self):
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   210
            req = self.request()
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   211
            rset = req.execute('Any C WHERE C is Conference')
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   212
            self.assertListEquals(self.pactions(req, rset),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   213
                                  [('workflow', workflow.WorkflowActions),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   214
                                   ('edit', confactions.ModifyAction),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   215
                                   ('managepermission', actions.ManagePermissionsAction),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   216
                                   ('addrelated', actions.AddRelatedActions),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   217
                                   ('delete', actions.DeleteAction),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   218
                                   ('generate_badge_action', badges.GenerateBadgeAction),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   219
                                   ('addtalkinconf', confactions.AddTalkInConferenceAction)
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   220
                                   ])
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   221
            self.assertListEquals(self.action_submenu(req, rset, 'addrelated'),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   222
                                  [(u'add Track in_conf Conference object',
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   223
                                    u'http://testing.fr/cubicweb/add/Track'
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   224
                                    u'?__linkto=in_conf%%3A%(conf)s%%3Asubject&'
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   225
                                    u'__redirectpath=conference%%2Fconf&'
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   226
                                    u'__redirectvid=' % {'conf': self.conf.eid}),
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   227
                                   ])
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   228
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   229
You just have to execute a rql query corresponding to the view you want to test,
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   230
and to compare the result of
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   231
:meth:`~cubicweb.devtools.testlib.CubicWebTC.pactions` with the list of actions
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   232
that must be visible in the interface. This is a list of tuples. The first
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   233
element is the action's `__regid__`, the second the action's class.
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   234
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   235
To test actions in submenu, you just have to test the result of
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   236
:meth:`~cubicweb.devtools.testlib.CubicWebTC.action_submenu` method. The last
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   237
parameter of the method is the action's category. The result is a list of
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   238
tuples. The first element is the action's title, and the second element the
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   239
action's url.
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   240
c0ee416040fa [book - #1251780] add a section to explain how to write tests to test which actions are visible
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6242
diff changeset
   241
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   242
.. _automatic_views_tests:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   243
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   244
Automatic views testing
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   245
-----------------------
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   246
6242
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   247
This is done automatically with the :class:`cubicweb.devtools.testlib.AutomaticWebTest`
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   248
class. At cube creation time, the mycube/test/test_mycube.py file
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   249
contains such a test. The code here has to be uncommented to be
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   250
usable, without further modification.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   251
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   252
The ``auto_populate`` method uses a smart algorithm to create
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   253
pseudo-random data in the database, thus enabling the views to be
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   254
invoked and tested.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   256
Depending on the schema, hooks and operations constraints, it is not
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   257
always possible for the automatic auto_populate to proceed.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   258
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   259
It is possible of course to completely redefine auto_populate. A
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   260
lighter solution is to give hints (fill some class attributes) about
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   261
what entities and relations have to be skipped by the auto_populate
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   262
mechanism. These are:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   263
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   264
* `no_auto_populate`, may contain a list of entity types to skip
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   265
* `ignored_relations`, may contain a list of relation types to skip
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   266
* `application_rql`, may contain a list of rql expressions that
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   267
  auto_populate cannot guess by itself; these must yield resultsets
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   268
  against which views may be selected.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   269
6242
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   270
.. warning::
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   271
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   272
  Take care to not let the imported `AutomaticWebTest` in your test module
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   273
  namespace, else both your subclass *and* this parent class will be run.
d9ebb391782c [book - #1251260] add a warning in automatic test when user redefine the base class
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6167
diff changeset
   274
6167
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   275
Testing on a real-life database
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   276
-------------------------------
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   277
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   278
The ``CubicWebTC`` class uses the `cubicweb.devtools.ApptestConfiguration`
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   279
configuration class to setup its testing environment (database driver,
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   280
user password, application home, and so on). The `cubicweb.devtools`
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   281
module also provides a `RealDatabaseConfiguration`
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   282
class that will read a regular cubicweb sources file to fetch all
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   283
this information but will also prevent the database to be initalized
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   284
and reset between tests.
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   285
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   286
For a test class to use a specific configuration, you have to set
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   287
the `_config` class attribute on the class as in:
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   288
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   289
.. sourcecode:: python
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   290
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   291
    from cubicweb.devtools import RealDatabaseConfiguration
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   292
    from cubicweb.devtools.testlib import CubicWebTC
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   293
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   294
    class BlogRealDatabaseTC(CubicWebTC):
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   295
        _config = RealDatabaseConfiguration('blog',
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   296
                                            sourcefile='/path/to/realdb_sources')
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   297
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   298
        def test_blog_rss(self):
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   299
	    req = self.request()
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   300
	    rset = req.execute('Any B ORDERBY D DESC WHERE B is BlogEntry, '
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   301
	                       'B created_by U, U login "logilab", B creation_date D')
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   302
            self.view('rss', rset)
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   303
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   304
376e6c3d4002 [doc] add documentation on RealDatabaseConfiguration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6065
diff changeset
   305
6065
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   306
Testing with other cubes
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   307
------------------------
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   308
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   309
Sometimes a small component cannot be tested all by itself, so one
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   310
needs to specify other cubes to be used as part of the the unit test
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   311
suite. This is handled by the ``bootstrap_cubes`` file located under
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   312
``mycube\test\data``. One example from the `preview` cube::
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   313
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   314
 card, file, preview
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   315
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   316
The format is:
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   317
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   318
* possibly several empy lines or lines starting with ``#`` (comment lines)
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   319
* one line containing a coma separated list of cube names.
eae118a84cc6 [doc/book] note about bootstrap_cubes file
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   320
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   321
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   322
Test APIS
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   323
---------
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   324
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   325
Using Pytest
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   326
````````````
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   327
5266
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   328
The `pytest` utility (shipping with `logilab-common`_, which is a
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   329
mandatory dependency of CubicWeb) extends the Python unittest
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   330
functionality and is the preferred way to run the CubicWeb test
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   331
suites. Bare unittests also work the usual way.
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   332
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5255
diff changeset
   333
.. _logilab-common: http://www.logilab.org/project/logilab-common
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   334
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   335
To use it, you may:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   336
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   337
* just launch `pytest` in your cube to execute all tests (it will
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   338
  discover them automatically)
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   339
* launch `pytest unittest_foo.py` to execute one test file
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   340
* launch `pytest unittest_foo.py bar` to execute all test methods and
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   341
  all test cases whose name contain `bar`
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   342
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   343
Additionally, the `-x` option tells pytest to exit at the first error
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   344
or failure. The `-i` option tells pytest to drop into pdb whenever an
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   345
exception occurs in a test.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   346
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   347
When the `-x` option has been used and the run stopped on a test, it
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   348
is possible, after having fixed the test, to relaunch pytest with the
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   349
`-R` option to tell it to start testing again from where it previously
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   350
failed.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   351
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   352
Using the `TestCase` base class
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   353
```````````````````````````````
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   354
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   355
The base class of CubicWebTC is logilab.common.testlib.TestCase, which
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   356
provides a lot of convenient assertion methods.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   357
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   358
.. autoclass:: logilab.common.testlib.TestCase
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   359
   :members:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   360
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   361
CubicWebTC API
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   362
``````````````
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   363
.. autoclass:: cubicweb.devtools.testlib.CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   364
   :members: