gpt4 book ai didi

django - 如何从 python manage.py test 调用 pytest-django?

转载 作者:行者123 更新时间:2023-12-04 13:36:17 25 4
gpt4 key购买 nike

我已经创建了名为 pytest_wrp 的自定义管理命令
所以当我打电话

python manage.py test
这段代码被称为:
class Command(test.Command):

def handle(self, *args, **options):
super(Command, self).handle(*args, **options) # this calls the python manage.py test
self.stdout.write("My code starts from here.")
management.call_command(pytest_wrp.Command(), '--pact-files="{argument}"'.format(argument=path_to_file), '--pact-provider-name="MyService"', verbosity=0)

pytest_wrp基本上有这个代码:
class Command(BaseCommand):
help = "Runs tests with Pytest"

def add_arguments(self, parser):
parser.add_argument("args", nargs=argparse.REMAINDER)

def handle(self, *args, **options):
pytest.main(list(args)) # This doesn't accept the pact args, even if you specify a "--" separator

但这叫 pytest不是 pytest-django因此,我传递的额外参数不会被识别,pytest 无法启动测试套件。
我想为一些测试用例传递额外的参数。
如果有某种方法可以直接调用 pytest-django 并在最佳代码中传递额外的参数。

最佳答案

我在这里找到了我的解决方案:
Can I still use `manage.py test` after switching to django-pytest?
有关完整和全面的文档,您需要查看 this .简单来说,你需要覆盖config/test.pyconfig.py取决于您的应用程序的设置方式

TEST_RUNNER = "your_project.your_app.runner"
还有您的 runner.py看起来像这样
class PytestTestRunner(object):
"""Runs pytest to discover and run tests."""

def __init__(self, verbosity=1, failfast=False, keepdb=False, **kwargs):
self.verbosity = verbosity
self.failfast = failfast
self.keepdb = keepdb

def run_tests(self, test_labels):
"""Run pytest and return the exitcode.

It translates some of Django's test command option to pytest's.
"""
import pytest

argv = []
if self.verbosity == 0:
argv.append('--quiet')
if self.verbosity == 2:
argv.append('--verbose')
if self.verbosity == 3:
argv.append('-vv')
if self.failfast:
argv.append('--exitfirst')
if self.keepdb:
argv.append('--reuse-db')

# NOTE: You don't need to quote the argument value here, they do some weird string pattern matching internally
argv.append('--pact-files={argument}'.format(argument=path_to_file))
argv.append('--pact-provider-name=MyService')
argv.extend(test_labels)
return pytest.main(argv)
请确保您有 pytest-django包裹
pip install pytest-django

关于django - 如何从 python manage.py test 调用 pytest-django?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61753266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com