Writing Unit Tests for Improved Code Reliability in Python
Explore how to write effective unit tests in Python using frameworks like unittest and pytest to ensure your code behaves as expected.
0 likes
13 views
Rule Content
{ "title": "Writing Unit Tests for Improved Code Reliability in Python", "description": "Explore how to write effective unit tests in Python using frameworks like unittest and pytest to ensure your code behaves as expected.", "category": "Python Cursor Rules", "rules": [ { "name": "Use Descriptive Test Names", "description": "Assign meaningful and descriptive names to your test functions to clearly convey their purpose, enhancing readability and maintainability.", "example": "def test_calculate_discount_for_loyal_customer():" }, { "name": "Test One Aspect per Function", "description": "Each test function should focus on a single behavior or functionality to simplify debugging and ensure clarity.", "example": "def test_add_with_positive_numbers():\n assert add(2, 3) == 5" }, { "name": "Follow the Arrange-Act-Assert (AAA) Pattern", "description": "Structure your tests into three phases: Arrange (setup), Act (execute), and Assert (verify) to improve organization and readability.", "example": "def test_add_numbers():\n # Arrange\n a = 5\n b = 7\n # Act\n result = a + b\n # Assert\n assert result == 12" }, { "name": "Isolate Tests with Mocking", "description": "Use mocking to replace external dependencies, ensuring that unit tests are isolated and focused solely on the code under test.", "example": "from unittest.mock import Mock\n\ndef test_external_api_call():\n # Arrange\n mock_api = Mock()\n mock_api.get_data.return_value = {\"id\": 1, \"name\": \"Test\"}\n # Act\n result = mock_api.get_data()\n # Assert\n assert result == {\"id\": 1, \"name\": \"Test\"}" }, { "name": "Use Fixtures for Setup and Teardown", "description": "Utilize fixtures to manage setup and teardown processes, promoting code reuse and reducing redundancy in test cases.", "example": "import pytest\n\n@pytest.fixture\ndef sample_data():\n return [1, 2, 3, 4]\n\ndef test_sum(sample_data):\n assert sum(sample_data) == 10" }, { "name": "Test Edge and Boundary Cases", "description": "Ensure your tests cover edge cases and boundary conditions to identify potential issues in extreme scenarios.", "example": "import pytest\n\ndef test_division_by_zero():\n with pytest.raises(ZeroDivisionError):\n result = 1 / 0" }, { "name": "Keep Tests Small and Independent", "description": "Write tests that are concise and do not depend on the state or outcome of other tests to maintain reliability and ease of maintenance.", "example": "def test_functionality_a():\n assert functionality_a() == expected_result_a\n\ndef test_functionality_b():\n assert functionality_b() == expected_result_b" }, { "name": "Run Tests Frequently", "description": "Incorporate testing into your development workflow by running tests frequently to catch issues early and ensure code stability.", "example": "ptw # Automatically runs tests on file changes" }, { "name": "Aim for Meaningful Test Coverage", "description": "Strive for high test coverage by testing critical and complex paths in your code, using tools like coverage.py to measure and improve coverage.", "example": "coverage run -m pytest\ncoverage report" }, { "name": "Document Your Tests", "description": "Add comments or docstrings to explain complex test scenarios, aiding in understanding and future maintenance.", "example": "# Test ensuring VIP customers receive a 20% discount\ndef test_vip_customer_discount():\n ..." } ] }