Best Python code snippet using autotest_python
invocation.py
Source:invocation.py  
...83    self.params = params   84    self.named_params = named_params 85    return AnswerSelector(self)86  87  def stub_with(self, answer):88    self.answers.append(answer)89    self.mock.stub(self.method_name)90    self.mock.finish_stubbing(self)91    92class AnswerSelector(object):93  def __init__(self, invocation):94    self.invocation = invocation95    self.answer = None96  97  def thenReturn(self, *return_values):98    for return_value in return_values:99      self.__then(Return(return_value))100    return self101    102  def thenRaise(self, *exceptions):103    for exception in exceptions:104      self.__then(Raise(exception))105    return self106  def __then(self, answer):107    if not self.answer:108      self.answer = CompositeAnswer(answer)109      self.invocation.stub_with(self.answer)110    else:111      self.answer.add(answer)112      113    return self      114class CompositeAnswer(object):115  def __init__(self, answer):116    self.answers = [answer]117    118  def add(self, answer):119    self.answers.insert(0, answer)120    121  def answer(self):122    if len(self.answers) > 1:123      a = self.answers.pop()...test_mock.py
Source:test_mock.py  
1from typing import Optional2import hammock3from hammock import AttrContract, ReturnContract4from tests.stack.abstract import StackInt5class MockStack(StackInt[str]):6    def peek(self) -> Optional[str]:7        raise AttrContract[Optional[str]](8            ReturnContract(args=(), kwargs={}, returns="value"),9        )10    def pop(self) -> str:11        raise12    def push(self, value: str) -> None:13        raise14    @property15    def size(self) -> int:16        raise AttrContract[int](ReturnContract(args=(), kwargs={}, returns=10))17def test_stack_mock() -> None:18    patcher = hammock.Patcher()19    stack_mock = patcher.mock(20        MockStack,21        [22            hammock.AttrMock(23                attr=StackInt.peek,24                stub_with="value",25                count=True,26            ),27            hammock.AttrMock(28                attr=StackInt.size,29                stub_with=10,30            ),31        ],32    )33    assert stack_mock.peek() == "value"34    assert stack_mock.size == 1035    assert not stack_mock.is_empty...attr.py
Source:attr.py  
1from dataclasses import dataclass2from typing import Any, Callable, Generic, Optional, TypeVar, Union3from hammock.spy import CallHistory4from hammock.util import name_from_attr5InterfaceType = TypeVar("InterfaceType")6@dataclass(frozen=True)7class AttrMock(Generic[InterfaceType]):8    attr: Union[Callable[[InterfaceType], Any], property]9    stub_with: Any10    count: bool = False11    @property12    def attr_name(self) -> str:13        return name_from_attr(self.attr)14    def wrap_attr(self, attr: Any, call_history: Optional[CallHistory]) -> Any:15        def new_attr_(*args: Any, **kwargs: Any) -> Any:16            return self.stub_with17        def new_attr_with_call_history_(*args: Any, **kwargs: Any) -> Any:18            call_history.register_call(self.attr_name)  # type: ignore19            return self.stub_with20        new_attr = new_attr_ if call_history is None else new_attr_with_call_history_...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
