doc/book/en/development/testing.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 14 Apr 2010 16:32:20 +0200
branchstable
changeset 5255 15ea2f3464a4
parent 5229 67dbd07a05f3
child 5266 84f285d96363
permissions -rw-r--r--
[doc/book] complete the unit tests chapter
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 -*-
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
Tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
=====
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
.. toctree::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
   :maxdepth: 1
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
Unit tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    11
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
    12
the module `cubicweb.devtools.testlib`.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    13
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    14
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
    15
data shall go into mycube/test/data.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    16
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    17
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
    18
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
    19
methods to help test all of this.
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
.. note::
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    22
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    23
  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
    24
  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
    25
  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
    26
  `selenium`_ are currently under invesitgation.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    27
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    28
.. _selenium: http://seleniumhq.org/projects/ide/
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    30
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
    31
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
    32
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
    33
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    34
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
    35
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
    36
be built automatically when the test suit starts.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    37
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    38
.. warning::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    40
  Whenever the schema changes (new entities, attributes, relations)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    41
  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
    42
  or relation type properties (constraints, cardinalities,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    43
  permissions) and generally dealt with using the
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    44
  `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
    45
  need not a database regeneration step.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    46
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    47
Unit test by example
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    48
````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    50
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
    51
from http://www.cubicweb.org/project/cubicweb-keyword).
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
.. sourcecode:: python
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    54
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    55
    from cubicweb.devtools.testlib import CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    56
    from cubicweb import ValidationError
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
    class ClassificationHooksTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    59
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    60
        def setup_database(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    61
            req = self.request()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    62
            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
    63
            c1 = req.create_entity('Classification', name=u'classif1',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    64
                                   classifies=group_etype)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    65
            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
    66
            c2 = req.create_entity('Classification', name=u'classif2',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    67
                                   classifies=user_etype)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    68
            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
    69
            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
    70
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    71
        def test_cannot_create_cycles(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    72
            # direct obvious cycle
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    73
            self.assertRaises(ValidationError, self.kw1.set_relations,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    74
                              subkeyword_of=self.kw1)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    75
            # testing indirect cycles
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    76
            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
    77
                               '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
    78
                               % self.kw1.eid).get_entity(0,0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    79
            self.kw1.set_relations(subkeyword_of=kw3)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    80
            self.assertRaises(ValidationError, self.commit)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    82
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
    83
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
    84
pre-populated database.
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    85
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    86
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
    87
preventing cycles amongst Keyword entities.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    88
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
    89
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
    90
:ref:`adv_tuto_security`.
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    91
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
    92
It is possible to have these tests run continuously using `apycot`
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
    93
(http://www.logilab.org/project/apycot).
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    94
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    95
Managing connections or users
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
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
    98
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
    99
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
   100
simulating security, changing users.
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   101
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   102
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
   103
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
   104
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   105
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
   106
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
   107
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   108
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
   109
context manager:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   110
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   111
.. sourcecode:: python
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   112
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   113
   with self.login('user1') as user:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   114
       req = user.req
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   115
       req.execute(...)
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   116
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   117
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
   118
which releases the connection.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   119
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   120
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
   121
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
   122
self.restore_connection().
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   123
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   124
Usage with restore_connection:
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   125
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   126
.. sourcecode:: python
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   127
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   128
    # execute using default admin connection
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   129
    self.execute(...)
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   130
    # 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
   131
    # (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
   132
    # 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
   133
    self.commit()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   134
    cnx = self.login('user')
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   135
    # execute using user connection
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   136
    self.execute(...)
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   137
    # 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
   138
    self.commit();  cnx.close()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   139
    # 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
   140
    # 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
   141
    self.restore_connection()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   142
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   143
.. warning::
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   144
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   145
   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
   146
   connection from another !
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   147
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
Email notifications tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   149
-------------------------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   151
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
   152
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
   153
`cubicweb.devtools.testlib`.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   154
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   155
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
   156
contains objects with two attributes:
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   157
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
* `recipients`, the list of recipients
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   159
* `msg`, object email.Message
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   160
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   161
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
   162
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   163
.. sourcecode:: python
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   164
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   165
    from cubicweb.devtools.testlib import CubicWebTC, MAILBOX
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   166
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   167
    class BlogTestsCubicWebTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   168
        """test blog specific behaviours"""
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   169
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   170
        def test_notifications(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   171
            req = self.request()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   172
            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
   173
                                description=u'cubicweb is beautiful')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   174
            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
   175
                                             content=u'cubicweb hop')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   176
            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
   177
            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
   178
                                             content=u'cubicweb yes')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   179
            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
   180
            self.assertEquals(len(MAILBOX), 0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   181
            self.commit()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   182
            self.assertEquals(len(MAILBOX), 2)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   183
            mail = MAILBOX[0]
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   184
            self.assertEquals(mail.subject, '[data] hop')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   185
            mail = MAILBOX[1]
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   186
            self.assertEquals(mail.subject, '[data] yes')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   187
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   188
.. _automatic_views_tests:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   189
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   190
Automatic views testing
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   191
-----------------------
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   192
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   193
This is done automatically with the AutomaticWebTest class. At cube
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   194
creation time, the mycube/test/test_mycube.py file contains such a
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   195
test. The code here has to be uncommented to be usable, without
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   196
further modification.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   197
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   198
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
   199
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
   200
invoked and tested.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   201
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   202
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
   203
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
   204
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   205
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
   206
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
   207
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
   208
mechanism. These are:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   209
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   210
* `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
   211
* `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
   212
* `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
   213
  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
   214
  against which views may be selected.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   215
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   216
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   217
Test APIS
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   218
---------
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   219
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   220
Using Pytest
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   221
````````````
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   222
5255
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   223
`pytest` is an utility that extends the Python unittest functionality
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   224
and is the preferred way to run the CubicWeb test suites. Bare
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   225
unittests also work the usual way.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   226
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   227
To use it, you may:
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   228
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   229
* 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
   230
  discover them automatically)
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   231
* 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
   232
* 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
   233
  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
   234
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   235
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
   236
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
   237
exception occurs in a test.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   238
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   239
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
   240
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
   241
`-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
   242
failed.
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   243
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   244
Using the `TestCase` base class
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   245
```````````````````````````````
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   246
15ea2f3464a4 [doc/book] complete the unit tests chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   247
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
   248
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
   249
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   250
.. autoclass:: logilab.common.testlib.TestCase
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   251
   :members:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   252
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   253
CubicWebTC API
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   254
``````````````
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   255
.. autoclass:: cubicweb.devtools.testlib.CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   256
   :members:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   257