【问题标题】:Use custom test suite runner with pytest-django将自定义测试套件运行器与 pytest-django 一起使用
【发布时间】:2015-02-01 13:12:56
【问题描述】:

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

pytest==2.6.4
pytest-django==2.7.0

通过常规 django-tests,我正在使用扩展 DjangoTestSuiteRunner 的自定义测试套件运行程序,我在我的 settings.py 中进行了配置:

settings.py:

TEST_RUNNER = "dcmanager.tests.runner.ManagedModelTestRunner"

runner.py:

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 使用我的自定义测试套件运行器?

【问题讨论】:

    标签: python django pytest


    【解决方案1】:

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

    恐怕您将不得不重做 py.test 中的自定义集合

    【讨论】:

      【解决方案2】:

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

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

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

      conftest.py

      @pytest.fixture
      def django_db_setup():
          # db setup
      
      def pytest_configure(config):
          # other pytest stuff
      

      【讨论】:

        猜你喜欢
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多