GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — codex-skill-pytest/SKILL.md at main · allthingssecurity/codex-skill-pytest

Expand file tree

/

SKILL.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
109 lines (84 loc) · 3.2 KB
/

SKILL.md

Copy path

File metadata and controls

109 lines (84 loc) · 3.2 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions
name pytest-testing
description Create and maintain tests with pytest: fixtures, parametrization, mocking (monkeypatch/pytest-mock), tmp_path, async, CLI/web apps, coverage and CI. Use this when adding tests to an existing project, refactoring a test suite, or debugging flaky/failing tests.

Pytest Testing Skill

Use this skill to quickly stand up a robust pytest setup and to write clear, maintainable tests. It includes a project initializer and copy‑paste recipes for common patterns.

Quick Start

  • Initialize pytest in your project directory:
    • python3 skill-pytest/init_pytest.py --ci
    • Creates tests/, pytest.ini, conftest.py, .coveragerc, requirements-dev.txt, and a GitHub Actions CI workflow (optional with --ci).
  • Run tests:
    • python3 -m pip install -r requirements-dev.txt
    • pytest -q or pytest -q --cov

Recommended Layout

project/
├─ src/yourpkg/          # or a flat module layout
├─ tests/
│  ├─ test_sample.py
│  └─ conftest.py        # shared fixtures/config
├─ pytest.ini            # pytest config
├─ .coveragerc           # coverage config
└─ requirements-dev.txt  # dev deps

Core Patterns

  • Fixtures: share setup/teardown logic; prefer function scope by default.
  • Parametrization: compactly test multiple input/output cases.
  • Isolation: use tmp_path and monkeypatch to avoid side effects.
  • Async: use pytest-asyncio and mark async tests with @pytest.mark.asyncio.
  • Marks: declare unit, integration, slow, flaky to filter/select.
  • Coverage: measure with pytest-cov, set a fail_under threshold.

Snippets

Parametrized test:

import pytest

@pytest.mark.parametrize(
    "a,b,expected",
    [
        (2, 2, 4),
        (2, -2, 0),
        (0, 0, 0),
    ],
)
def test_add(a, b, expected):
    assert a + b == expected

Using tmp_path and monkeypatch:

def test_writes_file(tmp_path, monkeypatch):
    out = tmp_path / "data.txt"
    monkeypatch.setenv("APP_MODE", "TEST")
    out.write_text("hello")
    assert out.read_text() == "hello"

Async test:

import asyncio
import pytest

@pytest.mark.asyncio
async def test_async_step():
    await asyncio.sleep(0)
    assert True

Mock with pytest-mock:

def test_external_call_is_mocked(mocker):
    mock = mocker.patch("yourpkg.api.fetch", return_value={"ok": True})
    from yourpkg.service import run
    assert run() is True
    mock.assert_called_once()

Useful CLI Flags

  • -q quiet output; -vv more detail
  • -k expr select by substring: -k "not slow and login"
  • -m mark select by mark: -m integration
  • --maxfail=1 -x fail fast
  • --pdb drop into debugger on failure
  • --cov=yourpkg --cov-report=term-missing coverage

CI Example (GitHub Actions)

The initializer can add .github/workflows/python-tests.yml that installs dev deps and runs pytest --cov on push/PR.

Troubleshooting

  • Tests not discovered: ensure files match test_*.py and are under tests/ (or set testpaths in pytest.ini).
  • Import errors: use src/ layout and pip install -e . or set PYTHONPATH in CI.
  • Flaky tests: use explicit timeouts, seed RNGs, and reduce implicit waits in async/HTTP tests.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.