pytest-dev / pytest-dev/pytest

--ignore option is not relative to rootdir?

Open
#6,399 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: collection type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

It appears that the --ignore option is not specified relative to --rootdir, which is a bit of a problem when you want to execute your tests from a directory other than the project root (particularly if you also want to allow people to execute from the root directory).

Here's a bash script to create an MWE:

cd $(mktemp -d)  # New project in a temporary dir
mkdir tests
mkdir otherdir

cat > tests/test_ignored.py <<< EOF
def test_bad():
    assert False

raise Exception("I told you to ignore me!")
EOF

cd otherdir

You can then trigger the issue with pytest --rootdir=.. .. --ignore=tests/test_ignored.py. You can see that the file is properly ignored if instead you work in the root directory and do pytest . --ignore=tests/test_ignored.py.

Obviously in this example I can just specify my paths relative to my root directory, but I'm converting over a project to run the tox job in a temporary directory and it's got a bunch of --ignore rules defined in its tox.ini. If I specify relative to the temporary directory, then bare pytest invocations would break. I thought I'd be clever and do something like this:

[testenv]
deps=pytest
setenv = TESTROOT={toxinidir}
changedir = {envtmpdir}
commands = python -m pytest {posargs} --rootdir={toxinidir} {toxinidir}

[pytest]
addopts=
    --ignore=${TESTROOT}/tests/test_ignored.py

But alas either that TESTROOT gets expanded too early or not at all (not sure which).

Should the default test root be switched to rootdir? If not, is there any suggested workaround for this? Any special value that expands to rootdir?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior from otherdir with pytest --rootdir=.. .. --ignore=tests/test_ignored.py, then compare it with the root-directory invocation and review the tox.ini configuration shown. Trace how --ignore and --rootdir are interpreted; done means the ignore rule works from both directories without breaking the temporary-directory workflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.