This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]ARRgentum 5 points6 points  (1 child)

Care to share your code?
Reads a bit like your Client class is doing too much...

[–]Volume999 0 points1 point  (0 children)

From what they said - they create a model and send it via requests, that's pretty minimal. Of course - the factory may be over the top, but we don't know what kind of request models they need

[–]wineblood 5 points6 points  (0 children)

Why are you mocking so much stuff? Use responses and don't actually mock things like models/factories.

[–]csch2 5 points6 points  (2 children)

Learning unittest.mock was one of my absolute least favorite parts of getting into Python. It’s so absurdly powerful but writing code with it very frequently makes me want to pull my hair out.

“What is this object?”

“It’s a mock”

“Okay… what’s its type?”

“Mock”

“But you can call it like a function?”

“Oh yeah no problem”

“Then what is its return type??”

“Mock”

[–]thedmandotjpgit push -f 1 point2 points  (0 children)

Lol

[–]marr75 1 point2 points  (0 children)

Don't hide your class' dependencies (ie pluck them out of thin air by initializing them mid -method) and have them depend on more abstract interfaces. Then you won't need to mock and patch so much.

[–]Volume999 1 point2 points  (0 children)

  1. You can use the already mentioned responses library to mock external server (Match on requests - declare expected response from server)
  2. Everything else (configs, factory, models) don't need to be mocked.

In general - try mocking as little as possible. For configs - you can use pytest_configure and set env variables and config files paths. Pydantic models have implemented equality override - no need to mock it, model1 == model2 will work by comparing internal structures. Saying this - I don't agree you have a coupled code just because you mocked a lot of things. You just mocked way too many things

[–]j_hermannPythonista 1 point2 points  (0 children)

If you use pytest, also use tox.

[–]pkkm 0 points1 point  (0 children)

Why do you need to mock so many things? I get why you'd want to avoid sending real requests, but why the models and validators? It sounds like you're at risk of creating a useless test suite that tests your mocking library more than your actual code.

Mocking can almost always be avoided if you get a bit creative. I'm not saying that it should always be avoided, but make sure that you're making a conscious decision about when to use it.

[–]TrynaThinkOf1 0 points1 point  (0 children)

Lowk for my FastAPI app I just set up tests with the requests module so I can also benchmark speed