My QA Projects

QA Projects I was involded.

View on GitHub

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:

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 --junitxml=report.xml test_binary_search.py

Test Report - HTML Report

pip install pytest-html
pytest --html=report.html test_binary_search.py

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