test_parseopt.py


Overview

`test_parseopt.py` is a comprehensive test suite for the `parseopt` module of the Pytest configuration system (`_pytest.config.argparsing`). The primary purpose of this file is to validate the functionality and robustness of the custom command-line argument parsing utilities used by Pytest. It ensures that the parser correctly handles options, groups, argument types, default values, help formatting, and integration with shell completion mechanisms.

The file contains unit tests for the core classes and methods of the `parseopt` parser, as well as an integration test for the argcomplete bash completion functionality. This test suite helps maintain the quality and reliability of Pytest’s command-line interface parsing, which is critical for user experience and extensibility.


Detailed Documentation

Imports and Setup


Fixtures

parser() -> parseopt.Parser


Test Class: TestParser

This class groups unit tests verifying the behavior of the `parseopt.Parser`, `parseopt.Argument`, and `parseopt.OptionGroup` classes.

1. test_no_help_by_default(self) -> None

2. test_custom_prog(self, parser: parseopt.Parser) -> None

3. test_argument(self) -> None

4. test_argument_type(self) -> None

5. test_argument_processopt(self) -> None

6. test_group_add_and_get(self, parser: parseopt.Parser) -> None

7. test_getgroup_simple(self, parser: parseopt.Parser) -> None

8. test_group_ordering(self, parser: parseopt.Parser) -> None

9. test_group_addoption(self) -> None

10. test_group_addoption_conflict(self) -> None

11. test_group_shortopt_lowercase(self, parser: parseopt.Parser) -> None

12. test_parser_addoption(self, parser: parseopt.Parser) -> None

13. test_parse(self, parser: parseopt.Parser) -> None

14. test_parse2(self, parser: parseopt.Parser) -> None

15. test_parse_from_file(self, parser: parseopt.Parser, tmp_path: Path) -> None

16. test_parse_known_args(self, parser: parseopt.Parser) -> None

17. test_parse_known_and_unknown_args(self, parser: parseopt.Parser) -> None

18. test_parse_will_set_default(self, parser: parseopt.Parser) -> None

19. test_parse_setoption(self, parser: parseopt.Parser) -> None

20. test_parse_special_destination(self, parser: parseopt.Parser) -> None

21. test_parse_split_positional_arguments(self, parser: parseopt.Parser) -> None

22. test_parse_defaultgetter(self) -> None

23. test_drop_short_helper(self) -> None

24. test_drop_short_0(self, parser: parseopt.Parser) -> None

25. test_drop_short_2(self, parser: parseopt.Parser) -> None

26. test_drop_short_3(self, parser: parseopt.Parser) -> None

27. test_drop_short_help0(self, parser: parseopt.Parser) -> None

28. test_drop_short_help1(self, parser: parseopt.Parser) -> None

29. test_multiple_metavar_help(self, parser: parseopt.Parser) -> None


Function: test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None


Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

from _pytest.config import argparsing as parseopt

parser = parseopt.Parser(_ispytest=True)
group = parser.getgroup("mygroup", description="Example group")
group.addoption("--option", action="store_true", help="An example option")

args = parser.parse(["--option"])
print(args.option)  # True

Visual Diagram

classDiagram
    class TestParser {
        +test_no_help_by_default()
        +test_custom_prog(parser)
        +test_argument()
        +test_argument_type()
        +test_argument_processopt()
        +test_group_add_and_get(parser)
        +test_getgroup_simple(parser)
        +test_group_ordering(parser)
        +test_group_addoption()
        +test_group_addoption_conflict()
        +test_group_shortopt_lowercase(parser)
        +test_parser_addoption(parser)
        +test_parse(parser)
        +test_parse2(parser)
        +test_parse_from_file(parser, tmp_path)
        +test_parse_known_args(parser)
        +test_parse_known_and_unknown_args(parser)
        +test_parse_will_set_default(parser)
        +test_parse_setoption(parser)
        +test_parse_special_destination(parser)
        +test_parse_split_positional_arguments(parser)
        +test_parse_defaultgetter()
        +test_drop_short_helper()
        +test_drop_short_0(parser)
        +test_drop_short_2(parser)
        +test_drop_short_3(parser)
        +test_drop_short_help0(parser)
        +test_drop_short_help1(parser)
        +test_multiple_metavar_help(parser)
    }

    class parseopt.Parser {
        +getgroup(name, description=None, after=None)
        +addoption(*args, **kwargs)
        +parse(args)
        +parse_known_args(args)
        +parse_known_and_unknown_args(args)
        +parse_setoption(args, namespace)
    }

    class parseopt.Argument {
        +__init__(*opts, **kwargs)
        +attrs()
    }

    class parseopt.OptionGroup {
        +__init__(name, _ispytest)
        +addoption(*opts, **kwargs)
        +_addoption(*opts, **kwargs)
    }

    TestParser --> parseopt.Parser : uses
    TestParser --> parseopt.Argument : tests
    TestParser --> parseopt.OptionGroup : tests

Summary

`test_parseopt.py` is a detailed, well-structured test module focused on verifying the custom Pytest argument parser and its components. It covers a wide range of scenarios from basic parsing to advanced features like argument files, default value processing, option groups, and shell completion. This test suite is a critical part of Pytest’s development, ensuring that command-line options behave as expected and that users receive helpful feedback and completion support.


End of Documentation for test_parseopt.py