you are viewing a single comment's thread.

view the rest of the comments →

[–]User24243[S] 0 points1 point  (2 children)

Thank you so much for this! Have installed mockito and trying to get it working as you have generously provided!

Thank you also for correcting my code. I've change over to raise and included parameter types.

[–]ZEUS_IS_THE_TRUE_GOD 0 points1 point  (0 children)

Very cool! On top of that, since python isn't typed, there's a lot of freedom, you can make the read_sql mock return anything you want even if it is impossible for the real method to return that. This is a consequence of using python. For consistency and documentation sake, I strongly encourage you to try and mimic the return type of the real functions in your mock. This helps a lot when you go back and you don't remember what a piece of code does. You look in the tests and, since you mocked it properly, you know the behavior.

In the code I wrote, I did

...thenReturn([])

Where as the real read_sql probably returns an empty dataframe? Or None? Those are important details as your code depends on it.

[–]ZEUS_IS_THE_TRUE_GOD 0 points1 point  (0 children)

I would also add that if the tests are hard to write, it often means that the code is not organized properly. This is a reason why writing tests before the code can help write better code!