doc/book/en/development/testing.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 13 Apr 2010 19:19:37 +0200
branchstable
changeset 5229 67dbd07a05f3
parent 5186 f3c2cb460ad9
child 5255 15ea2f3464a4
permissions -rw-r--r--
[doc/book] expand tesing material
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
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
.. toctree::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
   :maxdepth: 1
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
Unit tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    12
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
    13
the module `cubicweb.devtools.testlib`.
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
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
    16
data shall go into mycube/test/data.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    17
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    18
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
    19
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
    20
methods to help test all of this.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    21
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    22
.. note::
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    23
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    24
  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
    25
  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
    26
  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
    27
  `selenium`_ are currently under invesitgation.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    28
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    29
.. _selenium: http://seleniumhq.org/projects/ide/
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    31
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
    32
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
    33
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
    34
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    35
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
    36
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
    37
be built automatically when the test suit starts.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    38
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    39
.. warning::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    41
  Whenever the schema changes (new entities, attributes, relations)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    42
  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
    43
  or relation type properties (constraints, cardinalities,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    44
  permissions) and generally dealt with using the
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    45
  `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
    46
  need not a database regeneration step.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    47
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    48
Unit test by example
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    49
````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    51
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
    52
from http://www.cubicweb.org/project/cubicweb-keyword).
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    53
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    54
.. sourcecode:: python
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
    from cubicweb.devtools.testlib import CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    57
    from cubicweb import ValidationError
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    58
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    59
    class ClassificationHooksTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    60
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    61
        def setup_database(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    62
            req = self.request()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    63
            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
    64
            c1 = req.create_entity('Classification', name=u'classif1',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    65
                                   classifies=group_etype)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    66
            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
    67
            c2 = req.create_entity('Classification', name=u'classif2',
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    68
                                   classifies=user_etype)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    69
            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
    70
            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
    71
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    72
        def test_cannot_create_cycles(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    73
            # direct obvious cycle
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    74
            self.assertRaises(ValidationError, self.kw1.set_relations,
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    75
                              subkeyword_of=self.kw1)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    76
            # testing indirect cycles
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    77
            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
    78
                               '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
    79
                               % self.kw1.eid).get_entity(0,0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    80
            self.kw1.set_relations(subkeyword_of=kw3)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    81
            self.assertRaises(ValidationError, self.commit)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    83
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
    84
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
    85
pre-populated database.
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    86
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    87
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
    88
preventing cycles amongst Keyword entities.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    89
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    90
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    91
XXX ref to advanced use case
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
    92
XXX apycot plug
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
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   107
When one is logged in as a normal user and wants to switch back to the
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
   108
admin user, one has to use self.restore_connection().
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   109
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   110
Usually it looks like this:
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   111
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   112
.. sourcecode:: python
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   113
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   114
    # execute using default admin connection
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   115
    self.execute(...)
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   116
    # I want to login with another user, ensure to free admin connection pool
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   117
    # (could have used rollback but not close here, we should never close defaut admin connection)
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   118
    self.commit()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   119
    cnx = self.login('user')
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   120
    # execute using user connection
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   121
    self.execute(...)
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   122
    # 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
   123
    self.commit();  cnx.close()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   124
    # 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
   125
    # 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
   126
    self.restore_connection()
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   127
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   128
.. warning::
2953
e5bdf98be37f [doc] important note about tests and connections/users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2175
diff changeset
   129
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   130
   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
   131
   connection from another !
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   132
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   133
XXX the new context manager ?
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   134
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   135
Email notifications tests
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
-------------------------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   137
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   138
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
   139
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
   140
`cubicweb.devtools.testlib`.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   141
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   142
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
   143
contains objects with two attributes:
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   144
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
* `recipients`, the list of recipients
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   146
* `msg`, object email.Message
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   148
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
   149
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   150
.. sourcecode:: python
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   151
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   152
    from cubicweb.devtools.testlib import CubicWebTC, MAILBOX
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   153
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   154
    class BlogTestsCubicWebTC(CubicWebTC):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   155
        """test blog specific behaviours"""
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   156
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   157
        def test_notifications(self):
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   158
            req = self.request()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   159
            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
   160
                                description=u'cubicweb is beautiful')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   161
            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
   162
                                             content=u'cubicweb hop')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   163
            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
   164
            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
   165
                                             content=u'cubicweb yes')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   166
            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
   167
            self.assertEquals(len(MAILBOX), 0)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   168
            self.commit()
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   169
            self.assertEquals(len(MAILBOX), 2)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   170
            mail = MAILBOX[0]
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   171
            self.assertEquals(mail.subject, '[data] hop')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   172
            mail = MAILBOX[1]
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   173
            self.assertEquals(mail.subject, '[data] yes')
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   174
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   175
.. _automatic_views_tests:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   176
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   177
Automatic views testing
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   178
-----------------------
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   179
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   180
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
   181
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
   182
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
   183
further modification.
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   184
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   185
XXX more to come
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   186
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
Using Pytest
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
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   191
.. automodule:: logilab.common.testlib
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   192
.. autoclass:: logilab.common.testlib.TestCase
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   193
   :members:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   194
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   195
XXX pytestconf.py & options (e.g --source to use a different db
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   196
backend than sqlite)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   197
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   198
CubicWebTC API
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   199
``````````````
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   200
.. autoclass:: cubicweb.devtools.testlib.CubicWebTC
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   201
   :members:
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5186
diff changeset
   202