pytest-dev / pytest-dev/pytest

Allow plugins to cause a "command line usage error"

Open
#11,940 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

What's the problem this feature will solve?

I am trying to write a pytest plugin that has several command line flags. Some combinations of these flags are "illegal", for example in the sense that they contradict each other. However, I noticed that there seems to be no way to terminate the program with the standard "usage error" output as for example seen from argparse.

Describe the solution you'd like

There should be a method, for example named cmdline_error, for example at the Config class, that causes the usage error message "ERROR: usage: pytest [options] ..." to appear, followed by the termination of the program with the appropriate exit code.

Alternative Solutions

Currently, the way I achieve this is by accessing the error function of the underlying argparse parser via config._parser._getparser().error in the pytest_configure hook. But of course, accessing names starting with underscores is not generally a good idea.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Config class and the pytest_configure hook, then trace how config._parser._getparser().error currently produces argparse-style usage errors. Compare that path with pytest's plugin-facing APIs and define the public behavior requested: standard usage output followed by the appropriate exit code when a plugin reports an invalid flag combination.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.