Bird
Raised Fist0
Testing Fundamentalstesting~20 mins

Contract testing basics in Testing Fundamentals - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Contract Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary purpose of contract testing?

Imagine two teams working on different parts of a software system. One team builds a service (provider), and the other builds a client (consumer) that uses that service. What is the main goal of contract testing between these two teams?

ATo ensure the provider service has no security vulnerabilities.
BTo test the entire system end-to-end including all external dependencies.
CTo verify that the provider and consumer agree on the data format and interaction rules.
DTo check the user interface responsiveness under load.
Attempts:
2 left
💡 Hint

Think about what needs to be agreed upon between two separate parts that communicate.

❓ Predict Output
intermediate
2:00remaining
What is the output of this contract test result?

Given this simplified contract test result output, what does it indicate?

Testing Fundamentals
Contract Test Result:
Consumer: OrderApp
Provider: PaymentService
Test: PaymentService accepts order payment request
Result: FAIL
Error: Expected field 'amount' to be a number but got string
AThe consumer sent a wrong request format, but the provider responded correctly.
BThe provider returned a string instead of a number for 'amount', causing the test to fail.
CThe test passed successfully with no errors.
DThe provider service is down and did not respond.
Attempts:
2 left
💡 Hint

Look at the error message carefully about the 'amount' field type.

❓ assertion
advanced
2:00remaining
Which assertion correctly verifies a contract response field type?

You want to write an assertion in your contract test to check that the provider's response field status is a string with value 'success'. Which assertion is correct?

Aassert response['status'] == 'success' and isinstance(response['status'], str)
Bassert response.status == 'success' or isinstance(response.status, str)
Cassert response['status'] = 'success' and type(response['status']) == 'string'
Dassert response['status'] == 'success' and type(response['status']) == str
Attempts:
2 left
💡 Hint

Remember Python syntax for assertions and type checking.

🔧 Debug
advanced
2:00remaining
Why does this contract test fail with a KeyError?

Review this contract test snippet and identify why it raises a KeyError:

Testing Fundamentals
response = provider.get_order(123)
assert response['orderId'] == 123
assert response['total'] > 0
assert response['status'] == 'confirmed'
AThe 'total' field is a string, so comparison with 0 causes TypeError.
BThe 'orderId' key is misspelled in the assertion.
CThe provider.get_order method returns None, so indexing fails.
DThe response dictionary does not contain the key 'status', causing KeyError.
Attempts:
2 left
💡 Hint

Check which keys are accessed and whether they exist in the response.

❓ framework
expert
2:00remaining
Which contract testing framework supports consumer-driven contracts with automatic verification?

You want to implement consumer-driven contract testing where the consumer defines the contract and the provider verifies it automatically during CI. Which framework is best suited for this?

APact
BSelenium
CJUnit
DPostman
Attempts:
2 left
💡 Hint

Look for a framework specialized in contract testing between services.

Practice

(1/5)
1. What is the main purpose of contract testing in software development?
easy
A. To ensure two systems agree on the format and data of their communication
B. To test the user interface for visual bugs
C. To check the performance speed of an application
D. To verify database schema changes

Solution

  1. Step 1: Understand contract testing definition

    Contract testing focuses on the agreement between two systems on how they communicate, including request and response formats.
  2. Step 2: Compare options with contract testing purpose

    Only To ensure two systems agree on the format and data of their communication describes this agreement on communication format and data, which is the core of contract testing.
  3. Final Answer:

    To ensure two systems agree on the format and data of their communication -> Option A
  4. Quick Check:

    Contract testing = communication agreement [OK]
Hint: Contract testing checks communication agreements between systems [OK]
Common Mistakes:
  • Confusing contract testing with UI testing
  • Thinking contract testing measures performance
  • Assuming contract testing checks database structure
2. Which of the following is the correct way to describe a contract in contract testing?
easy
A. A set of UI elements and their styles
B. A list of database tables and columns
C. A defined request and expected response between services
D. A performance benchmark for API calls

Solution

  1. Step 1: Identify what a contract defines

    A contract in contract testing defines the request sent and the expected response received between two services.
  2. Step 2: Match options to contract definition

    A defined request and expected response between services correctly describes this request-response definition, while others describe unrelated concepts.
  3. Final Answer:

    A defined request and expected response between services -> Option C
  4. Quick Check:

    Contract = request + expected response [OK]
Hint: Contracts define requests and expected responses [OK]
Common Mistakes:
  • Mixing contracts with UI or database details
  • Thinking contracts are about performance
  • Confusing contracts with test cases
3. Given a contract test that expects a JSON response with a field status equal to "success", what will happen if the actual response is {"status": "fail"}?
medium
A. The test will pass because the response is JSON
B. The test will fail because the status value does not match
C. The test will pass because the field status exists
D. The test will fail due to a syntax error in the response

Solution

  1. Step 1: Understand the contract expectation

    The contract expects the status field to have the value "success" exactly.
  2. Step 2: Compare actual response to expected

    The actual response has "status": "fail", which does not match the expected value, causing the test to fail.
  3. Final Answer:

    The test will fail because the status value does not match -> Option B
  4. Quick Check:

    Expected value mismatch = test fail [OK]
Hint: Exact value mismatch causes contract test failure [OK]
Common Mistakes:
  • Assuming presence of field is enough to pass
  • Ignoring value differences in contract tests
  • Confusing syntax errors with value mismatches
4. You wrote a contract test but it always passes even when the provider sends incorrect data. What is the most likely mistake?
medium
A. The contract test has syntax errors and does not run
B. The contract test is missing the request URL
C. The contract test is running on the wrong environment
D. The contract test is not validating the response content properly

Solution

  1. Step 1: Analyze why test passes despite incorrect data

    If the test passes even with wrong data, it likely means the test is not checking the response content correctly.
  2. Step 2: Evaluate other options

    Missing URL or wrong environment would cause errors or failures, and syntax errors prevent running, so these are less likely.
  3. Final Answer:

    The contract test is not validating the response content properly -> Option D
  4. Quick Check:

    Incorrect validation = false pass [OK]
Hint: Check if test actually validates response content [OK]
Common Mistakes:
  • Ignoring validation logic in tests
  • Assuming environment issues cause false passes
  • Confusing syntax errors with test logic errors
5. You have two microservices: Service A (consumer) and Service B (provider). Service A expects Service B to return a JSON with a userId as a number and active as a boolean. Which contract test scenario best ensures compatibility?
hard
A. Test that Service B returns {"userId": 123, "active": true} and reject if types differ
B. Test that Service B returns any JSON object with keys userId and active regardless of types
C. Test that Service B returns a string containing userId and active anywhere
D. Test that Service B returns a JSON with userId as string and active as number

Solution

  1. Step 1: Identify expected data types in contract

    Service A expects userId as a number and active as a boolean, so the contract must enforce these types.
  2. Step 2: Evaluate options for strictness and correctness

    Test that Service B returns {"userId": 123, "active": true} and reject if types differ tests exact types and values, ensuring compatibility. Options A, B, and C ignore or mismatch types, risking integration errors.
  3. Final Answer:

    Test that Service B returns {"userId": 123, "active": true} and reject if types differ -> Option A
  4. Quick Check:

    Strict type check = compatibility ensured [OK]
Hint: Contract tests must check exact data types and values [OK]
Common Mistakes:
  • Ignoring data types in contract tests
  • Accepting any JSON structure without validation
  • Confusing string and number types in contracts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard