How to use test fixtures with other framework then unit tests

I'm using lettuce framework for testing and i would like to run tests with fresh database with some test fixtures 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.

链接地址: http://www.djcxy.com/p/52300.html

上一篇: 在Django上测试应用程序时未找到任何固件

下一篇: 如何使用测试夹具与其他框架,然后单元测试