Skip to content

Change conftest.py to default to python_library rather than python_tests#10603

Merged
Eric-Arellano merged 2 commits intopantsbuild:masterfrom
Eric-Arellano:conftest
Aug 13, 2020
Merged

Change conftest.py to default to python_library rather than python_tests#10603
Eric-Arellano merged 2 commits intopantsbuild:masterfrom
Eric-Arellano:conftest

Conversation

@Eric-Arellano
Copy link
Copy Markdown
Contributor

Problem

conftest.py is (typically) a util file with no tests in it. Now that we always run tests one-file-at-a-time thanks to generated subtargets, this causes Pytest to try to run on conftest.py, which fails because there are no tests.

More philosophically, test util code has always meant to go into python_library, rather than python_tests. A target is "metadata for some files" - it doesn't make sense to say something like timeout=120 on conftest.py.

Solution

Change default source globs so that conftest.py will show up in python_library() now.

Result

Pytest no longer will error if you have a conftest.py and were using default source globs.

If you already had a python_library() target in the directory where the conftest.py was defined—and you have --python-infer-conftests enabled (the default)—then you will not need to make any changes for this to work.

If you only had a python_tests() target—and you have --python-infer-conftests—you simply need to add python_library(name='conftest') to your BUILD file.

If you only had a python_tests target—and do not have --python-infer-conftests—you will need to add python_library(name='conftest') and also update your python_tests() to explicitly depend on that new target.

For new users, their BUILD file will now look something like this in a folder that solely has tests + conftest.py:

python_tests()

python_library(name="conftest")

[ci skip-rust]
[ci skip-build-wheels]

…on_tests`

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 0.0% when pulling 6b22e97 on Eric-Arellano:conftest into 05c3fd2 on pantsbuild:master.

@Eric-Arellano Eric-Arellano merged commit 255f9e0 into pantsbuild:master Aug 13, 2020
@Eric-Arellano Eric-Arellano deleted the conftest branch August 13, 2020 03:23
Eric-Arellano added a commit to Eric-Arellano/pants that referenced this pull request Aug 15, 2020
…an `python_tests` (pantsbuild#10603)"

This reverts commit 255f9e0.

[ci skip-rust]

[ci skip-build-wheels]
Eric-Arellano added a commit to Eric-Arellano/pants that referenced this pull request Aug 15, 2020
…s` again

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
Eric-Arellano added a commit that referenced this pull request Aug 15, 2020
…10619)

In #10603, we moved `conftest.py` from `python_tests` to `python_library`. This is because Pants now runs file-at-a-time for Pytest thanks to the model change in #10511. This causes `conftest.py` to error because it doesn't have any test files.

We decided in #10603 that `conftest.py` should be under `python_library` because it is not actual tests. For example, test util code goes under a `python_library` target already.

However, `python_library` owning a `conftest.py` by default ended up being problematic, as it caused test code to be mixed with production code, which is generally not desired. See #10613 for an approach to fix this.

Instead of adding a `conftest` target, this PR instead moves back `conftest.py` to `python_tests` and special cases `pytest_runner.py` to skip `conftest.py`. Even though this is less correct, it's simpler for users and it avoids making 1.x users having to change a bunch of things. Conceptually, `conftest.py` can be seen as "config" for tests, rather than traditional "test utils" like you'd have in a `python_library`.

[ci skip-rust]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants