Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark a test as flaky in pytest.
Testing Fundamentals
@pytest.mark.[1] def test_example(): assert True
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.mark.skip will skip the test instead of marking it flaky.
Using @pytest.mark.parametrize is for running tests with multiple inputs.
✗ Incorrect
Using @pytest.mark.flaky marks the test as flaky so it can be retried or handled specially.
2fill in blank
mediumComplete the code to retry a flaky test up to 3 times using pytest-rerunfailures.
Testing Fundamentals
@pytest.mark.flaky(reruns=[1]) def test_flaky(): assert random.choice([True, False])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting reruns=0 disables retries.
Setting reruns=1 retries only once, which may be insufficient.
✗ Incorrect
Setting reruns=3 retries the flaky test up to 3 times before failing.
3fill in blank
hardFix the error in the flaky test decorator syntax.
Testing Fundamentals
@pytest.mark.flaky(reruns=[1]) def test_network(): assert ping_server()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string '3' causes a type error.
Using True or None is invalid for reruns parameter.
✗ Incorrect
reruns expects an integer, so 3 without quotes is correct.
4fill in blank
hardFill both blanks to create a dictionary of test names and their flaky status.
Testing Fundamentals
flaky_tests = {test_name: [1] for test_name in tests if tests[test_name] [2] True} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using test_name as value instead of flaky status.
Using '!=' instead of '==' causes wrong filtering.
✗ Incorrect
We use tests[test_name] to get the flaky status and check if it equals True.
5fill in blank
hardFill both blanks to filter flaky tests and print their names.
Testing Fundamentals
for test, is_flaky in tests.items(): if is_flaky [1] True: print(test,'Flaky test detected: '[2] test)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' reverses the condition.
Using '+' instead of ',' in print causes syntax errors.
Using ',' instead of '+' for string concatenation inside print.
✗ Incorrect
We check if is_flaky == True, then print with comma to separate, and use + to concatenate strings.