0
0
Testing Fundamentalstesting~10 mins

Contract testing basics in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a service provider meets the expectations defined by a consumer contract. It verifies that the provider returns the correct response for a given request.

Test Code - unittest
Testing Fundamentals
import unittest

class ProviderService:
    def get_user(self, user_id):
        # Simulate provider response
        if user_id == 1:
            return {"id": 1, "name": "Alice"}
        else:
            return None

class ContractTest(unittest.TestCase):
    def setUp(self):
        self.provider = ProviderService()

    def test_get_user_contract(self):
        # Consumer expects user with id 1 and name Alice
        expected_response = {"id": 1, "name": "Alice"}
        actual_response = self.provider.get_user(1)
        self.assertEqual(actual_response, expected_response)

if __name__ == '__main__':
    unittest.main()
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsTest framework initializes and prepares to run tests-PASS
2ProviderService instance is createdProviderService ready to respond to requests-PASS
3Calls get_user(1) on ProviderServiceProviderService processes request for user id 1-PASS
4Receives response {"id": 1, "name": "Alice"}Response matches expected contractCheck if actual response equals expected response {"id": 1, "name": "Alice"}PASS
5Test completes successfullyAll assertions passed, contract is fulfilled-PASS
Failure Scenario
Failing Condition: ProviderService returns a different user data or None for user id 1
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify in contract testing?
AThe consumer UI displays the data correctly
BThe provider returns the expected response defined by the consumer
CThe provider's database connection is active
DThe network latency is below threshold
Key Result
Contract testing ensures that the service provider meets the consumer's expectations by verifying the exact response for a given request, preventing integration issues early.