# pytest > Generate pytest tests matching Stresscon project conventions. Reads existing test files to learn patterns, creates unit and integration tests with typed fixtures, pd.DataFrame construction, direct value assertions, and proper imports from the stresscon package. Use when adding or updating Python tests. - Author: BAM - Repository: bamussel23/claude-skills - Version: 20260207211010 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-08 - Source: https://github.com/bamussel23/claude-skills - Web: https://mule.run/skillshub/@@bamussel23/claude-skills~pytest:20260207211010 --- --- name: pytest description: > Generate pytest tests matching Stresscon project conventions. Reads existing test files to learn patterns, creates unit and integration tests with typed fixtures, pd.DataFrame construction, direct value assertions, and proper imports from the stresscon package. Use when adding or updating Python tests. --- # Smart Test Generator You generate pytest tests that match the exact conventions used in the Stresscon Operations Suite Python codebase. ## Before generating tests 1. Read the source module being tested to understand its public API 2. Read existing test files in `python/tests/` to confirm current conventions 3. Read the reference file `references/conventions.md` for the full pattern guide ## Test file conventions ### File structure ```python """Tests for the module.""" import pandas as pd import pytest from stresscon. import ( function_one, function_two, ) ``` - Module docstring describing what is being tested - Standard library imports first, then third-party, then local - Import specific functions from `stresscon.*`, not the module itself - No `if __name__ == "__main__"` blocks ### Fixtures - Use `@pytest.fixture` with return type annotations - Include a docstring explaining the fixture's purpose - Build DataFrames using `pd.DataFrame({...})` with dictionary constructor - Use `pd.to_datetime()` for datetime columns - Add inline comments noting expected calculated values ```python @pytest.fixture def sample_df() -> pd.DataFrame: """Create a small fixture DataFrame for testing.""" return pd.DataFrame( { "ColumnA": ["val1", "val2"], "StartTime": pd.to_datetime(["2025-06-01 08:00", "2025-06-15 14:00"]), "EndTime": pd.to_datetime([ "2025-06-01 12:00", # 240 min "2025-06-15 15:30", # 90 min ]), } ) ``` ### Test function naming - Pattern: `test__` - Examples: - `test_cost_of_idleness_basic` - `test_cost_of_idleness_zero_downtime` - `test_cost_of_idleness_fractional` - `test_department_cost_report_missing_column` - `test_load_logs_from_dataframe` ### Assertion patterns Use direct value assertions — no assertion messages, no `assertEqual`: ```python # Column existence assert "ColumnName" in result.columns # Exact value checks assert result["column"].iloc[0] == 240.0 assert result["key"] == expected_value # Length checks assert len(result) == 5 # DataFrame filtering + value extraction mech = result[result["IssueCategory"] == "Mechanical"] assert mech["TotalDowntimeMinutes"].values[0] == 390.0 assert mech["Count"].values[0] == 3 # Type checking assert pd.api.types.is_datetime64_any_dtype(result["DowntimeStart"]) # Exception testing with pytest.raises(ValueError, match="DowntimeMinutes"): function_that_should_fail(bad_input) ``` ### Type annotations - Fixture parameters in test functions use type hints: `(sample_df: pd.DataFrame)` - Test functions return `-> None` ```python def test_calculate_downtime_minutes(sample_df: pd.DataFrame) -> None: result = calculate_downtime_minutes(sample_df) assert "DowntimeMinutes" in result.columns assert result["DowntimeMinutes"].iloc[0] == 240.0 ``` ### What NOT to do - Do NOT use `unittest.TestCase` or class-based tests - Do NOT use `assert x == y, "message"` with assertion messages - Do NOT use `mock.patch` unless testing SharePoint client interactions - Do NOT create temporary files — use DataFrame fixtures - Do NOT use `parametrize` unless there are 4+ similar test cases - Do NOT add `conftest.py` unless sharing fixtures across multiple test files ## Generating tests When asked to generate tests for a module: 1. Read the module source to identify all public functions 2. For each function, create tests covering: - **Happy path** — typical inputs produce expected outputs - **Edge cases** — zero values, empty DataFrames, boundary conditions - **Error cases** — invalid inputs that should raise exceptions 3. Build realistic fixture data using Stresscon domain values: - AssetIDs: `CRN-01`, `MIX-01`, `FORM-01`, `SAW-01`, `BATCH-01` - IssueCategories: `Mechanical`, `Electrical`, `Hydraulic`, `Pneumatic` - SeverityLevels: `Critical (Line Down)`, `High (Needs Repair)`, `Routine (Scheduled)` - Equipment names: `Overhead Crane #1`, `Batch Mixer Primary`, `Double Tee Form A` 4. Place the test file at `python/tests/test_.py` ## Running tests ```bash cd /Users/blakemusselman/claude/agent && python -m pytest python/tests/ -v ```