Code for pytesting
def binary_search(arr, target):
# Initialize the low and high pointers
low = 0
high = len(arr) - 1
# While the search space is valid
while low <= high:
# Find the middle index
mid = (low + high) // 2
# Check if the target is at the mid index
if arr[mid] == target:
return mid
# If the target is greater than the mid element, ignore the left half
elif arr[mid] < target:
low = mid + 1
# If the target is less than the mid element, ignore the right half
else:
high = mid - 1
# Return -1 if the target is not found
return -1
# Example
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 7
result = binary_search(arr, target)
if result != -1:
print(f"Element {target} found at index {result}")
else:
print(f"Element {target} not found in the list")
TCs for pytest
import pytest
# Test Cases for pytest
@pytest.mark.parametrize("arr, target, expected", [
([2, 3, 4, 10, 40], 10, 3),
([2, 3, 4, 10, 40], 20, -1),
([1, 2, 3, 4, 5], 3, 2),
([], 10, -1),
([1], 1, 0),
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 7, 6), # Test case for your example
])
def test_binary_search(arr, target, expected):
assert binary_search(arr, target) == expected
Test Cases:
[arr, target, expected]
: This is the list of test cases, each containing:arr
: The sorted array to search in.target
: The element you’re searching for.expected
: The expected index where the element should be found (or -1 if not found).
assert binary_search(arr, target) == expected
: This assertion verifies that the actual result returned by the binary_search function matches the expected value for each TC.- Assertion statement for verifying if the actual result matches the expected result.
- TCs cover various scenarios (empty array, target not found, target in the middle, target at the edges, etc.).
Test Report - Console Output
pytest test_binary_search.py
Passed/failed tests Test durations Error messages (if any) A summary of the results at the end.
Test Report - Console Output
- pytest can generate an XML report in JUnit format, which is used in CI/CD pipelines.
pytest --junitxml=report.xml test_binary_search.py
- check for report.xml in the current directory
Test Report - HTML Report
pip install pytest-html
pytest --html=report.html test_binary_search.py
- This will create an report.html
- Test summaries
- Pass/fail indicators
- Test durations
- Error/failure details
- I never used
pytest markers
to group tests or categorize them (@pytest.mark.slow, @pytest.mark.integration, etc.). pytest fixtures
help to set up common data or resources for your tests. Again, never used it.- Pytest Plugins
pytesting #2
def binary_search(arr, x):
"""Performs binary search on a sorted array.
Args:
arr: The sorted array to search in.
x: The element to search for.
Returns:
The index of the element if found, otherwise -1.
"""
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
### TCs for pytest
import pytest
@pytest.mark.parametrize("arr, x, expected", [
([2, 3, 4, 10, 40], 10, 3),
([2, 3, 4, 10, 40], 20, -1),
([1, 2, 3, 4, 5], 3, 2),
([], 10, -1),
([1], 1, 0),
])
def test_binary_search(arr, x, expected):
assert binary_search(arr, x) == expected
- binary_search() function takes a sorted array arr and a target element x and returns the index of x if found, otherwise -1.
@pytest.mark.parametrize
: This decorator helps create multiple test cases with different inputs and expected outputs.- Test Cases:
- We define a list of test cases [arr, x, expected] using the parametrize decorator. Each test case contains:
arr
: The sorted array to search.x
: The target element to find.expected
: The expected result (index or -1).
- Corner Cases: It has various corner cases (empty array, target not found, target at the edge, etc.) that require testing.