0
0
PyTesttesting~10 mins

Branch coverage 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 assert the function returns True for positive input.

PyTest
def is_positive(num):
    return num > 0

def test_positive():
    assert is_positive([1]) == True
Drag options to blanks, or click blank then click option'
A0
B-3
C5
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or negative numbers which return False.
2fill in blank
medium

Complete the test to check the function returns False for zero input.

PyTest
def is_positive(num):
    return num > 0

def test_zero():
    assert is_positive([1]) == False
Drag options to blanks, or click blank then click option'
A10
B0
C-1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive numbers which return True.
3fill in blank
hard

Fix the error in the test to cover the negative branch correctly.

PyTest
def is_positive(num):
    return num > 0

def test_negative():
    assert is_positive([1]) == False
Drag options to blanks, or click blank then click option'
A-2
B0
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or positive numbers which do not test the negative branch.
4fill in blank
hard

Fill both blanks to complete a test that covers both branches using pytest parametrize.

PyTest
import pytest

@pytest.mark.parametrize('input, expected', [
    ([1], True),
    ([2], False)
])
def test_is_positive(input, expected):
    assert is_positive(input) == expected
Drag options to blanks, or click blank then click option'
A10
B-5
C0
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero which returns False but is not negative.
5fill in blank
hard

Fill all three blanks to create a test function that covers positive, zero, and negative inputs with correct assertions.

PyTest
import pytest

@pytest.mark.parametrize('num, expected', [
    ([1], True),
    ([2], False),
    ([3], False)
])
def test_branch_coverage(num, expected):
    assert is_positive(num) == expected
Drag options to blanks, or click blank then click option'
A3
B0
C-1
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero as positive or negative.