pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.87k stars 2.65k forks source link

document how to handle minversion= in pyproject.toml #7730

Open graingert opened 4 years ago

graingert commented 4 years ago

currently minversion pyproject.toml[[tool.pytest.ini_options]minversion doesn't apply when running on pytest<6 because those versions don't parse the file.

I've found a work-around using a setup.cfg because pyproject.toml takes priority over setup.cfg:

# pyproject.toml
[tool.pytest.ini_options]
minversion = "6.0"
# setup.cfg
[tool:pytest]
minversion = 6

secondly it would be useful if pyproject.toml took priority over pytest.ini to allow pytest 4-6 to have forward compatibility using pytest.ini instead of setup.cfg which have meanings to other tools

This is not possible to do in a plugin, because minversion is useful assuming you do not have control of the packages installed yet.

see https://github.com/pytest-dev/pytest/issues/7653 for https://pypi.org/project/pytest-forward-compatibility/

nicoddemus commented 4 years ago

Hi @graingert,

it would be useful if pyproject.toml took priority over pytest.ini to allow pytest 4-6 to have forward compatibility using pytest.ini instead of setup.cfg which have meanings to other tools

I sympathize with the problem, but I'm not sure I would like for pyproject.toml (I assume you mean one with a tool.pytest table) to have priority over a pytest.ini file; after all pytest.ini has pytest on the name of the file and is traditionally the source of truth for configuration, getting priority even when completely empty.

I think the use case of needing minversion while supporting both pytest 4 and 6 at the same time is not very common to break the rules of configuration file priority, or creating special cases.

Perhaps just documenting that workaround in the doc for the minversion config value is enough, as you suggested initially?

graingert commented 4 years ago

I think the use case of needing minversion while supporting both pytest 4 and 6 at the same time is not very common to break the rules of configuration file priority, or creating special cases.

My usecase is for a different project where only pytest 6 is used, I incorrectly merged the two issues into one as I thought they would have the same solution.

For this case I want to nudge an engineer into switching environment or upgrading pytest.

I really want to avoid using setup.cfg to deliver the minversion option, as that file has other semantics with other tools

How about supporting this:

# pytest.ini
config=pyproject.toml
minversion=6.2
nicoddemus commented 4 years ago

For this case I want to nudge an engineer into switching environment or upgrading pytest.

Sorry, I'm not understanding the use case quite right yet... are you planning on nudging an engineer by adding a new file to their project which will break their CI? Can you explain it again from the beginning? Sorry if seems obvious, but thanks!

Also consider that you can use a tox.ini file instead of setup.cfg, as pyproject.toml takes precedence:

https://github.com/pytest-dev/pytest/blob/9c0e0c756a36badb84edc017a803b9b0c4c122da/src/_pytest/config/findpaths.py#L92-L97

graingert commented 4 years ago

Also consider that you can use a tox.ini file instead of setup.cfg, as pyproject.toml takes precedence

yup but tox.ini has the same issue - my tox config is in

# pyproject.toml
[tool.tox]
legacy_tox_ini = """
[tox]
minversion = 3.20.0
requires = tox_venv >= 0.4.0
"""

Sorry, I'm not understanding the use case quite right yet... are you planning on nudging an engineer by adding a new file to their project which will break their CI?

as far as I can tell, minversion isn't needed for CI - where I have complete control of the environment. This is for the usecase of people running pytest without tox or CI scripts

nicoddemus commented 4 years ago

as far as I can tell, minversion isn't needed for CI - where I have complete control of the environment. This is for the usecase of people running pytest without tox or CI scripts

But pytest will read a tox.ini with a pytest section even if the user doesn't have tox installed:

# tox.ini
[pytest]
minversion = 6

But if I got that right, I think a simple conftest.py file will solve it nicely:

def pytest_configure():
   if int(pytest.__version__.split(".")[0]) < 6:
       pytest.fail("this project requires pytest >= 6")
graingert commented 4 years ago

Oh that's much nicer, do we want that in the docs?

nicoddemus commented 4 years ago

Oh that's much nicer, do we want that in the docs?

Which one do you mean? I posted two solutions.

graingert commented 4 years ago

Should I document both solutions?

graingert commented 4 years ago

Or we could upgrade this issue to a discussion and link to it?

nicoddemus commented 4 years ago

Upgrade this to a discussion is one solution.

If we want to add this to the docs, I think a note like this under minversion would suffice:

.. note::

    If you use ``pyproject.toml`` for configuration this option won't have an effect in pytest 
    versions prior to 6, which is where support for ``pyproject.toml`` files was introduced.

    A workaround is to add ``minversion`` to ``tox.ini``:

    .. code-block:: ini

        # tox.ini
        [pytest]
        minversion = 6

     While keeping the rest of your configuration (including `minversion`) in ``pyproject.toml``.

     This works because pytest versions prior to 6 will read that file and bail out because the 
     ``minversion`` option, while in pytest >=6 the ``pyproject.toml`` file takes precedence over 
     ``tox.ini``.

Also a test to ensure this is not broken by accident in the future is needed.