{"id":9249,"library":"pytest-ruff","title":"pytest-ruff","description":"pytest-ruff is a pytest plugin designed to integrate Ruff's powerful linting and formatting checks directly into your test suite. It automatically runs `ruff format --check` and `ruff check` as part of your `pytest` execution, failing tests if any Ruff requirements are not met. The current version is 0.5, and it maintains a consistent release cadence with updates addressing bug fixes and new features.","status":"active","version":"0.5","language":"en","source_language":"en","source_url":"https://github.com/businho/pytest-ruff","tags":["pytest","ruff","linting","formatting","testing","plugin"],"install":[{"cmd":"pip install pytest-ruff","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required as pytest-ruff is a plugin for the pytest testing framework.","package":"pytest"},{"reason":"Required as pytest-ruff wraps and runs the Ruff linter and formatter.","package":"ruff"}],"imports":[{"note":"Users interact with pytest-ruff primarily through pytest CLI flags like `--ruff` and `--ruff-format`, not via Python import statements.","symbol":"pytest-ruff","correct":"This is a pytest plugin and does not typically require direct Python imports in user code. Its functionality is enabled via pytest command-line arguments."}],"quickstart":{"code":"# Save as pyproject.toml\n[tool.ruff]\n# By default, Ruff enables 'E' (pycodestyle errors) and 'F' (Pyflakes rules).\n# Ensure these are active or extend with your desired rules.\nselect = [\"E\", \"F\"]\n\n# Save as test_example.py\ndef test_linting_violation():\n    x = 1; y = 2  # E702: Multiple statements on one line (will be flagged by Ruff)\n    assert x < y\n\n# Run from your terminal in the same directory:\n# pytest --ruff\n# Expected output will show a failure due to the E702 Ruff violation.","lang":"python","description":"To quickly verify `pytest-ruff` is working, create a `pyproject.toml` to configure Ruff and a `test_example.py` file with a deliberate Ruff violation. Then, run pytest with the `--ruff` flag. The test suite should report a failure originating from the Ruff check."},"warnings":[{"fix":"Update your pytest command or configuration to use `--output-format` instead of `--show-source`. For example, `pytest --ruff --output-format=full`.","message":"The `--show-source` CLI argument for controlling source code snippets in Ruff's output was replaced by `--output-format` in `pytest-ruff v0.4.0`. Using the old flag will result in an 'unrecognized arguments' error.","severity":"breaking","affected_versions":">=0.4.0"},{"fix":"Upgrade to `pytest-ruff v0.2.1` or newer to ensure `--ruff` and `--ruff-format` operate as distinct checks as intended.","message":"In versions prior to `v0.2.1`, the `--ruff` and `--ruff-format` flags might not have worked independently or as expected, potentially leading to inconsistent behavior.","severity":"gotcha","affected_versions":"<0.2.1"},{"fix":"Be aware that `-k` might not skip Ruff checks. If this is a significant performance bottleneck, consider running Ruff separately via `ruff check .` and `ruff format --check .` outside of your `pytest` invocation for focused test runs.","message":"Tests generated by `pytest-ruff` (and similar quality assurance plugins) might not always respect pytest's `-k` expression for selecting a subset of tests. This means Ruff checks could run even when you're trying to target specific functional tests, potentially slowing down development in large projects.","severity":"gotcha","affected_versions":"All versions"},{"fix":"You can add `# noqa: F401` to the line importing the fixture if you're sure it's being used implicitly by pytest. Alternatively, consider using `conftest.py` for shared fixtures or `pytest.mark.usefixtures()` for fixtures whose return value isn't directly consumed to make the dependency explicit and potentially avoid the `F401` warning.","message":"Ruff's `F401` (unused import) rule can sometimes incorrectly flag imported pytest fixtures as unused, especially if the fixtures are imported but not directly called in the test function body (e.g., when relying on implicit fixture injection).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Replace `--show-source` with `--output-format=full` (or `concise`) in your pytest command or configuration.","cause":"The `--show-source` CLI argument was removed in `pytest-ruff v0.4.0` in favor of `--output-format`.","error":"pytest: error: unrecognized arguments: --show-source"},{"fix":"If the fixture is correctly used by pytest, add `# noqa: F401` to the import line. For better clarity and to avoid the warning, consider defining fixtures in `conftest.py` or using `@pytest.mark.usefixtures('your_fixture_name')` on your test function.","cause":"Ruff's unused import checker (F401) is flagging a pytest fixture that is imported but not directly referenced in the test body. This can happen with implicitly used fixtures.","error":"F401 'your_fixture_name' imported but unused"},{"fix":"Review your Ruff configuration file for any syntax errors or invalid settings. Refer to the official Ruff documentation for valid configuration options and structure. The error message should provide more details on the specific parsing failure.","cause":"pytest-ruff encountered an issue parsing your Ruff configuration (e.g., in `pyproject.toml` or `ruff.toml`). This could be due to syntax errors or invalid options in the configuration file.","error":"RuffConfigError: Failed to load Ruff configuration from [...]"}]}