Pytest - 在 class_method 上访问 session_scope 夹具
Pytest - Access session_scope fixture on class_method
我正在使用 celery fixtures 来测试项目中的几个任务,我想知道是否有一种方法可以访问会话范围 fixture(celery fixtures 默认为会话范围)引用class方法:
@pytest.mark.usefixture('celery_worker')
class TasksTest(TestCase):
def test_my_task_a(self):
tas_info = my_task_a.delay()
assert celery_worker.something # access the celery_worker reference raises an error
如pytest
docs所述:
Note:
unittest.TestCase
methods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.
The above usefixtures
and autouse
examples should help to mix in pytest fixtures into unittest suites.
You can also gradually move away from subclassing from unittest.TestCase
to plain asserts and then start to benefit from the full pytest feature set step by step.
因此,按照此建议,您可以在 class 级别上定义一个“pass-through”自动夹具,它接受其他夹具作为参数并存储对单个测试中您应该需要的任何内容的引用:
class TasksTest(TestCase):
@pytest.fixture(autouse=True)
def my_celery_passthrough(self, celery_worker, celery_session_worker):
self.worker = celery_worker
self.session_worker = celery_session_worker
def test_spam(self):
assert 'macbookpro2017' in self.worker.hostname
assert 'macbookpro2017' in self.session_worker.hostname
但是,让我重复上面引用的最后一部分:
You can also gradually move away from subclassing from unittest.TestCase
to plain asserts and then start to benefit from the full pytest feature set step by step.
既然您已经在使用 pytest
功能,而 unittest
将不再理解 - 为什么要坚持测试 classes?从一开始就将新测试编写为函数,这样您就不必引入任何变通方法。至于旧的基于class的测试,你可以暂时保留它们,稍后重写,when/if你必须修改它们。
我正在使用 celery fixtures 来测试项目中的几个任务,我想知道是否有一种方法可以访问会话范围 fixture(celery fixtures 默认为会话范围)引用class方法:
@pytest.mark.usefixture('celery_worker')
class TasksTest(TestCase):
def test_my_task_a(self):
tas_info = my_task_a.delay()
assert celery_worker.something # access the celery_worker reference raises an error
如pytest
docs所述:
Note:
unittest.TestCase
methods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.The above
usefixtures
andautouse
examples should help to mix in pytest fixtures into unittest suites.You can also gradually move away from subclassing from
unittest.TestCase
to plain asserts and then start to benefit from the full pytest feature set step by step.
因此,按照此建议,您可以在 class 级别上定义一个“pass-through”自动夹具,它接受其他夹具作为参数并存储对单个测试中您应该需要的任何内容的引用:
class TasksTest(TestCase):
@pytest.fixture(autouse=True)
def my_celery_passthrough(self, celery_worker, celery_session_worker):
self.worker = celery_worker
self.session_worker = celery_session_worker
def test_spam(self):
assert 'macbookpro2017' in self.worker.hostname
assert 'macbookpro2017' in self.session_worker.hostname
但是,让我重复上面引用的最后一部分:
You can also gradually move away from subclassing from
unittest.TestCase
to plain asserts and then start to benefit from the full pytest feature set step by step.
既然您已经在使用 pytest
功能,而 unittest
将不再理解 - 为什么要坚持测试 classes?从一开始就将新测试编写为函数,这样您就不必引入任何变通方法。至于旧的基于class的测试,你可以暂时保留它们,稍后重写,when/if你必须修改它们。