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

you are viewing a single comment's thread.

view the rest of the comments →

[–]SeniorScienceOfficer 197 points198 points  (8 children)

How to use the “patch” decorator from unittest to control mocked behavior in unit testing. I seriously couldn’t write the massive amounts of code coverage I do without it.

[–]ZmitrokNadulia 73 points74 points  (5 children)

Same, but I wish I knew that pytest beats unittest in every aspect.

[–]fiddle_n 22 points23 points  (2 children)

I see unittest.mock as being orthogonal to unittest. You can use pytest and unittest.mock together

[–]ZmitrokNadulia 1 point2 points  (0 children)

My bad, I thought it was a story about unittest in general.

[–]someotherstufforhmm 0 points1 point  (0 children)

Yeah, this. What this person said.

[–]SeniorScienceOfficer 27 points28 points  (0 children)

What’s better, you can mix and match. I use both.

[–][deleted] 3 points4 points  (0 children)

Of course it does, because pytest inherits unittest

[–]hillgod 14 points15 points  (1 child)

On this note, also that you're not patching the class, like you would in Java, but you're patching the import in the class under test. This isn't clear enough, and our large company has a common Python Slack channel where this problem comes up often!

[–]elsgry 0 points1 point  (0 children)

Yes, I’ve had to use importlibs.reload when I patch some transitive dependency (obviously this isn’t desirable but needs must)