首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用自定义测试套件运行器和pytest-django

使用自定义测试套件运行器和pytest-django
EN

Stack Overflow用户
提问于 2014-12-04 04:14:01
回答 2查看 1.2K关注 0票数 7

我想将我的Django (1.6x版)应用程序切换为使用pytest-django进行测试。因此,我通过pip安装了最新的pytest-django,并获得了以下版本:

代码语言:javascript
复制
pytest==2.6.4
pytest-django==2.7.0

对于常规的django测试,我使用了一个自定义测试套件运行器来扩展我在settings.py中配置的DjangoTestSuiteRunner:

settings.py:

代码语言:javascript
复制
TEST_RUNNER = "dcmanager.tests.runner.ManagedModelTestRunner"

runner.py:

代码语言:javascript
复制
import unittest

from django.conf import settings
from django.db.models import get_app, get_apps
from django.test.simple import DjangoTestSuiteRunner, build_test, build_suite, runner


class ManagedModelTestRunner(DjangoTestSuiteRunner):
    """
    Test runner that automatically makes all unmanaged models in
    project managed for the duration of the test run and patches VStorage model,
    so that one doesn't need to execute the SQL manually to create them.
    """
    IGNORE_TESTS = ['django', 'rest_framework', 'rest_framework_swagger']

    def build_suite(self, test_labels, extra_tests=None, **kwargs):
        suite = unittest.TestSuite()

        if test_labels:
            for label in test_labels:
                if '.' in label:
                    suite.addTest(build_test(label))
                else:
                    app = get_app(label)
                    suite.addTest(build_suite(app))
        else:
            ignore_list = []
            for app in get_apps():
                app_name_parts = app.__name__.split('.')
                for index, _ in enumerate(app_name_parts):
                    app_part_name = '.'.join(app_name_parts[0:index])
                    if app_part_name and app_part_name in self.IGNORE_TESTS:
                        ignore_list.append(app.__name__)
                        break
                if app.__name__ not in ignore_list:
                    suite.addTest(build_suite(app))

        if extra_tests:
            for test in extra_tests:
                suite.addTest(test)

        return runner.reorder_suite(suite, (unittest.TestCase,))

    def setup_test_environment(self, *args, **kwargs):
        if settings.STAGE == 'TEST':
            from django.db.models.loading import get_models
            self.unmanaged_models = [m for m in get_models()
                                     if not m._meta.managed]
            for m in self.unmanaged_models:
                m._meta.managed = True

        super(ManagedModelTestRunner, self).setup_test_environment(*args,
                                                                   **kwargs)

    def teardown_test_environment(self, *args, **kwargs):
        super(ManagedModelTestRunner, self).teardown_test_environment(*args,
                                                                      **kwargs)
        # reset unmanaged models
        for m in self.unmanaged_models:
            m._meta.managed = False

我如何告诉pytest-django使用我的自定义测试套件运行器?

EN

回答 2

Stack Overflow用户

发布于 2015-07-31 20:36:37

py.test根本不使用单元测试运行器

恐怕您必须在py.test中重做自定义收集

票数 2
EN

Stack Overflow用户

发布于 2020-01-16 21:40:32

正如Ronny提到的,py.test没有运行器,但是您可以通过利用测试套件中的conftest.py文件的各种功能来获得相同的功能。

对于数据库设置,可以使用django_db_setup fixture:https://pytest-django.readthedocs.io/en/latest/database.html#django-db-setup

对于更一般的东西,你可以在你的conftest中使用一个函数pytest_configure

conftest.py

代码语言:javascript
复制
@pytest.fixture
def django_db_setup():
    # db setup

def pytest_configure(config):
    # other pytest stuff
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27280778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档