同时运行测试

时间:2019-06-04 17:03:42

标签: pytest python-asyncio python-trio pytest-asyncio curio

我想使用asyncio(/ curio / trio)和pytest同时运行几个测试,但是我找不到任何信息。我需要自己安排时间吗?如果我愿意,有没有办法提供一个很好的输出来区分(子)测试用例?

这是我正在尝试的一个小玩具示例:

import pytest
import time
import asyncio

pytestmark = pytest.mark.asyncio
expected_duration = 1
accepted_error = 0.1

async def test_sleep():
    start = time.time()
    time.sleep(expected_duration)
    duration = time.time() - start
    assert abs(duration-expected_duration) < accepted_error

async def test_async_sleep():
    start = time.time()
    await asyncio.sleep(expected_duration)
    duration = time.time() - start
    assert abs(duration-expected_duration) < accepted_error

3 个答案:

答案 0 :(得分:3)

不幸的是,pytest在内部工作的方式无法真正在同一调用trio.run / asyncio.run / curio.run下同时运行多个测试。 (这在某些方面也很不错–防止状态在测试之间泄漏,并且至少使用三重奏,它可以使您针对不同的测试以不同的方式配置三重奏,例如,将一个测试设置为使用autojump clock,而另一测试则不使用)

绝对最简单的选择是使用pytest-xdist在单独的线程中运行测试。您仍然可以在每个测试内部使用异步-所有这些异步库都支持在不同线程中运行不同的循环。

如果您确实需要使用异步并发,那么我认为您必须编写一个pytest测试函数,然后在该函数内部进行自己的调度和并发。如果您采用这种方式,那么从pytest的角度来看,只有一个测试,因此要获得良好的每次测试输出并不容易。我猜您可以尝试使用pytest-subtests吗?

答案 1 :(得分:3)

使用Nathaniel建议的pytest-subtests似乎是一个可行的解决方案。这是使用三重奏解决问题的方法,它将对每个以io_开头的函数运行子测试。

import pytest
import sys
import trio
import inspect
import re
import time


pytestmark = pytest.mark.trio
io_test_pattern = re.compile("io_.*")


async def tests(subtests):

    def find_io_tests(subtests, ignored_names):
        functions = inspect.getmembers(sys.modules[__name__], inspect.isfunction)
        for (f_name, function) in functions:
            if f_name in ignored_names:
                continue
            if re.search(io_test_pattern, f_name):
                yield (run, subtests, f_name, function)

    async def run(subtests, test_name, test_function):
        with subtests.test(msg=test_name):
            await test_function()

    self_name = inspect.currentframe().f_code.co_name
    async with trio.open_nursery() as nursery:
        for io_test in find_io_tests(subtests, {self_name}):
            nursery.start_soon(*io_test)


accepted_error = 0.1

async def io_test_1():
    await assert_sleep_duration_ok(1)

async def io_test_2():
    await assert_sleep_duration_ok(2)

async def io_test_3():
    await assert_sleep_duration_ok(3)

async def io_test_4():
    await assert_sleep_duration_ok(4)

async def assert_sleep_duration_ok(duration):
    start = time.time()
    await trio.sleep(duration)
    actual_duration = time.time() - start
    assert abs(actual_duration - duration) < accepted_error

运行python -m pytest -v输出:

============================ test session starts =============================
platform darwin -- Python 3.7.0, pytest-4.6.2, py-1.8.0, pluggy-0.12.0
plugins: asyncio-0.10.0, trio-0.5.2, subtests-0.2.1
collected 1 item

tests/stripe_test.py::tests PASSED                                     [100%]
tests/stripe_test.py::tests PASSED                                     [100%]
tests/stripe_test.py::tests PASSED                                     [100%]
tests/stripe_test.py::tests PASSED                                     [100%]
tests/stripe_test.py::tests PASSED                                     [100%]

========================== 1 passed in 4.07 seconds ==========================

这并不完美,因为百分比仅与测试数量有关,而与子测试数量无关(即此处标记为io_*的函数),但这似乎是一个不错的开始。

还请注意,使用time.time()对三重奏和异步都有意义,但在实际使用情况下,应使用trio.current_time()

使用asyncio可以实现相同的测试,您基本上必须替换三件事:

  • pytestmark = pytest.mark.triopytestmark = pytest.mark.asyncio
  • yield (run, subtests, f_name, function)yield run(subtests, f_name, function)
  • 最后,应该用类似以下内容的内容代替
await asyncio.gather(*find_io_tests(subtests, {self_name}))

答案 2 :(得分:1)

现在有https://github.com/willemt/pytest-asyncio-cooperative

今天2020-03-25的局限性非常严格-您必须确保测试不共享anything(从技术上讲,不要共享可变状态),并且不能使用{ {1}}(从技术上讲,不要模拟其他测试可能使用的任何东西)。

您可以按照https://github.com/pytest-dev/pytest-asyncio/issues/69的讨论进行操作,我相信这很困难,但是可以提出一种方法来标记每个灯具以允许或禁止并发使用,并安排测试以保留这些限制。

相关问题