question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
nicoddemuscommented, Dec 5, 2019

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.

0reactions
Zac-HDcommented, 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change pytest rootdir - Stack Overflow
But I think you will need to use python -m pytest , not just pytest , in order to make sure that the...
Read more >
TSConfig Option: rootDir - TypeScript
Default : The longest common path of all non-declaration input files. If composite is set, the default is instead the directory containing the...
Read more >
How to change the root directory for a Linux user?
It sounds like you want to change the HOME dir, not the root dir. Root will always be / or /root. usermod -d...
Read more >
How to change directory in Linux terminal - nixCraft
This page explains how to change directory in Linux terminal using the cd command and related commands such as pwd, ls, and others....
Read more >
16.6 The working directory for R code chunks | R Markdown ...
The usual way to change the working directory is setwd() , but please note that setwd() is not persistent in R Markdown (or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found