Skip to content
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

[WIP] Add support for toml config #3686

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1556.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for configs in ``pyproject.toml`` files.
22 changes: 12 additions & 10 deletions doc/en/customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ Here is the algorithm which finds the rootdir from ``args``:
recognised as paths that exist in the file system. If no such paths are
found, the common ancestor directory is set to the current working directory.

- look for ``pytest.ini``, ``tox.ini`` and ``setup.cfg`` files in the ancestor
directory and upwards. If one is matched, it becomes the ini-file and its
directory becomes the rootdir.
- look for ``pytest.ini``, ``tox.ini``, ``setup.cfg`` and ``pyproject.toml``
files in the ancestor directory and upwards. If one is matched, it becomes
the ini-file and its directory becomes the rootdir.

- if no ini-file was found, look for ``setup.py`` upwards from the common
ancestor directory to determine the ``rootdir``.

- if no ``setup.py`` was found, look for ``pytest.ini``, ``tox.ini`` and
``setup.cfg`` in each of the specified ``args`` and upwards. If one is
- if no ``setup.py`` was found, look for ``pytest.ini``, ``tox.ini``, ``setup.cfg``
and ``pyproject.toml`` in each of the specified ``args`` and upwards. If one is
matched, it becomes the ini-file and its directory becomes the rootdir.

- if no ini-file was found, use the already determined common ancestor as root
Expand All @@ -77,10 +77,11 @@ directory and also starts determining the rootdir from there.
possible.

Note that an existing ``pytest.ini`` file will always be considered a match,
whereas ``tox.ini`` and ``setup.cfg`` will only match if they contain a
``[pytest]`` or ``[tool:pytest]`` section, respectively. Options from multiple ini-files candidates are never
merged - the first one wins (``pytest.ini`` always wins, even if it does not
contain a ``[pytest]`` section).
whereas ``tox.ini``, ``setup.cfg`` and ``pyproject.toml`` will only match if
they contain a ``[pytest]``, ``[tool:pytest]`` or ``[tool.pytest]`` section,
respectively. Options from multiple ini-files candidates are never merged -
the first one wins (``pytest.ini`` always wins, even if it does not contain
a ``[pytest]`` section).

The ``config`` object will subsequently carry these attributes:

Expand All @@ -101,6 +102,7 @@ check for ini-files as follows::

# first look for pytest.ini files
path/pytest.ini
path/pyproject.toml # must also contain [tool.pytest] section to match
path/setup.cfg # must also contain [tool:pytest] section to match
path/tox.ini # must also contain [pytest] section to match
pytest.ini
Expand Down Expand Up @@ -128,7 +130,7 @@ progress output, you can write it into a configuration file:
.. code-block:: ini

# content of pytest.ini
# (or tox.ini or setup.cfg)
# (or tox.ini or setup.cfg or pyproject.toml)
[pytest]
addopts = -ra -q

Expand Down
4 changes: 2 additions & 2 deletions doc/en/example/pythoncollection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ the :confval:`python_files`, :confval:`python_classes` and
:confval:`python_functions` configuration options. Example::

# content of pytest.ini
# can also be defined in tox.ini or setup.cfg file, although the section
# name in setup.cfg files should be "tool:pytest"
# can also be defined in tox.ini, setup.cfg or pyproject.toml file, although the section
# name in setup.cfg files should be "tool:pytest" or "tool.pytest" in pyproject.toml files
[pytest]
python_files=check_*.py
python_classes=Check
Expand Down
8 changes: 4 additions & 4 deletions doc/en/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -884,9 +884,9 @@ processes can inspect it, see :ref:`pytest current test env` for more informatio
Configuration Options
---------------------

Here is a list of builtin configuration options that may be written in a ``pytest.ini``, ``tox.ini`` or ``setup.cfg``
file, usually located at the root of your repository. All options must be under a ``[pytest]`` section
(``[tool:pytest]`` for ``setup.cfg`` files).
Here is a list of builtin configuration options that may be written in a ``pytest.ini``, ``tox.ini``, ``setup.cfg``
or ``pyproject.toml`` file, usually located at the root of your repository. All options must be under a ``[pytest]``
section (``[tool:pytest]`` for ``setup.cfg`` or ``[tool.pytest]`` for ``pyproject.toml`` files).

Configuration file options may be overwritten in the command-line by using ``-o/--override``, which can also be
passed multiple times. The expected format is ``name=value``. For example::
Expand Down Expand Up @@ -928,7 +928,7 @@ passed multiple times. The expected format is ``name=value``. For example::

Sets a directory where search upwards for ``conftest.py`` files stops.
By default, pytest will stop searching for ``conftest.py`` files upwards
from ``pytest.ini``/``tox.ini``/``setup.cfg`` of the project if any,
from ``pytest.ini``/``tox.ini``/``setup.cfg``/``pyproject.toml`` of the project if any,
or up to the file-system root.


Expand Down
7 changes: 6 additions & 1 deletion src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def getcfg(args, warnfunc=None):
"""
from _pytest.deprecated import CFG_PYTEST_SECTION

inibasenames = ["pytest.ini", "tox.ini", "setup.cfg"]
inibasenames = ["pytest.ini", "tox.ini", "setup.cfg", "pyproject.toml"]
args = [x for x in args if not str(x).startswith("-")]
if not args:
args = [py.path.local()]
Expand All @@ -44,6 +44,11 @@ def getcfg(args, warnfunc=None):
and "tool:pytest" in iniconfig.sections
):
return base, p, iniconfig["tool:pytest"]
if (
inibasename == "pyproject.toml"
and "tool.pytest" in iniconfig.sections
):
return base, p, iniconfig["tool.pytest"]
elif inibasename == "pytest.ini":
# allowed to be empty
return base, p, {}
Expand Down
7 changes: 6 additions & 1 deletion testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

class TestParseIni(object):
@pytest.mark.parametrize(
"section, filename", [("pytest", "pytest.ini"), ("tool:pytest", "setup.cfg")]
"section, filename",
[
("pytest", "pytest.ini"),
("tool:pytest", "setup.cfg"),
("tool.pytest", "pyproject.toml"),
],
)
def test_getcfg_and_config(self, testdir, tmpdir, section, filename):
sub = tmpdir.mkdir("sub")
Expand Down