0
0
PyTesttesting~10 mins

Conditional parametrize in PyTest - Interactive Code Practice

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

Complete the code to parametrize the test with values 1, 2, and 3.

PyTest
import pytest

@pytest.mark.parametrize('num', [1])
def test_is_positive(num):
    assert num > 0
Drag options to blanks, or click blank then click option'
A(1, 2, 3)
B[1, 2, 3]
C{1, 2, 3}
D'1,2,3'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a list or tuple.
Using curly braces which create a set, not a list or tuple.
2fill in blank
medium

Complete the code to skip the test when the parameter is 0.

PyTest
import pytest

@pytest.mark.parametrize('num', [0, 1, 2])
def test_non_zero(num):
    if num == 0:
        pytest.skip([1])
    assert num != 0
Drag options to blanks, or click blank then click option'
A'Skipping zero value'
B0
CTrue
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing None or non-string values to pytest.skip().
Not providing any message.
3fill in blank
hard

Fix the error in the conditional parametrize to only run test for even numbers.

PyTest
import pytest

values = [1, 2, 3, 4]
@pytest.mark.parametrize('num', [v for v in values if [1]])
def test_even(num):
    assert num % 2 == 0
Drag options to blanks, or click blank then click option'
Av % 2 = 0
Bv // 2 == 0
Cv % 2 == 0
Dv & 2 == 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment operator '=' instead of comparison '=='.
Using integer division '//' which does not check evenness.
Using bitwise AND incorrectly.
4fill in blank
hard

Fill both blanks to parametrize test only for numbers greater than 2 and skip others.

PyTest
import pytest

values = [1, 2, 3, 4]
@pytest.mark.parametrize('num', [v for v in values if v [1] [2]])
def test_greater_than_two(num):
    assert num > 2
Drag options to blanks, or click blank then click option'
A>
B2
C<
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator which selects wrong values.
Using wrong number in comparison.
5fill in blank
hard

Fill all three blanks to parametrize test with uppercase strings only and skip lowercase.

PyTest
import pytest

words = ['Hello', 'WORLD', 'test', 'PYTHON']
@pytest.mark.parametrize('word', [w for w in words if w.[1]() == w])
def test_uppercase(word):
    if not word.isupper():
        pytest.skip([2])
    assert word.isupper() is [3]
Drag options to blanks, or click blank then click option'
Aupper
B'Skipping lowercase word'
CTrue
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() instead of upper() in the filter.
Not providing a skip message.
Asserting with incorrect boolean value.