0
0
PyTesttesting~20 mins

Test independence in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Test Independence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is test independence important?

Imagine you have multiple tests for a calculator app. Why should each test be independent from others?

ABecause tests should share data to reduce code duplication.
BSo that one test's failure doesn't cause others to fail, making debugging easier.
CTo make tests run slower and use more resources.
DTo ensure tests always run in a fixed order.
Attempts:
2 left
💡 Hint

Think about how one broken test might affect others if they depend on each other.

Predict Output
intermediate
2:00remaining
What is the output of these pytest tests?

Consider these two pytest tests that share a global variable. What will be the test results?

PyTest
counter = 0

def test_increment():
    global counter
    counter += 1
    assert counter == 1

def test_increment_again():
    global counter
    counter += 1
    assert counter == 1
ABoth tests fail
BBoth tests pass
Ctest_increment fails, test_increment_again passes
Dtest_increment passes, test_increment_again fails
Attempts:
2 left
💡 Hint

Think about the value of counter when each test runs.

🔧 Debug
advanced
2:30remaining
Identify the cause of flaky tests due to shared state

These pytest tests sometimes fail randomly. What is the main cause?

PyTest
shared_list = []

def test_add_item():
    shared_list.append(1)
    assert 1 in shared_list

def test_clear_list():
    shared_list.clear()
    assert shared_list == []
ATests share mutable global state causing interference between tests.
BTests use different assert statements causing confusion.
CTests do not import pytest properly.
DTests run in parallel causing race conditions.
Attempts:
2 left
💡 Hint

Look at the variable shared_list and how it is used.

framework
advanced
1:30remaining
How to ensure test independence in pytest?

Which pytest feature helps to create independent tests by providing fresh data for each test?

AUsing global variables to share data between tests.
BRunning tests in a fixed order using <code>pytest-order</code> plugin.
CUsing fixtures with <code>scope='function'</code> to provide fresh setup for each test.
DDisabling assertions to speed up tests.
Attempts:
2 left
💡 Hint

Think about how pytest can prepare data or state before each test runs.

assertion
expert
2:00remaining
Which assertion best checks test independence in pytest?

You want to verify that a test does not affect global state. Which assertion is best?

PyTest
global_state = {'count': 0}

def test_modify_state():
    global_state['count'] += 1
    # Which assertion below best ensures test independence?
Aassert global_state['count'] == 0
Bassert global_state['count'] > 0
Cassert isinstance(global_state, dict)
Dassert global_state != {}
Attempts:
2 left
💡 Hint

Think about what value count should have if tests are independent.