【问题标题】:How do I hook a pytest plugin into a unittest-based class?如何将 pytest 插件挂接到基于单元测试的类中?
【发布时间】:2015-11-10 08:40:45
【问题描述】:

我的问题:我需要一种简单的方法将测试方法标记为协程。 (除其他原因外,我想尽量减少 asyncio 版本和原始代码之间的差异。)

以下代码有效——test_one 除外,它被忽略。

我查看了 pytest 和 pytest-asyncio 的内部结构,虽然我(大部分)了解它如何连接起来以增强简单的测试功能,但我不知道如何用单元测试方法。

那我该怎么做呢?

import unittest
import asyncio
import pytest

class TestSimple(unittest.TestCase):

    @pytest.mark.asyncio
    def test_one(self):
        print("One")
        yield from asyncio.sleep(0.2)
        print("Two")

    def test_two(self):
        print("Three")

def test_three():
    print("Four")

@pytest.mark.asyncio
def test_four():
    print("Five")
    yield from asyncio.sleep(0.2)
    print("Six")

【问题讨论】:

    标签: python python-3.x pytest python-asyncio


    【解决方案1】:

    不可能将 unittest 样式与本机 pytest 交错。

    如果您需要编写单元测试风格的测试,您可以使用装饰器,例如aioredis 会。

    【讨论】:

      猜你喜欢
      • 2017-07-29
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      相关资源
      最近更新 更多