0
0
PyTesttesting~5 mins

Testing with external services in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ABecause real services always return errors
BBecause real services can be slow or unavailable, causing flaky tests
CBecause tests must always run offline
DBecause external services are illegal to use in tests
What does mocking an external service mean in testing?
AReplacing the real service with a fake one that returns controlled data
BDeleting the external service
CRunning the external service on your local machine
DIgnoring the external service calls
Which pytest feature helps you replace functions or objects during a test?
Aparametrize decorator
Bassert statement
Cmonkeypatch fixture
Dskip marker
What is a benefit of using mocking in tests with external services?
ATests run faster and are more reliable
BTests become more complex
CTests require internet connection
DTests always fail
When should you test with the real external service instead of mocking?
AOnly when the service is down
BNever, mocking is always better
COnly for unit tests
DIn dedicated integration tests to verify real interactions
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.