Editor

Build a Test Fixture

Advanced · Write the code · from Python Mocking and Fixtures: Test Code with External Dependencies

From the lesson: Python Mocking and Fixtures: Test Code with External Dependencies

Before car companies test a new vehicle in a real crash, they use a crash test dummy. The dummy is a stand-in for a real person - it behaves similarly enough to be useful, but nobody gets hurt.

Read the lesson

Create a make_inventory fixture function that returns a dictionary with {'apple': 10, 'banana': 5, 'cherry': 0}. Write three test functions that each call make_inventory() for a fresh copy: test_in_stock (apple count > 0), test_out_of_stock (cherry count == 0), and test_add_stock (add 3 bananas, then banana count == 8). Run all three and print All inventory tests passed!.

# Create the fixture function
# Write 3 test functions
# Run them and print the result

More Mocking exercises

All Python exercises · 50 practice problems · Testing quiz