Created
December 1, 2023 05:37
-
-
Save devops-school/d6ef65f294c90db3aec02179d84bb4e9 to your computer and use it in GitHub Desktop.
Python Test Runners - pytest-trio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install pytest pytest-trio | |
# my_module.py | |
import trio | |
async def trio_async_operation(): | |
async with trio.open_nursery() as nursery: | |
nursery.start_soon(trio_async_task) | |
async def trio_async_task(): | |
await trio.sleep(1) # Simulate an asynchronous operation | |
# tests/test_trio_code.py | |
# In this code, we use the @pytest.mark.trio decorator to mark the test function as a Trio-based asynchronous test. Inside the test, we use Trio's event loop to start the asynchronous operation. | |
import pytest | |
import trio | |
from my_module import trio_async_operation | |
@pytest.mark.trio | |
async def test_trio_async_operation(): | |
async with trio.open_nursery() as nursery: | |
nursery.start_soon(trio_async_operation) | |
# pytest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment