{"id":9242,"library":"pytest-black","title":"pytest-black Plugin","description":"pytest-black is a pytest plugin that enables code format checking using the 'black' formatter directly within the pytest test suite. It integrates `black` as a linter, failing tests if code is not formatted according to `black`'s standards. The current version is 0.6.0, with releases typically occurring on an annual to bi-annual cadence.","status":"active","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/coherent-oss/pytest-black","tags":["pytest","testing","formatter","black","linter","code quality"],"install":[{"cmd":"pip install pytest-black","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for formatting checks; pytest-black uses it as an internal dependency.","package":"black","optional":false},{"reason":"pytest-black is a plugin for pytest and requires it to run.","package":"pytest","optional":false}],"imports":[{"note":"pytest-black is a pytest plugin that extends pytest's command-line interface. It does not expose Python symbols (classes, functions) for direct import into user test files or application code. Its functionality is primarily controlled via pytest flags.","symbol":"pytest_black plugin activation","correct":"Install 'pytest-black' and run 'pytest --black'"}],"quickstart":{"code":"import pytest\n\n# Create a test file (e.g., test_example.py)\n# with some unformatted code:\n# def test_hello():\n#    print(   'Hello, World!')\n#    assert True\n\n# To run:\n# 1. Save the above unformatted code in test_example.py\n# 2. Run in terminal: pip install pytest-black black pytest\n# 3. Run in terminal: pytest --black\n\n# Expected output for unformatted code would include black errors.\n# If code is formatted, it would pass without black errors.\n","lang":"python","description":"To quickly start using pytest-black, install the plugin along with black and pytest. Then, simply run pytest with the `--black` flag. pytest-black will check all Python files found by pytest for black compliance. If any files are unformatted, tests will fail."},"warnings":[{"fix":"To fix formatting, manually run `black .` in your project root after `pytest --black` reports issues.","message":"pytest-black checks formatting but does NOT automatically reformat code. It merely reports formatting violations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `pytest --black` to enable formatting checks. Consider adding it to your CI/CD pipeline or as a pre-commit hook.","message":"The plugin is only active when the `--black` flag is explicitly passed to pytest. Running `pytest` without this flag will not perform any black checks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Make sure to include `pip install black` alongside `pytest` and `pytest-black` in your setup, e.g., `pip install pytest pytest-black black`.","message":"Ensure that the `black` package itself is installed in the same environment where `pytest` and `pytest-black` are running. `pytest-black` depends on `black` being available.","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":"Ensure `pytest-black` is installed in the active Python environment: `pip install pytest-black`.","cause":"The pytest-black plugin is not installed or not correctly recognized by pytest.","error":"pytest: error: unrecognized arguments: --black"},{"fix":"Install the 'black' formatter: `pip install black`.","cause":"The 'black' formatter package, which pytest-black relies on, is not installed.","error":"ModuleNotFoundError: No module named 'black'"},{"fix":"Install `blackd` (`pip install blackd`) or explicitly configure pytest-black to use the `black` executable by setting `--black-executable black`.","cause":"pytest-black's default behavior tries to use `blackd` (the black daemon) for faster checks if available, but it's not installed, and the fallback to `black` isn't working as expected.","error":"blackd cannot be used as an executable. Please install blackd to use it."}]}