63 lines
1.3 KiB
Markdown
63 lines
1.3 KiB
Markdown
# Testing
|
|
|
|
Verify behavior automatically so you can change code with confidence.
|
|
|
|
## Tools by Language
|
|
|
|
| Language | Recommended | Alternatives |
|
|
|---|---|---|
|
|
| Python | `pytest` | unittest, hypothesis |
|
|
| JavaScript/TS | `vitest` | jest, mocha |
|
|
| Go | `testing` (stdlib) | testify |
|
|
| C++ | `googletest` | catch2, doctest |
|
|
|
|
## Python — `pytest`
|
|
|
|
```bash
|
|
pytest # run all tests
|
|
pytest -x # stop on first failure
|
|
pytest -k "test_login" # run matching tests
|
|
pytest --cov=src # with coverage
|
|
```
|
|
|
|
Minimal test:
|
|
|
|
```python
|
|
def add(a: int, b: int) -> int:
|
|
return a + b
|
|
|
|
def test_add():
|
|
assert add(2, 3) == 5
|
|
```
|
|
|
|
Configure in `pyproject.toml`:
|
|
|
|
```toml
|
|
[tool.pytest.ini_options]
|
|
testpaths = ["tests"]
|
|
addopts = "-ra -q"
|
|
```
|
|
|
|
## JavaScript — `vitest`
|
|
|
|
```bash
|
|
vitest run # run once
|
|
vitest # watch mode
|
|
vitest --coverage
|
|
```
|
|
|
|
## Go
|
|
|
|
```bash
|
|
go test ./... # run all tests
|
|
go test -race ./... # with race detector
|
|
go test -cover ./...
|
|
```
|
|
|
|
## Key Principles
|
|
|
|
- Test behavior, not implementation — tests that break on refactors aren't useful
|
|
- Fast tests get run; slow tests get skipped
|
|
- Don't mock more than necessary — integration tests catch what unit tests miss
|
|
- Add a test when you fix a bug; the bug was a missing test
|