Python Developer Interview Question

How would you test a function that calls a third party payment API?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Isolate the boundary. Put the HTTP call behind a thin client, then in tests replace it with a fake or patch it where it is used, not where it is defined. Use pytest fixtures for setup and parametrize for the success, failure, and timeout cases. Add a small set of contract tests against the real sandbox so mocks cannot drift.

Why interviewers ask this

Interviewers use this to see how you think about test boundaries and design. Anyone who patches deep internals is telling you their code is not seam friendly. They also listen for pytest fluency, meaning fixtures, parametrize, and monkeypatch, and for awareness that mocks encode assumptions which rot, so something has to verify the real contract occasionally.

How to structure your answer

  • Design a seam before you discuss mocking.
  • Explain patching at the point of use.
  • Cover failure paths with parametrize.
  • Guard against mock drift with contract tests.

Example answer

Spoken example, first person

Testability is a design question first. If the payment call is buried inside business logic I pull it out into a client class with a couple of methods, and then the logic takes that client as an argument. At that point most tests need no mocking library at all, because I pass a small fake that returns canned responses, which is easier to read than a stack of patch decorators. Where I do patch, I patch the name in the module under test, not in the library module, because patching where it is defined does nothing once it has been imported elsewhere. Fixtures build the objects, and parametrize covers the interesting cases: approved, declined, timeout, and a malformed payload, since the failure branches are what actually break in production. I also keep a handful of tests against the provider sandbox, tagged so they run nightly rather than on every commit. That caught a change where the provider started returning an error code we had never mocked.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How do you avoid tests that assert on mock internals?
  • When is a fake better than a MagicMock?
  • How would you test retry and backoff behavior?

Related python developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot