-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add support for native TOML configuration #13846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
18fbe17
doc/reference: add a couple of missing confval entries
bluetech cc400d1
config: consider empty .pytest.ini as config file
bluetech 147467d
config: generalize from INI format
bluetech 7672daf
config: add native TOML configuration
bluetech 8ab2500
pyproject.toml: convert pytest's own config to use `[tool.pytest]`
bluetech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| Added support for native TOML configuration files. | ||
|
|
||
| While pytest, since version 6, supports configuration in ``pyproject.toml`` files under ``[tool.pytest.ini_options]``, | ||
| it does so in an "INI compatibility mode", where all configuration values are treated as strings or list of strings. | ||
| Now, pytest supports the native TOML data model. | ||
|
|
||
| In ``pyproject.toml``, the native TOML configuration is under the ``[tool.pytest]`` table. | ||
|
|
||
| .. code-block:: toml | ||
| # pyproject.toml | ||
| [tool.pytest] | ||
| minversion = "9.0" | ||
| addopts = ["-ra", "-q"] | ||
| testpaths = [ | ||
| "tests", | ||
| "integration", | ||
| ] | ||
| The ``[tool.pytest.ini_options]`` table remains supported, but both tables cannot be used at the same time. | ||
|
|
||
| If you prefer to use a separate configuration file, or don't use ``pyproject.toml``, you can use ``pytest.toml`` or ``.pytest.toml``: | ||
|
|
||
| .. code-block:: toml | ||
| # pytest.toml or .pytest.toml | ||
| [pytest] | ||
| minversion = "9.0" | ||
| addopts = ["-ra", "-q"] | ||
| testpaths = [ | ||
| "tests", | ||
| "integration", | ||
| ] | ||
| The documentation now shows configuration snippets in both TOML and INI formats, in a tabbed interface. | ||
|
|
||
| See :ref:`config file formats` for full details. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Hidden ``.pytest.ini`` files are now picked up as the config file even if empty. | ||
| This was an inconsistency with non-hidden ``pytest.ini``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -239,13 +239,21 @@ Registering markers | |
|
|
||
| Registering markers for your test suite is simple: | ||
|
|
||
| .. code-block:: ini | ||
| .. tab:: toml | ||
|
|
||
| # content of pytest.ini | ||
| [pytest] | ||
| markers = | ||
| webtest: mark a test as a webtest. | ||
| slow: mark test as slow. | ||
| .. code-block:: toml | ||
|
|
||
| [pytest] | ||
| markers = ["webtest: mark a test as a webtest.", "slow: mark test as slow."] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a example of dissonance in toml this should be |
||
|
|
||
| .. tab:: ini | ||
|
|
||
| .. code-block:: ini | ||
|
|
||
| [pytest] | ||
| markers = | ||
| webtest: mark a test as a webtest. | ||
| slow: mark test as slow. | ||
|
|
||
| Multiple custom markers can be registered, by defining each one in its own line, as shown in above example. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,13 +88,21 @@ Example: | |
| Changing directory recursion | ||
| ----------------------------------------------------- | ||
|
|
||
| You can set the :confval:`norecursedirs` option in an ini-file, for example your ``pytest.ini`` in the project root directory: | ||
| You can set the :confval:`norecursedirs` option in a configuration file: | ||
|
|
||
| .. code-block:: ini | ||
| .. tab:: toml | ||
|
|
||
| # content of pytest.ini | ||
| [pytest] | ||
| norecursedirs = .svn _build tmp* | ||
| .. code-block:: toml | ||
|
|
||
| [pytest] | ||
| norecursedirs = [".svn", "_build", "tmp*"] | ||
|
|
||
| .. tab:: ini | ||
|
|
||
| .. code-block:: ini | ||
|
|
||
| [pytest] | ||
| norecursedirs = .svn _build tmp* | ||
|
|
||
| This would tell ``pytest`` to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory. | ||
|
|
||
|
|
@@ -108,14 +116,25 @@ the :confval:`python_files`, :confval:`python_classes` and | |
| :confval:`python_functions` in your :ref:`configuration file <config file formats>`. | ||
| Here is an example: | ||
|
|
||
| .. code-block:: ini | ||
| .. tab:: toml | ||
|
|
||
| .. code-block:: toml | ||
|
|
||
| # content of pytest.ini | ||
| # Example 1: have pytest look for "check" instead of "test" | ||
| [pytest] | ||
| python_files = check_*.py | ||
| python_classes = Check | ||
| python_functions = *_check | ||
| # Example 1: have pytest look for "check" instead of "test" | ||
| [pytest] | ||
| python_files = ["check_*.py"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in future we might want to have |
||
| python_classes = ["Check"] | ||
| python_functions = ["*_check"] | ||
|
|
||
| .. tab:: ini | ||
|
|
||
| .. code-block:: ini | ||
|
|
||
| # Example 1: have pytest look for "check" instead of "test" | ||
| [pytest] | ||
| python_files = check_*.py | ||
| python_classes = Check | ||
| python_functions = *_check | ||
|
|
||
| This would make ``pytest`` look for tests in files that match the ``check_* | ||
| .py`` glob-pattern, ``Check`` prefixes in classes, and functions and methods | ||
|
|
@@ -152,12 +171,21 @@ The test collection would look like this: | |
|
|
||
| You can check for multiple glob patterns by adding a space between the patterns: | ||
|
|
||
| .. code-block:: ini | ||
| .. tab:: toml | ||
|
|
||
| .. code-block:: toml | ||
|
|
||
| # Example 2: have pytest look for files with "test" and "example" | ||
| [pytest] | ||
| python_files = ["test_*.py", "example_*.py"] | ||
|
|
||
| # Example 2: have pytest look for files with "test" and "example" | ||
| # content of pytest.ini | ||
| [pytest] | ||
| python_files = test_*.py example_*.py | ||
| .. tab:: ini | ||
|
|
||
| .. code-block:: ini | ||
|
|
||
| # Example 2: have pytest look for files with "test" and "example" | ||
| [pytest] | ||
| python_files = test_*.py example_*.py | ||
|
|
||
| .. note:: | ||
|
|
||
|
|
@@ -178,14 +206,22 @@ example if you have unittest2 installed you can type: | |
| pytest --pyargs unittest2.test.test_skipping -q | ||
|
|
||
| which would run the respective test module. Like with | ||
| other options, through an ini-file and the :confval:`addopts` option you | ||
| other options, through a configuration file and the :confval:`addopts` option you | ||
| can make this change more permanently: | ||
|
|
||
| .. code-block:: ini | ||
| .. tab:: toml | ||
|
|
||
| .. code-block:: toml | ||
|
|
||
| [pytest] | ||
| addopts = ["--pyargs"] | ||
|
|
||
| .. tab:: ini | ||
|
|
||
| # content of pytest.ini | ||
| [pytest] | ||
| addopts = --pyargs | ||
| .. code-block:: ini | ||
|
|
||
| [pytest] | ||
| addopts = --pyargs | ||
|
|
||
| Now a simple invocation of ``pytest NAME`` will check | ||
| if NAME exists as an importable package/module and otherwise | ||
|
|
@@ -224,11 +260,19 @@ Customizing test collection | |
|
|
||
| You can easily instruct ``pytest`` to discover tests from every Python file: | ||
|
|
||
| .. code-block:: ini | ||
| .. tab:: toml | ||
|
|
||
| .. code-block:: toml | ||
|
|
||
| [pytest] | ||
| python_files = ["*.py"] | ||
|
|
||
| .. tab:: ini | ||
|
|
||
| .. code-block:: ini | ||
|
|
||
| # content of pytest.ini | ||
| [pytest] | ||
| python_files = *.py | ||
| [pytest] | ||
| python_files = *.py | ||
|
|
||
| However, many projects will have a ``setup.py`` which they don't want to be | ||
| imported. Moreover, there may files only importable by a specific python | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish I'd seen this earlier — I tend to prefer keeping the same section names in other TOML files due to the ease of migration between them: coveragepy/coveragepy#1952 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree and think that would present a markedly worse UX coveragepy/coveragepy#1952 (comment)