Back to Repositories

Testing Search Algorithm Implementation in Python-100-Days

This test suite implements unit tests for search algorithms in Python, specifically validating sequential and binary search functions. It demonstrates comprehensive test coverage using the unittest framework with setup and teardown capabilities.

Test Coverage Overview

The test suite provides thorough coverage of two search algorithms – sequential search (seq_search) and binary search (bin_search).

Key functionality tested includes:
  • Successful searches with elements at different positions (start, middle, end)
  • Edge cases for non-existent elements
  • Boundary value testing
  • Different input data arrangements (unsorted for sequential, sorted for binary)

Implementation Analysis

The implementation follows unittest’s TestCase pattern with a structured approach using setUp for test data initialization.

Technical patterns include:
  • Test method naming convention (test_ prefix)
  • Assertion-based validation using assertEqual
  • Fixture setup with sorted and unsorted test data
  • Systematic test case organization for each search function

Technical Details

Testing tools and configuration:
  • Python unittest framework
  • Optional support for pytest and nose2
  • Coverage reporting capabilities
  • Test execution via unittest.main() or command line
  • Modular test structure with separate test class

Best Practices Demonstrated

The test suite exemplifies several testing best practices and quality standards.

Notable practices include:
  • Clear test method documentation
  • Proper test isolation through setUp/tearDown
  • Comprehensive edge case coverage
  • Consistent assertion patterns
  • Organized test data management

jackfrued/python-100-days

Day16-20/code/test_example01.py

            
"""
单元测试 - 针对程序中最小的功能模块(函数和方法)的测试
测试方法:
- 白盒测试:程序自己写的测试
- 黑盒测试:测试人员或QA,不知道代码实现细节,只关注功能
编写Python单元测试 - 定义类继承TestCase,写测试方法(test_开头)
执行单元测试:
- unittest.main()
- python3 -m unittest test_example01.py
第三方库 - nose2 / pytest
pip install pytest pytest-cov
pytest -v --cov
------------------------------
pip install nose2 cov-core
nose2 -v -C
"""
from unittest import TestCase

from example01 import seq_search, bin_search


class TestExample01(TestCase):
    """测试查找函数的测试用例"""

    # 执行每个测试函数之前要执行的方法
    def setUp(self):
        self.data1 = [35, 97, 12, 68, 55, 73, 81, 40]
        self.data2 = [12, 35, 40, 55, 68, 73, 81, 97]

    # 执行每个测试函数之后要执行的方法
    def tearDown(self):
        pass

    def test_seq_search(self):
        """测试顺序查找"""
        self.assertEqual(0, seq_search(self.data1, 35))
        self.assertEqual(2, seq_search(self.data1, 12))
        self.assertEqual(6, seq_search(self.data1, 81))
        self.assertEqual(7, seq_search(self.data1, 40))
        self.assertEqual(-1, seq_search(self.data1, 99))
        self.assertEqual(-1, seq_search(self.data1, 7))

    def test_bin_search(self):
        """测试二分查找"""
        self.assertEqual(1, bin_search(self.data2, 35))
        self.assertEqual(0, bin_search(self.data2, 12))
        self.assertEqual(6, bin_search(self.data2, 81))
        self.assertEqual(2, bin_search(self.data2, 40))
        self.assertEqual(7, bin_search(self.data2, 97))
        self.assertEqual(-1, bin_search(self.data2, 7))
        self.assertEqual(-1, bin_search(self.data2, 99))