Helper for unittest

For unittest, classes are available to offer some fonctionnalities

Base classes for unit/integration tests with anyblok.

This module provides BlokTestCase, which is the main one meant for blok tests, and DBTestCase, whose primary purpose is to test anyblok itself, in so-called “framework tests”.


from anyblok.tests.testcase import TestCase
class anyblok.tests.testcase.TestCase(methodName='runTest')


Common helpers, not meant to be used directly.

classmethod createdb(keep_existing=False)

Create the database specified in configuration.

Parameters:keep_existing – If false drop the previous db before create it
classmethod dropdb()

Drop the database specified in configuration.


Return the registry for the test database.

This assumes the database is created, and the registry has already been initialized:

registry = self.getRegistry()
Return type:registry instance
classmethod init_configuration_manager(**env)

Initialise the configuration manager with environ variable to launch the test


For the moment we not use the environ variable juste constante

  • prefix – prefix the database name
  • env – add another dict to merge with environ variable

Roll back the session



this testcase destroys the test database for each unittest

class anyblok.tests.testcase.DBTestCase(methodName='runTest')

Bases: anyblok.tests.testcase.TestCase

Base class for tests that need to work on an empty database.


The database is created and dropped with each unit test

For instance, this is the one used for Field, Column, RelationShip, and more generally core framework tests.

The drawback of using this base class is that tests will be slow. The advantage is ultimate test isolation.

Sample usage:

from anyblok.tests.testcase import DBTestCase

def simple_column(ColumnType=None, **kwargs):

    class Test:

        id = Declarations.Column.Integer(primary_key=True)
        col = ColumnType(**kwargs)

class TestColumns(DBTestCase):

    def test_integer(self):
        Integer = Declarations.Column.Integer

        registry = self.init_registry(simple_column,
        test = registry.Test.insert(col=1)
        self.assertEqual(test.col, 1)
blok_entry_points = ('bloks',)

setuptools entry points to load blok

current_blok = 'anyblok-core'

In the blok to add the new model

init_registry(function, **kwargs)

call a function to filled the blok manager with new model

  • function – function to call
  • kwargs – kwargs for the function
Return type:

registry instance


Create a database and load the blok manager

classmethod setUpClass()

Intialialise the configuration manager


Clear the registry, unload the blok manager and drop the database

upgrade(registry, **kwargs)

Upgrade the registry:

class MyTest(DBTestCase):

    def test_mytest(self):
        registry = self.init_registry(...)
        self.upgrade(registry, install=('MyBlok',))
  • registry – registry to upgrade
  • install – list the blok to install
  • update – list the blok to update
  • uninstall – list the blok to uninstall


class anyblok.tests.testcase.BlokTestCase(methodName='runTest')


Base class for tests meant to run on a preinstalled database.

The tests written with this class don’t need to start afresh on a new database, and therefore run much faster than those inheriting DBTestCase. Instead, they expect the tested bloks to be already installed and up to date.

The session gets rollbacked after each test.

Such tests are appropriate for a typical blok developper workflow:

  • create and install the bloks once
  • run the tests of the blok under development repeatedly
  • upgrade the bloks in database when needed (schema change, update of dependencies)

They are also appropriate for on the fly testing while installing the bloks: the tests of each blok get run on the database state they expect, before dependent (downstream) bloks, that could. e.g., alter the database schema, get themselves installed. This is useful to test a whole stack at once using only one database (typically in CI bots).

Sample usage:

from anyblok.tests.testcase import BlokTestCase

class MyBlokTest(BlokTestCase):

    def test_1(self):
        # access to the registry by ``self.registry``
registry = None

The instance of anyblok.registry.Registry` to use in tests.

The session_commit() method is disabled to avoid side effects from one test to the other.

classmethod setUpClass()

Initialize the registry.


Roll back the session

classmethod tearDownClass()