python-betterproto/tests/test_deprecated.py
Michał Górny 32d642d2a0
Update no-warning tests for pytest-8 (#622)
Replace the deprecated `pytest.warns(None)` with the suggested
replacement (from https://github.com/pytest-dev/pytest/issues/9404)
to make the test suite forward compatible with pytest-8.  This works
correctly with pytest-6 as well.
2024-10-05 13:08:49 +01:00

62 lines
1.6 KiB
Python

import warnings
import pytest
from tests.mocks import MockChannel
from tests.output_betterproto.deprecated import (
Empty,
Message,
Test,
TestServiceStub,
)
@pytest.fixture
def message():
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
return Message(value="hello")
def test_deprecated_message():
with pytest.warns(DeprecationWarning) as record:
Message(value="hello")
assert len(record) == 1
assert str(record[0].message) == f"{Message.__name__} is deprecated"
def test_message_with_deprecated_field(message):
with pytest.warns(DeprecationWarning) as record:
Test(message=message, value=10)
assert len(record) == 1
assert str(record[0].message) == f"{Test.__name__}.message is deprecated"
def test_message_with_deprecated_field_not_set(message):
with warnings.catch_warnings():
warnings.simplefilter("error")
Test(value=10)
def test_message_with_deprecated_field_not_set_default(message):
with warnings.catch_warnings():
warnings.simplefilter("error")
_ = Test(value=10).message
@pytest.mark.asyncio
async def test_service_with_deprecated_method():
stub = TestServiceStub(MockChannel([Empty(), Empty()]))
with pytest.warns(DeprecationWarning) as record:
await stub.deprecated_func(Empty())
assert len(record) == 1
assert str(record[0].message) == f"TestService.deprecated_func is deprecated"
with warnings.catch_warnings():
warnings.simplefilter("error")
await stub.func(Empty())