用 Lettuce 测试 Django 模型?

发布于 2024-11-27 00:16:01 字数 82 浏览 3 评论 0 原文

Lettuce 似乎是 Django 应用程序的一个非常好的 BDD 测试框架;但是,我还没有找到任何如何使用它测试模型的示例或文档。有什么可用的吗?

Lettuce seems like a pretty good BDD testing framework for Django apps; however, I haven't found any examples or documentation how to test models with it. Is there anything available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-12-04 00:16:01

好吧,我一直在寻找相同的内容,但找不到任何适当的文档或教程。
只是为了测试模型的验证并检查关系的健全性,我尝试从场景中放入和获取值并验证它们。我猜这就是从模型中验证所需的大部分验证。

well I was looking for the same, but couldn't find any proper documentation or a tutorial for this.
Just to test the validations on models and checking the sanity of relationships i tried putting and fetching the values from the scenarios and and validate them. Thats much of the validations one can require to validate from a model i guess.

薄暮涼年 2024-12-04 00:16:01

这篇文章似乎是不久前发布的,尽管它对我来说是最重要的结果,所以这是我的发现。

Lettuce 有 Django 的 @before.runserver@after.runserver 装饰器,可用于在您的terrain.py 文件中实现测试数据库。

我在本示例中使用 SQLite 数据库,并且还使用 South [:(],因此我有一个额外的测试来确保 SOUTH_TESTS_MIGRATE 已设置为 False。我有local_settings_test.py 文件,它用特定于我的测试用例的设置覆盖设置,并使用harves命令调用,如下所示:

python manage.py Harvest --settings=local_settings_test

这是我的当然,如果您希望在每个功能、场景或步骤上重置数据库,您可以使用其他装饰器来实现这些调用,例如,请参阅 http://lettuce.it/reference/terrain.html 了解有关可用内容的更多信息。

from lettuce import *
from django.conf import settings
from django.core.management.base import CommandError
from django.core.management import call_command


def assert_test_database():

    """
    Raises a CommandError in the event that the database name does not contain
    any reference to testing.

    Also checks South settings to ensure migrations are not implemented.
    """

    if not '-test' in settings.DATABASES['default']['NAME']:
        raise CommandError('You must run harvest with a test database')

    if getattr(settings, 'SOUTH_TESTS_MIGRATE', True):
        raise CommandError('SOUTH_TESTS_MIGRATE should be set to False')


@before.runserver
def create_database(server):

    """
    Asserts the database name is correct and creates initial structure, loading
    in any test_data fixtures which may have been created.
    """

    assert_test_database()
    call_command('syncdb', interactive=False, verbosity=0)
    call_command('loaddata', 'test_data', interactive=False, verbosity=0)


@after.runserver
def flush_database(server):

    """
    Asserts the database name is correct and flushes the database.
    """

    assert_test_database()
    call_command('flush', interactive=False, verbosity=0)

将这些步骤添加到地形文件中后,您可以像在中一样在步骤中调用模型你的单元测试。

Seems this post was made some time ago, though it was top of the results for me so here's my findings.

Lettuce has @before.runserver and @after.runserver decorators for Django, which can be used to implement a test database in your terrain.py file.

I'm using an SQLite database for this example and I also use South [:(] so I have an additional test to ensure SOUTH_TESTS_MIGRATE has been set to False. I have a local_settings_test.py file, which overrides settings with the ones specific to my test case and call with the harvest command like so:

python manage.py harvest --settings=local_settings_test

Here's my set-up and destruction calls. Of course you can implement these with other decorators if you'd prefer to reset the database upon every feature, scenario or step, for example. Refer to http://lettuce.it/reference/terrain.html for more information on what's available to you.

from lettuce import *
from django.conf import settings
from django.core.management.base import CommandError
from django.core.management import call_command


def assert_test_database():

    """
    Raises a CommandError in the event that the database name does not contain
    any reference to testing.

    Also checks South settings to ensure migrations are not implemented.
    """

    if not '-test' in settings.DATABASES['default']['NAME']:
        raise CommandError('You must run harvest with a test database')

    if getattr(settings, 'SOUTH_TESTS_MIGRATE', True):
        raise CommandError('SOUTH_TESTS_MIGRATE should be set to False')


@before.runserver
def create_database(server):

    """
    Asserts the database name is correct and creates initial structure, loading
    in any test_data fixtures which may have been created.
    """

    assert_test_database()
    call_command('syncdb', interactive=False, verbosity=0)
    call_command('loaddata', 'test_data', interactive=False, verbosity=0)


@after.runserver
def flush_database(server):

    """
    Asserts the database name is correct and flushes the database.
    """

    assert_test_database()
    call_command('flush', interactive=False, verbosity=0)

Once you've added these steps in your terrain file, you can call the models in your steps as you would in your unit tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文