0
0
Testing Fundamentalstesting~10 mins

State transition testing in Testing Fundamentals - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a state transition function that moves from 'locked' to 'unlocked'.

Testing Fundamentals
def transition(state, event):
    if state == 'locked' and event == 'coin':
        return [1]
    return state
Drag options to blanks, or click blank then click option'
A'locked'
B'open'
C'unlocked'
D'closed'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the same state instead of changing it.
2fill in blank
medium

Complete the code to check if the current state is 'unlocked' before locking it again.

Testing Fundamentals
def transition(state, event):
    if state == [1] and event == 'push':
        return 'locked'
    return state
Drag options to blanks, or click blank then click option'
A'locked'
B'open'
C'closed'
D'unlocked'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the wrong state before locking.
3fill in blank
hard

Fix the error in the transition function to handle an invalid event by returning the current state.

Testing Fundamentals
def transition(state, event):
    if state == 'locked' and event == 'coin':
        return 'unlocked'
    elif state == 'unlocked' and event == 'push':
        return 'locked'
    else:
        return [1]
Drag options to blanks, or click blank then click option'
ANone
Bstate
C'invalid'
D'error'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning an error string instead of the current state.
4fill in blank
hard

Fill both blanks to complete the state transition dictionary and the function that uses it.

Testing Fundamentals
transitions = {
    'locked': {'coin': [1],
    'unlocked': {'push': [2]
}

def transition(state, event):
    return transitions.get(state, {}).get(event, state)
Drag options to blanks, or click blank then click option'
A'unlocked'
B'locked'
C'open'
D'closed'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the states or using incorrect state names.
5fill in blank
hard

Fill all three blanks to create a test case that verifies the transition from 'locked' to 'unlocked' on 'coin' event.

Testing Fundamentals
def test_locked_to_unlocked():
    initial_state = [1]
    event = [2]
    expected_state = [3]
    actual_state = transition(initial_state, event)
    assert actual_state == expected_state, f"Expected {expected_state}, got {actual_state}"
Drag options to blanks, or click blank then click option'
A'locked'
B'coin'
C'unlocked'
D'push'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up event names or expected states.