Replies: 2 comments 1 reply
-
Resolving a path is cwd relative |
Beta Was this translation helpful? Give feedback.
1 reply
-
Converting this to a discussion, as this has nothing to do with pytest itself really. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Bug when executing pytest via Terminal.
Hello together, I have the following issue:
I am running pytest in PyCharm. It runs Python tests in tests. In my testcode I have a test sql file which I need to open and to execute during the test run. I am using the method str(Path("path/to/sql").resolve()) to get the absolute path. When this method gets executed in tests in PyCharm it returns than the correct absolute path: "~/project_dir/tests/path/to/sql". So via PyCharm or IntelliJ UI the tests are working fine.
But now I have a problem when I try to execute pytest via terminal: pytest ./tests --cov=./src --cov-branch. The test case fails since I get a FileNotFoundError. The absolute path which I get back from the method above is than "~/project_dir/path/to/sql". It resolves it to the project_dir and not to tests. Why does this happen and what should I need to adjust in order to fix it?
I know that one option is to add the absolute path in my device instead of resolving it, but I will also execute test than in a ci-cd pipeline, where the tests are execute in a container. There I will have a complete different folder structure. I want to have it generic in that way. I also could now do a workaround where I can check what is the working_directory if it is tests than str(Path("path/to/sql").resolve()) is enough, else go with str(Path("tests/path/to/sql").resolve()). Than it would also work. But I assume that there should be a way to fix it via command-line and I want it to fix it there to avoid doing these kind of workarounds.
The problem is that the working_directory in terminal is the project folder and in PyCharm it is set to /tests. But there is no possible way to set the working directory in pytest command line. Also when I specify --rootdir=./tests this does not work.
More info about my project: it is pretty simple structured:
my_project
Kind regards,
Maxim
Beta Was this translation helpful? Give feedback.
All reactions