Pytest stuck on collection ...

Hi I am learning to write tests for the first time and I am stuck with the PyCharm settings.

First, I had this issue  in windows 11 where the pytest command would get stuck. The solution was to run a pytest comand as an administrator.

If I click on the green run symbols next to my test functions the terminal gets stuck at the collecting phase:

============================= test session starts =============================
collecting ... 

I wonder if this is because the script does not find the testing functions on my project, this is the case once I run pytest from the pycharm terminal. This is my folder structure:

myproject/
docs/
examples/ src/
myproject/ functionsA.py functionsB.py tests/ myproject_test.py

The  myproject_test.py starts with:

from myproject.functionsA import operation1

and the error message is 

ModuleNotFoundError: No module named 'myproject'

I wonder if anyone has any advice on how to avoid these issues and how to properly run pytest from the pycharm sidebar launch icons and/or pycharm terminal.

Thanks for any advice.

0

When you run it in the terminal, do you run it from the same directory that's defined as a "Working directory" in your run configuration (Run | Edit Configurations...)?
pytest shouldn't just hang on "collecting" stage. If no tests were found, it should report so. So it seems that the process may just hang at that stage.

0

Hello Sergey Karpov.

I am running it on my root folder (from the file tree example above that would be:)

D:/Pycharm projects/myproject/

I came by this question which is very similar to mine and I tried adding a pyproject.toml on my root folder:

[tool.pytest.ini_options]
pythonpath = ["src"]

Now, the terminal gets stuck as well. This is the command I use on the root directory:

python -m pytest ./tests

And this is the message I get:

platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\Pycharm projects\lime, configfile: pyproject.toml
plugins: asdf-2.15.0, cov-4.1.0, anyio-3.5.0
collecting ... 

Would you have any suggestion?

0

What if you run it with the following commands? Is there any additional output?

python -m pytest -s ./tests

python -m pytest -v ./tests

0

Do your tests' names start with the word "test"?

0

Thank you Sergey Karpov for the reply.

Yes all the tests start with "test".

I am having the same issues with those commands:

(base) PS D:\Pycharm projects\lime> python -m pytest -s ./tests
=================================================== test session starts ====================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\Pycharm projects\lime, configfile: pyproject.toml
plugins: asdf-2.15.0, cov-4.1.0, anyio-3.5.0
collecting ... 
(base) PS D:\Pycharm projects\lime> python -m pytest -v ./tests
=================================================== test session starts ====================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- C:\ProgramData\anaconda3\python.exe
cachedir: .pytest_cache
rootdir: D:\Pycharm projects\lime, configfile: pyproject.toml
plugins: asdf-2.15.0, cov-4.1.0, anyio-3.5.0
collecting ... 
0

请先登录再写评论。