Recall & Review
beginner
What is the main challenge when testing code that interacts with external services?
The main challenge is that external services can be unreliable, slow, or change unexpectedly, which can cause tests to fail even if the code is correct.
Click to reveal answer
beginner
Why do we use mocking in tests involving external services?
Mocking replaces the real external service with a fake one that returns controlled responses, making tests faster, reliable, and independent of the real service.
Click to reveal answer
intermediate
What pytest feature helps to replace parts of your system during tests?
Pytest supports fixtures and the 'monkeypatch' fixture to replace functions or objects temporarily during a test.
Click to reveal answer
intermediate
How can you simulate different responses from an external API in pytest?
You can create a mock function that returns different data or raises exceptions, then use monkeypatch to replace the real API call with this mock during tests.
Click to reveal answer
advanced
What is a good practice to ensure tests with external services remain fast and reliable?
Use mocking or stubbing to avoid real network calls, and test integration with real services separately in dedicated integration tests.
Click to reveal answer
Why should tests avoid calling real external services directly?
✗ Incorrect
Real external services can be slow, unreliable, or change unexpectedly, which makes tests flaky and slow.
What does mocking an external service mean in testing?
✗ Incorrect
Mocking means replacing the real service with a fake version that returns predictable responses for testing.
Which pytest feature helps you replace functions or objects during a test?
✗ Incorrect
The monkeypatch fixture allows you to replace or modify objects temporarily during a test.
What is a benefit of using mocking in tests with external services?
✗ Incorrect
Mocking avoids real network calls, making tests faster and less likely to fail due to external issues.
When should you test with the real external service instead of mocking?
✗ Incorrect
Integration tests check real interactions with external services to ensure everything works end-to-end.
Explain why mocking external services is important in automated tests.
Think about what happens if your test depends on the internet or a service that might be down.
You got /4 concepts.
Describe how you would use pytest to mock an external API call in a test.
Focus on how pytest's monkeypatch can help replace parts of your code during tests.
You got /4 concepts.