I need some marker, which will allow me to access member in pytest but not in normal code:
<code>class A:
def __init__(self) -> None:
self.__pv_memeber = 0
@some_magic_marker
def get_pv_member(self) -> int:
return self.__pv_memeber
</code>
<code>class A:
def __init__(self) -> None:
self.__pv_memeber = 0
@some_magic_marker
def get_pv_member(self) -> int:
return self.__pv_memeber
</code>
class A:
def __init__(self) -> None:
self.__pv_memeber = 0
@some_magic_marker
def get_pv_member(self) -> int:
return self.__pv_memeber
So that:
<code># normal code
> A().get_pv_member() # raises some exception
</code>
<code># normal code
> A().get_pv_member() # raises some exception
</code>
# normal code
> A().get_pv_member() # raises some exception
but in tests:
<code>def test_pv_member_init_value(a: A) -> None:
assert a.get_pv_member() == 0
</code>
<code>def test_pv_member_init_value(a: A) -> None:
assert a.get_pv_member() == 0
</code>
def test_pv_member_init_value(a: A) -> None:
assert a.get_pv_member() == 0
I can write it myself using this library:
- https://pypi.org/project/pytest-is-running/
or using pytest environment variables, but i’m curious is there dedicated library or solution for this?
I don’t want to go with:
<code>def test_pv_member_init_value(a: A) -> None:
assert a._A__pv_member == 0
</code>
<code>def test_pv_member_init_value(a: A) -> None:
assert a._A__pv_member == 0
</code>
def test_pv_member_init_value(a: A) -> None:
assert a._A__pv_member == 0
as mypy and ruff is screaming and in my opinion it does not look good
I am willing to use such decorator, but i would rather go with dedicated solution (if exists):
<code>ArgumentsT = TypeVar('ArgumentsT')
ReturnT = TypeVar('ReturnT')
class PytestUseOnlyMethodError(BaseException):
"""Raised if function is called outside pytest context."""
def __init__(self, func_name: str) -> None: # noqa: D107
super().__init__(f"Function: `{func_name}` can only be called from pytest")
def pytest_only(function: Callable[[ArgumentsT], ReturnT]) -> Callable[[ArgumentsT], ReturnT]:
"""Marked methods/functions can be only called inside pytest context."""
import pytest_is_running
@wraps(function)
def _wrapper(*args: Any, **kwargs: Any) -> Any:
if pytest_is_running.is_running():
return function(*args, **kwargs)
raise PytestUseOnlyMethodError(function.__name__)
return _wrapper
</code>
<code>ArgumentsT = TypeVar('ArgumentsT')
ReturnT = TypeVar('ReturnT')
class PytestUseOnlyMethodError(BaseException):
"""Raised if function is called outside pytest context."""
def __init__(self, func_name: str) -> None: # noqa: D107
super().__init__(f"Function: `{func_name}` can only be called from pytest")
def pytest_only(function: Callable[[ArgumentsT], ReturnT]) -> Callable[[ArgumentsT], ReturnT]:
"""Marked methods/functions can be only called inside pytest context."""
import pytest_is_running
@wraps(function)
def _wrapper(*args: Any, **kwargs: Any) -> Any:
if pytest_is_running.is_running():
return function(*args, **kwargs)
raise PytestUseOnlyMethodError(function.__name__)
return _wrapper
</code>
ArgumentsT = TypeVar('ArgumentsT')
ReturnT = TypeVar('ReturnT')
class PytestUseOnlyMethodError(BaseException):
"""Raised if function is called outside pytest context."""
def __init__(self, func_name: str) -> None: # noqa: D107
super().__init__(f"Function: `{func_name}` can only be called from pytest")
def pytest_only(function: Callable[[ArgumentsT], ReturnT]) -> Callable[[ArgumentsT], ReturnT]:
"""Marked methods/functions can be only called inside pytest context."""
import pytest_is_running
@wraps(function)
def _wrapper(*args: Any, **kwargs: Any) -> Any:
if pytest_is_running.is_running():
return function(*args, **kwargs)
raise PytestUseOnlyMethodError(function.__name__)
return _wrapper