Debian Codegraph @

tests/README.md

raw symbols SCIP
1 # Cyclopts Test Suite
2
3 This directory contains the test suite for cyclopts, including unit tests, integration tests, and snapshot tests.
4
5 ## Directory Structure
6
7 ```
8 tests/
9 ├── __snapshots__/ # Snapshot files (auto-generated by syrupy)
10 │ └── test_docs_snapshots/ # Individual .md and .rst snapshot files
11 ├── apps/ # Test applications
12 │ ├── complex-demo/ # Comprehensive CLI for doc testing
13 │ └── cyclopts-demo/ # Simple demo app
14 ├── cli/ # CLI-specific tests
15 ├── completion/ # Shell completion tests
16 ├── config/ # Configuration file tests
17 ├── py312/ # Python 3.12+ specific tests
18 ├── types/ # Type handling tests
19 ├── validators/ # Validator tests
20 ├── conftest.py # Shared fixtures
21 ├── test_docs_e2e.py # End-to-end documentation build tests
22 ├── test_docs_snapshots.py # Snapshot tests for doc generation
23 ├── test_mkdocs_ext.py # MkDocs plugin tests
24 ├── test_sphinx_ext.py # Sphinx extension tests
25 └── ... (other test files)
26 ```
27
28 ## Running Tests
29
30 ```bash
31 # Run all tests
32 uv run pytest
33
34 # Run with coverage
35 uv run pytest --cov=cyclopts --cov-report=term
36
37 # Run specific test file
38 uv run pytest tests/test_mkdocs_ext.py -v
39
40 # Run tests matching a pattern
41 uv run pytest -k "mkdocs" -v
42 ```
43
44 ## Documentation Plugin Tests
45
46 The documentation plugins (MkDocs and Sphinx) have three levels of testing:
47
48 ### 1. Unit Tests (`test_mkdocs_ext.py`, `test_sphinx_ext.py`)
49
50 Test individual components like directive parsing, import mechanisms, and filtering logic.
51
52 ```bash
53 uv run pytest tests/test_mkdocs_ext.py tests/test_sphinx_ext.py -v
54 ```
55
56 ### 2. Snapshot Tests (`test_docs_snapshots.py`)
57
58 Capture the exact markdown/RST output generated by the plugins to detect unintended changes.
59
60 ```bash
61 # Run snapshot tests
62 uv run pytest tests/test_docs_snapshots.py -v
63
64 # Update snapshots after intentional changes
65 uv run pytest tests/test_docs_snapshots.py --snapshot-update
66 ```
67
68 **What's snapshotted:**
69 - Generated markdown documentation (various filtering/nesting scenarios)
70 - Generated RST documentation
71 - MkDocs directive processing output
72 - Sphinx directive processing output
73
74 **When to update snapshots:**
75 - After intentionally changing documentation output format
76 - After adding new features that affect output
77 - After fixing bugs that change output
78
79 **Reviewing snapshot changes:**
80 ```bash
81 # See what changed (snapshots are individual .md/.rst files)
82 git diff tests/__snapshots__/
83
84 # Preview a snapshot directly
85 open tests/__snapshots__/test_docs_snapshots/TestMarkdownSnapshots.test_full_app_markdown.md
86
87 # Accept changes
88 uv run pytest tests/test_docs_snapshots.py --snapshot-update
89 git add tests/__snapshots__/
90 ```
91
92 ### 3. End-to-End Build Tests (`test_docs_e2e.py`)
93
94 Actually run `mkdocs build` and `sphinx-build` to verify the full documentation pipeline works.
95
96 ```bash
97 uv run pytest tests/test_docs_e2e.py -v
98 ```
99
100 These tests:
101 - Build documentation using the complex-demo app
102 - Verify the output HTML contains expected content
103 - Test that nested commands, dataclass flattening, etc. work in real builds
104
105 ## Test Applications
106
107 ### `apps/complex-demo/`
108
109 A comprehensive CLI application designed to test all edge cases:
110
111 - **Dataclass parameter flattening** - `@Parameter(name="*")`
112 - **Nested dataclasses** - Dataclass containing other dataclasses
113 - **Pydantic models** - BaseModel parameters (if pydantic installed)
114 - **attrs classes** - @attrs.define parameters (if attrs installed)
115 - **4-level nested commands** - `admin → users → permissions → roles`
116 - **Custom groups** - `Group.create_ordered()`
117 - **Complex union types** - `int | Literal["auto"]`
118 - **Count parameters** - `-v`, `-vv`, `-vvv`
119 - **Validators** - Number, Path validators
120 - **Hidden commands/parameters**
121 - **Multiple docstring formats** - NumPy, Google, Sphinx
122
123 See `apps/complex-demo/README.md` for full details.
124
125 ### `apps/cyclopts-demo/`
126
127 A simpler demo app used for basic MkDocs plugin testing.
128
129 ## Writing New Tests
130
131 ### Adding Snapshot Tests
132
133 1. Add a new test method in `test_docs_snapshots.py`
134 2. Use the `snapshot` fixture from syrupy
135 3. Assert output equals snapshot: `assert output == snapshot`
136 4. Run with `--snapshot-update` to generate initial snapshot
137
138 ```python
139 def test_my_new_feature(self, ensure_complex_demo_importable, snapshot):
140 """Snapshot test for my new feature."""
141 from complex_app import app
142 from cyclopts.docs.markdown import generate_markdown_docs
143
144 markdown = generate_markdown_docs(app, my_new_option=True)
145 assert markdown == snapshot
146 ```
147
148 ### Adding E2E Tests
149
150 1. Add test methods to `test_docs_e2e.py`
151 2. Use the build fixtures to run mkdocs/sphinx
152 3. Verify output files contain expected content
153
154 ### Adding to complex-demo
155
156 If you need to test a new cyclopts feature:
157
158 1. Add the feature usage to `apps/complex-demo/complex_app.py`
159 2. Add documentation directives to `apps/complex-demo/mkdocs_docs/` and `apps/complex-demo/docs/source/`
160 3. Add snapshot tests for the new output
161 4. Add E2E assertions if needed
162
163 ## Test Fixtures
164
165 Key fixtures from `conftest.py`:
166
167 - `app` - Basic cyclopts App instance
168 - `console` - Rich console for output capture
169 - `assert_parse_args` - Helper to verify argument parsing
170 - `chdir_to_tmp_path` - Auto-fixture that runs tests in temp directory
171
172 ## Continuous Integration
173
174 Tests run on multiple Python versions (3.10-3.14) and operating systems (Ubuntu, macOS, Windows). Some tests are skipped on certain configurations:
175
176 - `py312/` tests require Python 3.12+
177 - Pydantic/attrs tests gracefully degrade if not installed
178 - MkDocs/Sphinx build tests skip if tools not available