Helpers for unittest

AnyBlok provides base test classes to help creating fixtures. Blok developers will be mostly interested in BlokTestCase.

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 additional_setting()
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.

classmethod getRegistry()

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

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


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``
classmethod additional_setting()
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