mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+234
-274
@@ -1,316 +1,276 @@
|
||||
---
|
||||
description: Comprehensive best practices and coding standards for Python projects using the nose2 testing framework. Covers code organization, common patterns, performance, security, testing, and tooling.
|
||||
globs: *test*.py
|
||||
description: This rule file provides opinionated best practices for writing, organizing, and configuring tests using nose2, emphasizing modern Python development workflows and common pitfalls.
|
||||
globs: **/*.py
|
||||
---
|
||||
# nose2 Best Practices and Coding Standards
|
||||
# nose2 Best Practices
|
||||
|
||||
This document provides a comprehensive guide to best practices and coding standards for Python projects using the nose2 testing framework. Following these guidelines will help you write clean, maintainable, and efficient testing code.
|
||||
`nose2` is the modern successor to `nose`, built upon Python's `unittest` module. It offers powerful test discovery, a plugin architecture, and helpers for writing clean, maintainable test suites. This guide outlines essential best practices for leveraging `nose2` effectively in your projects.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
## 1. Project Layout & Naming
|
||||
|
||||
### 1.1. Directory Structure
|
||||
Always organize your tests in a dedicated `tests/` directory at the root of your project. Name test files consistently using the `test_*.py` pattern. This ensures `nose2`'s default discovery works reliably.
|
||||
|
||||
- **Dedicated Test Directory:** Maintain a dedicated directory (e.g., `tests/`) at the project root to house all test files. This keeps test code separate from the application code.
|
||||
- **Mirror Application Structure:** Mirror the structure of your application code within the test directory. For example, if your application has modules in `myapp/module1/` and `myapp/module2/`, create corresponding directories `tests/module1/` and `tests/module2/`.
|
||||
- **Test Modules:** Each application module should have a corresponding test module. For `myapp/module.py`, the test module should be named `tests/test_module.py` or `tests/module_test.py`
|
||||
**❌ BAD: Scattered tests or inconsistent naming**
|
||||
```
|
||||
# Project root
|
||||
my_module/
|
||||
feature_a.py
|
||||
feature_a_tests.py # Inconsistent naming
|
||||
test_feature_b.py # Mixed with source
|
||||
```
|
||||
|
||||
Example:
|
||||
**✅ GOOD: Dedicated `tests/` directory and `test_*.py` naming**
|
||||
```
|
||||
# Project root
|
||||
my_module/
|
||||
__init__.py
|
||||
feature_a.py
|
||||
feature_b.py
|
||||
tests/
|
||||
__init__.py
|
||||
test_feature_a.py
|
||||
test_feature_b.py
|
||||
```
|
||||
|
||||
## 2. Configuration (`pyproject.toml`)
|
||||
|
||||
myproject/
|
||||
├── myapp/
|
||||
│ ├── __init__.py
|
||||
│ ├── module1.py
|
||||
│ ├── module2.py
|
||||
│ └── ...
|
||||
├── tests/
|
||||
│ ├── __init__.py
|
||||
│ ├── test_module1.py
|
||||
│ ├── test_module2.py
|
||||
│ └── ...
|
||||
├── ...
|
||||
Centralize all `nose2` configuration within your `pyproject.toml` file under the `[tool.nose2]` table. This keeps your project configuration tidy, version-controlled, and compatible with modern Python tooling. Avoid `unittest.cfg` or `nose2.cfg` for new projects.
|
||||
|
||||
**❌ BAD: Using legacy `.cfg` files**
|
||||
```ini
|
||||
# unittest.cfg
|
||||
[unittest]
|
||||
start-dir = tests
|
||||
```
|
||||
|
||||
### 1.2. File Naming Conventions
|
||||
**✅ GOOD: `pyproject.toml` for all configuration**
|
||||
```toml
|
||||
# pyproject.toml
|
||||
[tool.nose2]
|
||||
start-dir = "tests"
|
||||
code-directories = ["src"] # Add your source code directories to sys.path if needed
|
||||
plugins = [
|
||||
"nose2.plugins.coverage",
|
||||
"nose2.plugins.params",
|
||||
"nose2.plugins.junitxml",
|
||||
]
|
||||
exclude-plugins = [
|
||||
"nose2.plugins.loader.functions", # Avoid loading plain functions as tests
|
||||
]
|
||||
|
||||
- **Test File Prefix:** Name test files with a prefix like `test_` or a suffix `_test` (e.g., `test_module.py` or `module_test.py`). nose2 automatically discovers files that follow this convention.
|
||||
- **Descriptive Names:** Use descriptive file names that clearly indicate the module or component being tested. For example, `test_user_authentication.py` for tests related to user authentication.
|
||||
[tool.nose2.coverage]
|
||||
always-on = true
|
||||
coverage-combine = true # Essential for multiprocessing
|
||||
coverage-report = ["term", "html"]
|
||||
coverage-config = ".coveragerc"
|
||||
```
|
||||
|
||||
### 1.3. Module Organization
|
||||
## 3. Test Structure & Assertions
|
||||
|
||||
- **Single Responsibility:** Each test module should focus on testing a single application module or a closely related set of functionalities.
|
||||
- **Test Classes:** Organize tests within a module into test classes. Each class should test a specific aspect or component of the module. Inherit from `unittest.TestCase`.
|
||||
- **Test Functions/Methods:** Each test case should be a separate function (starting with `test_`) within a test class. These methods should be small, focused, and test a single condition or scenario.
|
||||
All tests must inherit from `unittest.TestCase`. Inside your test methods, use `self.assert*` methods provided by `unittest.TestCase` for detailed failure reports. Avoid raw `assert` statements, as they offer less diagnostic information on failure.
|
||||
|
||||
Example:
|
||||
|
||||
python
|
||||
# tests/test_user.py
|
||||
**❌ BAD: Raw `assert` statements**
|
||||
```python
|
||||
import unittest
|
||||
from myapp import user
|
||||
|
||||
class TestUser(unittest.TestCase):
|
||||
class MyFeatureTest(unittest.TestCase):
|
||||
def test_addition(self):
|
||||
result = 1 + 1
|
||||
assert result == 3, "Addition failed" # Vague error message
|
||||
```
|
||||
|
||||
def setUp(self):
|
||||
# Setup code to run before each test
|
||||
self.user = user.User("testuser", "password")
|
||||
**✅ GOOD: `unittest.TestCase` and `self.assert*` methods**
|
||||
```python
|
||||
import unittest
|
||||
|
||||
def tearDown(self):
|
||||
# Teardown code to run after each test
|
||||
class MyFeatureTest(unittest.TestCase):
|
||||
def test_addition(self0):
|
||||
result = 1 + 1
|
||||
self.assertEqual(result, 2, "Addition should be 2") # Specific error message
|
||||
self.assertNotEqual(result, 3)
|
||||
```
|
||||
|
||||
## 4. Setup/Teardown
|
||||
|
||||
Utilize `setUp` and `tearDown` methods within your `unittest.TestCase` classes for per-test setup and cleanup. For class-level setup/teardown, use `setUpClass` and `tearDownClass`. Avoid module-level `setup()`/`teardown()` functions unless maintaining legacy `nose` suites.
|
||||
|
||||
**❌ BAD: Module-level setup/teardown (legacy `nose` style)**
|
||||
```python
|
||||
# test_db_operations.py
|
||||
DB_CONNECTION = None
|
||||
|
||||
def setup_module():
|
||||
global DB_CONNECTION
|
||||
DB_CONNECTION = connect_to_db()
|
||||
|
||||
def teardown_module():
|
||||
DB_CONNECTION.close()
|
||||
|
||||
class DatabaseTest(unittest.TestCase):
|
||||
def test_query(self):
|
||||
# ... use DB_CONNECTION ...
|
||||
pass
|
||||
```
|
||||
|
||||
def test_user_creation(self):
|
||||
self.assertEqual(self.user.username, "testuser")
|
||||
self.assertTrue(self.user.check_password("password"))
|
||||
**✅ GOOD: Class-level `setUp`/`tearDown` for fixtures**
|
||||
```python
|
||||
import unittest
|
||||
from my_module.db import connect_to_db, disconnect_from_db
|
||||
|
||||
def test_invalid_password(self):
|
||||
self.assertFalse(self.user.check_password("wrong_password"))
|
||||
class DatabaseTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
"""Connect to the database once for all tests in this class."""
|
||||
cls.db_connection = connect_to_db()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls) -> None:
|
||||
"""Disconnect from the database after all tests in this class."""
|
||||
disconnect_from_db(cls.db_connection)
|
||||
|
||||
def setUp(self) -> None:
|
||||
"""Reset database state before each test."""
|
||||
self.db_connection.clear_data()
|
||||
|
||||
### 1.4. Component Architecture
|
||||
def test_insert_user(self) -> None:
|
||||
self.db_connection.insert("users", {"name": "Alice"})
|
||||
user = self.db_connection.get("users", "Alice")
|
||||
self.assertIsNotNone(user)
|
||||
|
||||
- **Isolate Components:** Design your application to isolate components, making them easier to test independently. Use dependency injection or other techniques to decouple modules.
|
||||
- **Testable Interfaces:** Define clear and testable interfaces between components. This allows you to mock or stub dependencies during testing.
|
||||
def test_delete_user(self) -> None:
|
||||
self.db_connection.insert("users", {"name": "Bob"})
|
||||
self.db_connection.delete("users", "Bob")
|
||||
user = self.db_connection.get("users", "Bob")
|
||||
self.assertIsNone(user)
|
||||
```
|
||||
|
||||
### 1.5. Code Splitting Strategies
|
||||
## 5. Parameterized Tests
|
||||
|
||||
- **Functional Decomposition:** Split large test functions into smaller, more manageable functions that focus on specific aspects of the test.
|
||||
- **Parameterization:** Use parameterized tests (e.g., with `nose2.tools.params`) to test the same code with different inputs and expected outputs. This avoids code duplication and improves test coverage.
|
||||
Leverage `nose2.tools.params` for writing data-driven tests. This significantly reduces boilerplate when you need to run the same test logic with different input data. Ensure the `nose2.plugins.params` plugin is enabled in `pyproject.toml`.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1. Design Patterns
|
||||
|
||||
- **Arrange-Act-Assert (AAA):** Follow the AAA pattern in each test: _Arrange_ the test environment (set up data, mock dependencies), _Act_ (execute the code under test), and _Assert_ (verify the expected outcome).
|
||||
- **Test Fixtures:** Use `setUp` and `tearDown` methods in `unittest.TestCase` to create and clean up test fixtures (e.g., database connections, temporary files) before and after each test.
|
||||
- **Mocking:** Use mocking libraries like `unittest.mock` or `mock` to replace real dependencies with controlled substitutes during testing. This allows you to isolate the code under test and simulate various scenarios.
|
||||
|
||||
### 2.2. Recommended Approaches
|
||||
|
||||
- **Test-Driven Development (TDD):** Write tests before writing the application code. This helps you define clear requirements and ensure that the code is testable.
|
||||
- **Behavior-Driven Development (BDD):** Use BDD frameworks like `behave` to write tests in a human-readable format that describes the expected behavior of the system.
|
||||
- **Focus on Edge Cases:** Don't only test the happy path. Dedicate tests to verify edge cases, boundary conditions, and error handling.
|
||||
|
||||
### 2.3. Anti-patterns and Code Smells
|
||||
|
||||
- **Over-testing:** Avoid testing implementation details that are likely to change. Focus on testing the behavior and expected outputs of the code.
|
||||
- **Fragile Tests:** Tests that break frequently due to minor code changes are fragile. Strive for tests that are resilient to refactoring and implementation changes.
|
||||
- **Slow Tests:** Slow tests can significantly slow down the development process. Optimize your tests to run quickly. Use techniques like mocking and in-memory databases to speed up testing.
|
||||
- **Ignoring Test Failures:** Always investigate and fix failing tests. Don't ignore them or disable them without a proper reason.
|
||||
- **Redundant Assertions:** Avoid having more than one assertion per test case unless absolutely necessary. Multiple assertions make it hard to pinpoint what failed.
|
||||
|
||||
### 2.4. State Management
|
||||
|
||||
- **Isolate Test State:** Each test should be independent and not rely on the state left by previous tests. Use `setUp` and `tearDown` to ensure a clean state for each test.
|
||||
- **Avoid Global State:** Minimize the use of global variables or shared state in your application, as they can make tests harder to write and maintain.
|
||||
|
||||
### 2.5. Error Handling
|
||||
|
||||
- **Test Exception Handling:** Write tests to verify that your code raises the correct exceptions in error scenarios.
|
||||
- **Use `assertRaises`:** Use `self.assertRaises(ExceptionType, callable, *args, **kwargs)` to assert that a specific exception is raised when calling a function or method.
|
||||
- **Context Managers for Asserting Exceptions:** Use context managers when checking for exceptions in more complex scenarios.
|
||||
|
||||
Example:
|
||||
|
||||
python
|
||||
**❌ BAD: Duplicating test logic for different inputs**
|
||||
```python
|
||||
import unittest
|
||||
|
||||
class MyClass:
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError("Cannot divide by zero")
|
||||
return a / b
|
||||
class CalculatorTest(unittest.TestCase):
|
||||
def test_add_positive(self):
|
||||
self.assertEqual(add(1, 2), 3)
|
||||
def test_add_negative(self):
|
||||
self.assertEqual(add(-1, -2), -3)
|
||||
def test_add_zero(self):
|
||||
self.assertEqual(add(0, 5), 5)
|
||||
```
|
||||
|
||||
class TestMyClass(unittest.TestCase):
|
||||
**✅ GOOD: Using `nose2.tools.params`**
|
||||
```python
|
||||
import unittest
|
||||
from nose2.tools import params
|
||||
|
||||
class CalculatorTest(unittest.TestCase):
|
||||
@params(
|
||||
(1, 2, 3),
|
||||
(-1, -2, -3),
|
||||
(0, 5, 5),
|
||||
(10, -3, 7)
|
||||
)
|
||||
def test_add(self, a: int, b: int, expected: int) -> None:
|
||||
self.assertEqual(add(a, b), expected)
|
||||
```
|
||||
|
||||
## 6. Coverage & CI Integration
|
||||
|
||||
Enable the `coverage` plugin and set `coverage-combine = true` in `pyproject.toml` to ensure accurate coverage reporting, especially in environments using multiprocessing. Run `nose2 -v` in your CI pipeline and enforce a coverage threshold to prevent regressions.
|
||||
|
||||
**Actionable Steps:**
|
||||
1. Install `coverage`: `pip install coverage`
|
||||
2. Configure `pyproject.toml` (as shown in section 2).
|
||||
3. Run tests with coverage in CI: `nose2 -v`
|
||||
4. Use `coverage report` or `coverage html` to view results.
|
||||
5. Enforce a minimum coverage percentage in your CI/CD pipeline.
|
||||
|
||||
## 7. Mocking Strategies
|
||||
|
||||
Use Python's built-in `unittest.mock` library for isolating units under test from their dependencies. Prefer `mock.patch` as a decorator or context manager for clear and scoped mocking.
|
||||
|
||||
**❌ BAD: Manually replacing dependencies or global state**
|
||||
```python
|
||||
# my_module.py
|
||||
import external_service
|
||||
|
||||
def process_data(data):
|
||||
return external_service.send(data)
|
||||
|
||||
# test_my_module.py
|
||||
import unittest
|
||||
import my_module
|
||||
import external_service # This will be the real service
|
||||
|
||||
class MyModuleTest(unittest.TestCase):
|
||||
def test_process_data(self):
|
||||
# This is bad, it affects the actual external_service module
|
||||
original_send = external_service.send
|
||||
external_service.send = lambda x: "mocked_response"
|
||||
result = my_module.process_data("test_data")
|
||||
self.assertEqual(result, "mocked_response")
|
||||
external_service.send = original_send # Must remember to clean up
|
||||
```
|
||||
|
||||
**✅ GOOD: Using `unittest.mock.patch`**
|
||||
```python
|
||||
import unittest
|
||||
from unittest import mock
|
||||
from my_module import process_data # Assuming process_data is in my_module.py
|
||||
|
||||
class MyModuleTest(unittest.TestCase):
|
||||
@mock.patch('my_module.external_service.send') # Patch the dependency where it's looked up
|
||||
def test_process_data_decorator(self, mock_send):
|
||||
mock_send.return_value = "mocked_response_from_decorator"
|
||||
result = process_data("test_data")
|
||||
mock_send.assert_called_once_with("test_data")
|
||||
self.assertEqual(result, "mocked_response_from_decorator")
|
||||
|
||||
def test_process_data_context_manager(self):
|
||||
with mock.patch('my_module.external_service.send') as mock_send:
|
||||
mock_send.return_value = "mocked_response_from_context"
|
||||
result = process_data("another_data")
|
||||
mock_send.assert_called_once_with("another_data")
|
||||
self.assertEqual(result, "mocked_response_from_context")
|
||||
```
|
||||
|
||||
## 8. Type Hints
|
||||
|
||||
Apply type hints consistently to your test code, especially for method signatures (`setUp`, `test_*`, `tearDown`). This improves readability, enables static analysis, and makes your tests easier to understand and maintain.
|
||||
|
||||
**❌ BAD: Untyped test methods**
|
||||
```python
|
||||
class MyTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.my_class = MyClass()
|
||||
self.client = create_test_client()
|
||||
|
||||
def test_divide_valid(self):
|
||||
self.assertEqual(self.my_class.divide(10, 2), 5)
|
||||
def test_endpoint(self):
|
||||
response = self.client.get('/api/data')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
```
|
||||
|
||||
def test_divide_by_zero(self):
|
||||
with self.assertRaises(ValueError) as context:
|
||||
self.my_class.divide(10, 0)
|
||||
self.assertEqual(str(context.exception), "Cannot divide by zero")
|
||||
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1. Optimization Techniques
|
||||
|
||||
- **Profiling:** Use profiling tools to identify performance bottlenecks in your tests. Optimize the slowest tests first.
|
||||
- **Mocking Expensive Operations:** Mock out slow operations (e.g., database queries, network requests) during testing to reduce test execution time.
|
||||
- **Caching Test Data:** Cache frequently used test data to avoid redundant calculations or data retrieval.
|
||||
|
||||
### 3.2. Memory Management
|
||||
|
||||
- **Clean Up Resources:** Ensure that tests properly clean up any resources they allocate (e.g., files, database connections) to avoid memory leaks.
|
||||
- **Limit Test Data Size:** Use realistic but minimal test data to reduce memory consumption during testing.
|
||||
|
||||
### 3.3. Rendering Optimization (If Applicable)
|
||||
|
||||
- This is generally not applicable to `nose2` testing itself, but if testing code involves UI rendering, strategies like reducing render frequency and complexity should be considered.
|
||||
|
||||
### 3.4. Bundle Size Optimization (If Applicable)
|
||||
|
||||
- This is generally not applicable to `nose2` testing itself.
|
||||
|
||||
### 3.5. Lazy Loading
|
||||
|
||||
- **Lazy Initialization of Test Data:** Defer the initialization of test data until it is actually needed by a test case. This can reduce test setup time and memory consumption.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1. Common Vulnerabilities
|
||||
|
||||
- **Insecure Dependencies:** Use dependency scanning tools to identify and address vulnerabilities in third-party libraries used by your application or tests.
|
||||
- **Hardcoded Secrets:** Avoid hardcoding secrets (e.g., passwords, API keys) in your tests. Use environment variables or configuration files to store sensitive information.
|
||||
- **Insufficient Input Validation:** Test that your application properly validates user inputs to prevent injection attacks and other security vulnerabilities.
|
||||
|
||||
### 4.2. Input Validation
|
||||
|
||||
- **Test Boundary Conditions:** Test that your application handles invalid or unexpected inputs gracefully, including boundary conditions, edge cases, and malicious inputs.
|
||||
|
||||
### 4.3. Authentication and Authorization
|
||||
|
||||
- **Test Authentication Flows:** Write tests to verify that your application correctly authenticates users and grants access to authorized resources.
|
||||
- **Role-Based Access Control (RBAC):** If your application uses RBAC, test that users with different roles have the appropriate permissions.
|
||||
|
||||
### 4.4. Data Protection
|
||||
|
||||
- **Test Data Encryption:** If your application encrypts sensitive data, test that the encryption and decryption processes are working correctly.
|
||||
- **Secure Storage:** Test that sensitive data is stored securely (e.g., using hashed passwords, encrypted databases).
|
||||
|
||||
### 4.5. Secure API Communication
|
||||
|
||||
- **Test HTTPS:** If your application communicates with external APIs, test that it uses HTTPS to encrypt the communication.
|
||||
- **API Key Security:** Protect API keys and other credentials used for API communication. Don't store them directly in the code, and use appropriate access controls.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1. Unit Testing
|
||||
|
||||
- **Focus on Individual Units:** Unit tests should focus on testing individual functions, methods, or classes in isolation.
|
||||
- **Mock Dependencies:** Use mocking to isolate the unit under test from its dependencies.
|
||||
- **Test All Code Paths:** Aim for high code coverage by testing all possible code paths and branches.
|
||||
|
||||
### 5.2. Integration Testing
|
||||
|
||||
- **Test Interactions Between Components:** Integration tests verify that different components of the application work together correctly.
|
||||
- **Use Real Dependencies (Where Possible):** Unlike unit tests, integration tests should use real dependencies (e.g., databases, external APIs) where feasible, or integration-specific lightweight replacements.
|
||||
|
||||
### 5.3. End-to-End (E2E) Testing
|
||||
|
||||
- **Test the Entire System:** E2E tests simulate real user scenarios and test the entire application from end to end.
|
||||
- **Use Automated Testing Tools:** Use automated testing tools like Selenium or Cypress to automate E2E tests.
|
||||
|
||||
### 5.4. Test Organization
|
||||
|
||||
- **Test Suites:** Use test suites to group related tests together. This makes it easier to run specific sets of tests.
|
||||
- **Test Runners:** Use test runners (e.g., `nose2`, `pytest`) to discover and execute tests automatically.
|
||||
|
||||
### 5.5. Mocking and Stubbing
|
||||
|
||||
- **`unittest.mock`:** Use the `unittest.mock` library (or the older `mock` library for Python 2) to create mock objects and stub out dependencies.
|
||||
- **`patch` Decorator:** Use the `patch` decorator to replace objects or attributes with mock objects during testing.
|
||||
- **`side_effect`:** Use the `side_effect` attribute of mock objects to simulate different behaviors or raise exceptions when the mock object is called.
|
||||
|
||||
Example:
|
||||
|
||||
python
|
||||
**✅ GOOD: Type-hinted test methods**
|
||||
```python
|
||||
from typing import Any
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
class MyClass:
|
||||
def get_data(self):
|
||||
# This method might make an external API call
|
||||
return "Real data"
|
||||
class MyTest(unittest.TestCase):
|
||||
client: Any # Define attributes used across methods
|
||||
|
||||
def process_data(self):
|
||||
data = self.get_data()
|
||||
return data.upper()
|
||||
def setUp(self) -> None:
|
||||
self.client = create_test_client()
|
||||
|
||||
class TestMyClass(unittest.TestCase):
|
||||
def test_endpoint(self) -> None:
|
||||
response = self.client.get('/api/data')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
```
|
||||
|
||||
@patch('__main__.MyClass.get_data') # Patch the get_data method
|
||||
def test_process_data(self, mock_get_data):
|
||||
# Configure the mock to return a specific value
|
||||
mock_get_data.return_value = "Mock data"
|
||||
## 9. Common Pitfalls & Gotchas
|
||||
|
||||
my_class = MyClass()
|
||||
result = my_class.process_data()
|
||||
|
||||
self.assertEqual(result, "MOCK DATA")
|
||||
mock_get_data.assert_called_once() # Verify that the mock was called
|
||||
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1. Frequent Mistakes
|
||||
|
||||
- **Not Using `setUp` and `tearDown` Correctly:** Failing to properly set up test conditions or clean up resources after each test can lead to inconsistent test results.
|
||||
- **Ignoring Dependencies:** Forgetting to mock out dependencies can make tests slow, unreliable, and prone to external factors.
|
||||
- **Asserting Too Much in One Test:** Trying to test too many things in a single test case makes it difficult to identify the source of failures and maintain the tests.
|
||||
- **Writing Tests That Are Too Specific:** Writing tests that are tightly coupled to the implementation details can make them brittle and difficult to maintain.
|
||||
|
||||
### 6.2. Edge Cases
|
||||
|
||||
- **Null Values:** Test how your application handles null or missing values.
|
||||
- **Empty Collections:** Test how your application handles empty lists, dictionaries, or sets.
|
||||
- **Large Data Sets:** Test how your application performs with large amounts of data.
|
||||
- **Special Characters:** Test how your application handles special characters in user inputs.
|
||||
- **Date and Time Zones:** Test how your application handles different date and time zones.
|
||||
|
||||
### 6.3. Version-Specific Issues
|
||||
|
||||
- **Python Version Compatibility:** nose2 requires Python 3. Ensure compatibility across different minor versions of Python 3.
|
||||
- **Dependency Versions:** Be aware of compatibility issues between nose2 and its dependencies. Specify version constraints in your project's requirements file.
|
||||
|
||||
### 6.4. Compatibility Concerns
|
||||
|
||||
- **`unittest` Compatibility:** nose2 extends `unittest`, so understanding `unittest` is fundamental. Be aware of any differences in behavior between standard `unittest` and nose2.
|
||||
|
||||
### 6.5. Debugging Strategies
|
||||
|
||||
- **Use a Debugger:** Use a debugger (e.g., `pdb`, `ipdb`) to step through your tests and examine the state of the application.
|
||||
- **Print Statements:** Use `print` statements to output debugging information to the console.
|
||||
- **Logging:** Use the `logging` module to record debugging information to a file. This is useful for debugging tests that run in a CI/CD environment.
|
||||
- **Isolate the Problem:** When a test fails, try to isolate the problem by running the test in isolation or by commenting out parts of the test.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1. Recommended Development Tools
|
||||
|
||||
- **IDE:** PyCharm, VS Code with Python extension, or other Python-friendly IDE.
|
||||
- **Text Editor:** Sublime Text, Atom, or other text editor.
|
||||
- **Debugger:** `pdb` (Python Debugger) or `ipdb` (IPython Debugger).
|
||||
- **Mocking Library:** `unittest.mock` (built-in) or `mock` (for older Python versions).
|
||||
|
||||
### 7.2. Build Configuration
|
||||
|
||||
- **`setup.py` or `pyproject.toml`:** Use `setup.py` or `pyproject.toml` to define your project's dependencies and build configuration.
|
||||
- **`requirements.txt`:** Use `requirements.txt` to list the project's dependencies. Generate this file using `pip freeze > requirements.txt`.
|
||||
- **Virtual Environments:** Use virtual environments (e.g., `venv`, `virtualenv`) to isolate the project's dependencies from the system's Python environment.
|
||||
|
||||
### 7.3. Linting and Formatting
|
||||
|
||||
- **Pylint:** Use Pylint to enforce coding standards and detect potential errors in your code. Configure Pylint with a `.pylintrc` file.
|
||||
- **pycodestyle (formerly pep8):** Use pycodestyle to check your code for compliance with PEP 8.
|
||||
- **Flake8:** Use Flake8 as a wrapper around pycodestyle, pyflakes, and McCabe complexity checker.
|
||||
- **Black:** Use Black to automatically format your code according to a consistent style.
|
||||
- **isort:** Use isort to automatically sort your imports.
|
||||
|
||||
### 7.4. Deployment Best Practices
|
||||
|
||||
- This is generally out of scope of `nose2` itself, but automated testing as part of the deployment pipeline ensures high quality code.
|
||||
|
||||
### 7.5. CI/CD Integration
|
||||
|
||||
- **GitHub Actions, GitLab CI, Jenkins:** Integrate nose2 into your CI/CD pipeline to automatically run tests whenever code is pushed or merged.
|
||||
- **Test Reporting:** Configure your CI/CD system to generate test reports and display test results.
|
||||
- **Code Coverage Reporting:** Integrate code coverage tools (e.g., `coverage.py`) into your CI/CD pipeline to track code coverage and identify areas that need more testing.
|
||||
- **Fail Fast:** Configure your CI/CD pipeline to fail immediately if any tests fail. This prevents broken code from being deployed to production.
|
||||
|
||||
By following these best practices and coding standards, you can create robust and reliable Python testing code using nose2.
|
||||
* **Not staying current:** `nose2` is actively maintained. Always use the latest stable release (0.15.x as of May 2024) to benefit from bug fixes, new features, and Python version compatibility.
|
||||
* **Overly complex `setUp`:** Keep `setUp` methods focused on preparing the immediate test context. If setup becomes too complex, consider breaking tests into smaller, more focused classes or using factories.
|
||||
* **Mixing `nose` and `nose2` idioms:** `nose2` is a distinct project. Avoid relying on `nose`-specific features that `nose2` explicitly does not support (e.g., certain module-level functions or specific test discovery quirks).
|
||||
* **When to consider `pytest`:** If `nose2`'s `unittest`-centric approach feels restrictive, or you find yourself fighting its conventions, consider migrating to `pytest`. `pytest` offers a more "Pythonic" test syntax (raw `assert`), powerful fixtures, and a vast plugin ecosystem. `pytest` can run `nose`-style tests, making migration easier.
|
||||
Reference in New Issue
Block a user