开发者

How to use test fixtures with other framework then unit tests

开发者 https://www.devze.com 2023-02-05 01:07 出处:网络
I\'m using lettuce framework for testing and i would like to run tests with fresh database with some test fixture开发者_运维知识库s loaded. similarly to unit tests run, when test fixtures are defined

I'm using lettuce framework for testing and i would like to run tests with fresh database with some test fixture开发者_运维知识库s loaded. similarly to unit tests run, when test fixtures are defined

is it possible?


Here is a code snippet that loads the fixtures that's mostly taken from the Django test case. You just need to make sure that "db" points to the correct db (the test db). I do this by just passing in a custom settings file. "db" here points to just an alias, not an actual connection. If you are only using one database (not counting the test db) you just set this to 'default'. So if you test has a class attribute of 'fixtures' it will load the fixtures with the same rules as the loaddata management command.

    if getattr(self, 'multi_db', False):
        databases = connections
    else:
        databases = [DEFAULT_DB_ALIAS]

    for db in databases:

        if hasattr(self, 'fixtures'):
        # We have to use this slightly awkward syntax due to the fact
        # that we're using *args and **kwargs together.
        call_command('loaddata', *self.fixtures,
                         **{'verbosity': 0, 'database': db})

You will need to

import from django.core.management import call_command

to make this work.

0

精彩评论

暂无评论...
验证码 换一张
取 消