lgwillmore / typemock Goto Github PK
View Code? Open in Web Editor NEWType safe mocking for python 3
License: MIT License
Type safe mocking for python 3
License: MIT License
Figure out how to handle ClassVar
type annotation. Do it.
Hi!
What's the status of this project?
I saw a note that it's still alfa and there haven't been any changes for quite a while already.
Any plans to continue working on it?
Should be able to mock a function. Something like.
def a_function(some_arg: str) -> int:
pass
with tmock(a_function) as mocked_function:
when(mocked_function("hello")).then_return(1)
The tmock
function returns a context manager. But, this means that mocking something that wants to be mocked as a context manager will have issues. This is closely related to being able to mock all magic methods. Solve both.
Potential solution:
Consider the following example:
class MyEntity(BaseModel):
id: uuid.UUID
name: Optional[str]
class MyRepository:
def save(self, entity: MyEntity) -> None:
pass
class MyService:
def __init__(self, repository: MyRepository):
self.__repository = repository
def run(self, entity: MyEntity):
self.__repository.save(entity)
entity.name = 'test'
def test_typemock_deep_copy():
entity = MyEntity(id=uuid.uuid4())
entity_copy = entity.copy(deep=True)
with tmock(MyRepository) as repository:
when(repository.save(entity)).then_return(None)
service = MyService(repository)
service.run(entity_copy)
verify(repository).save(entity)
Here MyService
makes modifications to an entity after a call to repository. When test verifies that the call was made if fails, because it cannot find matching call, because recorded interaction contains a reference to the method arguments instead of making copies.
Mock
has the same problem in Python and there's an example of the CopyingMock
on the official documentation.
I can try to prepare PR to fix it, or maybe it could be optional, by adding an extra parameter to tmock
function. Personally I don't see any harm in making deep copy all the time, there might be some performance concerns though.
What do you think?
Need to pretty much ignore type safety for methods with args/kwargs.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.