Apparently my codebase uses this project so I'm just stumbling upon this, so forgive my ignorance if this exists or handled in another way
I've been trying to understand how the whole conftest.py system works cause I have two issues
conftest.py i.e. tests don't work if you want to run them individuallypytest -s -c=./config/python/pytest_unit.ini path/to/tests/file_test.py
if I put a conftest anywhere (path/*, or path/to/tests/*) it is not detected, but running without a path does detect it
I might want to disable internet access or setup mocks depending on the type of test I want to run (we use different ini files and suffix based file detection) but I don't see the option in: https://docs.pytest.org/en/latest/reference.html#ini-options-ref
(there is a useFixture but that doesn't seem to solve my problem)
[pytest]
testpaths=path
python_files = *_test.py
addopts = --tb=native --disable-pytest-warnings
# Add this:
setup_file=path/setup_pytest.py
I'm getting ahead of myself but if this is something to add then pls comment with the places of which files to change (i.e. where the definitions are and where the code should go) if someone wants to write a PR so that they're not daunted by the new codebase
Closing this because it hasn't been answered in several months open 😕
Oh I completely missed this one.
conftest.py files are discovered automatically as test files are collected, so things should be just working. Is pytest_unit.ini located outside the test tree?
@ayroblu is this still an issue for you? If so we can reopen the issue.
Yup, pretty sure this is still an issue. It seems pretty straightforward, with a specific file specified as the test to run, there's no test tree and therefore no conftests that are detected?
conftest.py files are meant to be put next to the test files. Can you post the full directory tree? It is OK to mask it a bit if it is confidential, but the test, config and conftest files themselves must appear in their actual location relative to each other.
Closing this because it hasn't been answered in several months open
Thanks for the issue grooming btw Zac, appreciate it! Closing old issues without resolution sometimes is the ping needed to bring an issue back to attention. 😁
Hey, thanks for this, I haven't worked on this in a while, but it looked like at the time:
config/python/pytest_unit.ini
python/path/to/file_test.py
And then it didn't matter where I put my conftest.py, it never ran if I specified a file, but it did if I just ran it on the whole project.
To me the problem seems pretty clear in that it's not a supported use case, then the supported implementation would be line in the ini with conftest.py that you are definitely sure you want to include irrelevant of directory structure, and then whatever else this pytest style dictates (perhaps cli args etc). Though I might be completely missing the mark here
Hey just following up!? @nicoddemus
Thanks for the ping @ayroblu,
And then it didn't matter where I put my conftest.py, it never ran if I specified a file, but it did if I just ran it on the whole project.
Hmm if the .ini and test files are in a different hierarchy, then indeed the conftest.py files won't be located.
The standard/recommended layout is to have your config file at the root of the tests directory, and then `conftest.py´ files placed inside this hierarchy according to their visibility.
@nicoddemus
I have the same issue even though my conftest.py is in my rootdir together with my pytest.ini.
pytest -c /home/user/project/pytest.ini --rootdir=/home/user/project --pyargs testsA testsB --with-some-plugin
The command above doesn't use my conftest: /home/user/project/conftest.py so it doesn't find and register the plugins defined within.
However, just running:
cd /home/user/project; pytest --pyargs folderA folderB
will find the conftest and correctly register the plugin. I only have one conftest file. I just want to be able to run pytest without changing CWD.
Hi @magnusvmt,
In the command-line you posted, shouldn't testsB be in their own --pyargs argument?
pytest -c /home/user/project/pytest.ini --rootdir=/home/user/project --pyargs testsA --pyargs testsB --with-some-plugin
I've just created a similar layout here and my conftest.py file is found.
@nicoddemus
With your syntax I get:
pytest: error: unrecognized arguments: testsB --with-some-plugin inifile: /home/user/project/pytest.ini rootdir: /home/user/project
So it doesn't even recognize the second pyargs. If I remove the plugin argument, it runs the tests in both testsA and testsB.
If I instead remove the second pyargs (testsB) I get:
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...] pytest: error: unrecognized arguments: --with-some-plugin inifile: /home/user/project/pytest.ini rootdir: /home/user/project`
So for some reason pytest is not registering the plugins listed in conftest if I provide the inifile or rootdir arguments.
OH sorry, I should have realized that sooner: unfortunately command-line arguments and conftest.py files are bit tricky, please see this note here:
https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_addoption
This function should be implemented only in plugins or conftest.py files situated at the tests root directory due to how pytest discovers plugins during startup.
There's an explanation there on why this is slightly more complicated.
OH sorry, I should have realized that sooner: unfortunately command-line arguments and conftest.py files are bit tricky, please see this note here:
https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_addoption
This function should be implemented only in plugins or conftest.py files situated at the tests root directory due to how pytest discovers plugins during startup.
There's an explanation there on why this is slightly more complicated.
I have the pytest_addoption function implemented in every plugin separately. It works as it should as long as I run pytest from the root directory without the -c and --rootdir flags, or if I remove conftest and use setuptools entry_points instead (however, then I can't control the order they get registered).
Do you mean I should implement the pytest_addoption function in conftest.py instead? Make a long function that parses all plugin options? I will try that tomorrow!
Thank you for being very helpful :)
Actually the pytest_addoption plugin works with conftest.py files, but with the caveat that the conftest.py needs to be one of the "initial conftest" files (meaning it should be in the root of the collection directory). We only have that warning because many times people expected (reasonably) that pytest_addoption will work even when defined in conftest.py files deep in the collection tree.
Having said that if you always execute the test suite the same way and it is working, then that's fine, but if you want to execute it from anywhere and have it working always, then setuptools entry points is the best bet. 👍
I... think it's working now? Let's try closing the issue again to find out!
Yes sorry, putting the conftest inside the path does get run
What is the way to launch tests if I have next directory structure:
.........|-
...................|-conftest.py
...................|-pytest.ini
...................|-
And I would like to launch them from
I do:
cd ${PROJECT_ROOT}
pytest --rootdir=${TESTS_ROOT}/ --myOption1 --myOption2
but pytest doesn't resolve my options from conftest :(
@elchupanebrej
Have you tried it without --rootdir?
Also make sure that your conftest.py gets loaded (put an assert 0 there at the module level, and with pytest_addoption etc), and also check with PYTEST_DEBUG=1 set in the env.
But this is off topic with this issue AFAICS, so please consider following up on IRC/StackOverflow/a new issue.
I had a similar case
/root/code/service1/automation/tests/conftest.py
Now I wanted to use this conftest.py into another directory to run test file say
/root/code/service2/automation/tests/test_suite1.py
without duplicating the entire file from service1 to service 2 directory.
CWD: /root/code/service2/automation
Still no solution
I have almost similar problem
I need to use the conftest.py file form one directory to other directory.
....repo1/
........../DIR1/
................/conftest.py
................/testdir1
................/testdir2
........../DIR2/
................/testdir3
................/testdir4
I need to use DIR1/conftest.py file in DIR2/
Anyone help me with this structure how can i access this conftest.py
Hi @kgabasha,
The simplest solution is to just move the conftest.py up one directory (repo1/conftest.py).
In case you can't do that for some reason, if it is possible for DIR2 to import DIR1 (I DIR1 and DIR2 are separete projects), an alternative is to rename DIR1/conftest.py to DIR1/fixtures.py, and then use conftest.py files to load it as a plugin using pytest_plugins.
To exemplify:
repo1/
DIR1/
conftest.py # contains: pytest_plugins = ...
fixtures.py # original contents of conftest.py
DIR2/
conftest.py # contains: pytest_plugins = ...
The conftest.py files above would contain:
pytest_plugins = "DIR1.fixtures"
@Azee77 this should also help with your case (sorry I missed this).
HTH,
Hi @kgabasha,
The simplest solution is to just move the
conftest.pyup one directory (repo1/conftest.py).In case you can't do that for some reason, if it is possible for
DIR2to importDIR1(IDIR1andDIR2are separete projects), an alternative is to renameDIR1/conftest.pytoDIR1/fixtures.py, and then useconftest.pyfiles to load it as a plugin usingpytest_plugins.To exemplify:
repo1/ DIR1/ conftest.py # contains: pytest_plugins = ... fixtures.py # original contents of conftest.py DIR2/ conftest.py # contains: pytest_plugins = ...The
conftest.pyfiles above would contain:pytest_plugins = "DIR1.fixtures"@Azee77 this should also help with your case (sorry I missed this).
HTH,
This still does not answer the issue I want to solve. Pytest does not support that directly. Looks a lot of code change will be needed
This still does not answer the issue I want to solve. Pytest does not support that directly. Looks a lot of code change will be needed
It does not address your request no, but will solve your problem with fairly minimal changes. Of course it is up to you to accept this offer of help, or ignore it.
@Azee77 the code changes needed should be fairly minimal, rename a file to something other than conftest.py so it is not included according to the conftest discovery rules, and then make that new name visible by including it in the root conftest.py file via the pytest_plugins = [...] mechanism. You still need to be sure your rootdir is set properly for that plugin to be visible from where you're invoking, which hopefully was the repo1 level.
You might get some insight working through this set of pytest examples I wrote up recently
https://github.com/jxramos/pytest_behavior