felixdivo / ros2-easy-test

A Python test framework for ROS2 allowing simple and expressive assertions based on message interactions.
https://ros2-easy-test.readthedocs.io
MIT License
15 stars 5 forks source link

Example for use with mock #24

Closed vik748 closed 7 months ago

vik748 commented 9 months ago

Hi, Do you have an example for using ros2_easy_test along with mock. We have a Node which reads from the sensors using the socket library and I'd like to mock either the socket.socket OR the lower level library which uses socket.socket, so that I can fake a read from the sensor and check for published messages.

Thanks in advance for your help.

felixdivo commented 9 months ago

I haven't tried this yet. Is there anything special about this library in this regard? Can't you create the mock object from within your tet case as usual? Or even use @patch?

vik748 commented 9 months ago

The main challenge I ran into is that when we use the decorator from ros2-easy-test, inside the function the node being tested is already created, so I was not able to modify any return values. I was able to work around it by using a Pytest fixture and performing all the mocking in there. How would you approach this?

felixdivo commented 8 months ago

I was able to work around it by using a Pytest fixture and performing all the mocking in there.

That sounds reasonable and like the way to go. I would have tried the same. Do you think this is bad in some significant way? Should we add an example for how to do this to the library?

vik748 commented 8 months ago

I wouldn't say it is bad, it was my first time using pytest so there was an additional layer of uncertainty. It would definitely be helpful to have some examples.

felixdivo commented 8 months ago

Is your use case suitable as a demo? We could think about adding something simple here: tests/demo.

felixdivo commented 7 months ago

Let's close this until there is a concrete proposal for what to do.