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

How to change the default rootdir ($PWD) to some other path #4594

Closed
Lekensteyn opened this issue Jan 3, 2019 · 11 comments
Closed

How to change the default rootdir ($PWD) to some other path #4594

Lekensteyn opened this issue Jan 3, 2019 · 11 comments
Labels
topic: config related to config handling, argument parsing and config file topic: selection related to test selection from the command line type: question general question, might be closed after 2 weeks of inactivity

Comments

@Lekensteyn
Copy link

Is it possible to change the default rootdir (where tests and conftest.py are located) based on a file in the current working directory?

I am using pytest for testing a C project (not Python), so it is quite common to have a separate source and build directory. Additionally, tests are located in a subdirectory. The directory layout could be:

$srcdir/test/conftest.py    (contains pytest_addoption and other stuff)
$srcdir/test/pytest.ini     (contains python_files to override the test file pattern)
$srcdir/test/some_tests.py

Right now, tests are executed from the build (or source) directory by passing the test directory, e.g.:

srcdir$ pytest test
objdir$ pytest $srcdir/test

What I am trying to achieve is being able to run the same tests with:

srcdir$ pytest
objdir$ pytest

To this effect, I have tried to create $srcdir/pytest.ini and $objdir/pytest.ini containing testpaths = test and testpaths = $srcdir/test respectively. Unfortunately $srcdir/test/conftest.py is initially not picked up and pytest_addoption is not executed.

Essentially I want to redirect the rootdir by creating a file in the current working directory (source or build dir). Is this possible? What alternatives do I have, while trying to keep the number of pytest-related files (e.g. conftest.py) in the top-level directory to a minimum?

@Zac-HD Zac-HD added type: question general question, might be closed after 2 weeks of inactivity topic: selection related to test selection from the command line labels Jan 4, 2019
@Lekensteyn
Copy link
Author

Lekensteyn commented Jan 4, 2019

Found a potential solution using addopts, not sure if it is intended that way, but it works. It does not change the rootdir, but will load tests from the correct path.

In the top-level source directory, have pytest.ini consisting of:

[pytest]
testpaths = test
python_files = suite_*.py group_*.py
addopts = -ra

In the build directory, duplicate relevant options from the top-level pytest.ini file but add the test directory to the addopts option:

[pytest]
python_files = suite_*.py group_*.py
addopts = $srcdir/test -ra

Adding testpaths here will not matter since it is ignored when a path is explicitly provided. This is documented here: https://docs.pytest.org/en/latest/reference.html#confval-testpaths

Is this supported/recommended? It is not exactly documented at https://docs.pytest.org/en/latest/customize.html#rootdir

Edit: one issue with this solution is that nodeids in pytest_collection_modifyitems will include the test/ suffix when executed with a relative path from a parent directory.

@blueyed
Copy link
Contributor

blueyed commented Jan 4, 2019

Is objdir below srcdir?
If it would be e.g. build/ then you could just use the ini from the srcdir above, no?

@blueyed blueyed added the topic: config related to config handling, argument parsing and config file label Jan 4, 2019
@Lekensteyn
Copy link
Author

Is objdir below srcdir?

Sometimes, but not always. I often have objdir=/tmp/wsbuild and srcdir=/tmp/wireshark (the latter being a symlink into my home dir).

@blueyed
Copy link
Contributor

blueyed commented Jan 6, 2019

Does using --rootdir explicitly help?

@Lekensteyn
Copy link
Author

Does using --rootdir explicitly help?

It would not, the goal was to avoid adding explicit options while in the build directory. Adding --rootdir to pytest.ini also does not change the rootdir. Adding it to pytest --rootdir=$srcdir does not have the desired effect either since it tries to look up pytest.ini in the current or parent working directories.

So one remaing question, is it supported to have an explicit test directory in pytest.ini (addopts = $srcdir/test -ra)?

@Zac-HD Zac-HD closed this as completed Jan 23, 2019
@nnop
Copy link

nnop commented Dec 5, 2019

Why was this issue closed?

@nicoddemus
Copy link
Member

Hi @nnop,

Not sure why it was closed, @Zac-HD usually leaves the reason when closing an issue.

Anyway,

So one remaing question, is it supported to have an explicit test directory in pytest.ini (addopts = $srcdir/test -ra

This is supported by using testpaths, but this won't affect localizing the config files: --rootdir is not used to find config files, it is actually defined by finding a config file.

More info: https://docs.pytest.org/en/latest/customize.html#finding-the-rootdir

@nnop
Copy link

nnop commented Dec 5, 2019

Thanks for the quick responding. @nicoddemus
I'm a little confused about several kinds of paths (sys.path, rootdir, testpaths) about how they affect the searching of tests and config files.

@nicoddemus
Copy link
Member

Let me try to quickly summarize:

  • sys.path: pytest won't look into that at all, it is used by the Python runtime to find modules during import
  • rootdir: is determined after finding a suitable config file (pytest.ini) or by the common-ancestor of all paths given in the command line (the logic is explained in detail here).

Test files are always searched starting from paths given in the command-line. If if no path is given in the command-line, testpaths is used, otherwise then $PWD is used.

@nnop
Copy link

nnop commented Dec 5, 2019

Great, now I'm clear on that. 😀

@Zac-HD
Copy link
Member

Zac-HD commented Dec 5, 2019

@nnop - we have a general policy of closing question-issues after two weeks of inactivity, which makes it easier for users and contributors to sort through all the other issues (e.g. bug reports and feature requests) we get.

Glad @nicoddemus could help out though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: config related to config handling, argument parsing and config file topic: selection related to test selection from the command line type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

5 participants