0

I am learning to write tests and I am having issues with the PyCharm configuration.

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

Now, if I click on the green run symbols next to my test functions, they are launched but get stuck at the collecting phase:

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

I think this is because pytest cannot find my project folder. If I run pytest within the pycharm terminal:

python -m pytest ./tests

I get this error:

ModuleNotFoundError: No module named 'myproject'

This is my folder structure:

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

I wonder if anyone could please help me to to avoid these issues and properly run pytest from the pycharm sidebar launch icons and/or pycharm terminal.

Delosari
  • 677
  • 2
  • 17
  • 29
  • Maybe not your desired solution, but I/we always install the package (given a proper `setup.py`) via `pip install -e .` (of course into a dedicated environment). – Dr. V Jul 04 '23 at 19:51
  • For your terminal issue: your `myproject` module is stored under `src` dir. Have you added it as "Sources root"? It should either be in `PYTHONPATH` or marked as "Sources root". Right click `src` -> Mark directory as -> Sources Root – pL3b Jul 05 '23 at 07:44
  • Ty @pL3b for your comment: In Pycharm my custom is adding the src folder as a source folder. (in File -> Settings -> Project: -> Project structrure -> Source folders). But you are right in the Pycharm terminal it is not included (it is on the python console though). How could I add it from the interface? – Delosari Jul 06 '23 at 14:46
  • Ty @Dr.V for your comment. You mean my project? Should we not keep both tests and project on the same project/repository? – Delosari Jul 06 '23 at 14:46
  • @Delosari please have a look at this answer - https://stackoverflow.com/a/50156706/17200418. Also in this thread there is another way of doing it - Installing the package with src folder included. However I prefer running tests using pycharm UI, not terminal. So you definitely need to fix your collecting stuck issue. Some debugging may help, but it can be quite complicated for beginners. – pL3b Jul 07 '23 at 09:50
  • @Delosari, Yes. I would/do install the actual project with the `-e` option, such that code changes take immediate effect (so you can still write new tests and debug your code). Then the tests can import the modules. We do this to avoid any further IDE dependent path configurations (because if you have 5 developers, they will use at least 7 different IDEs ;)). – Dr. V Jul 07 '23 at 21:20
  • Ty @pL3b for your patience, I have I tested with the pytest.ini and the pyproject.toml. I get the message: rootdir: D:\Pycharm projects\lime, configfile: pyproject.toml but the terminal gets stuck on the "collecting..." part – Delosari Jul 10 '23 at 13:51
  • Ty @Dr.V I have done the editable installation of my project within pycharm. Now I can run my notebook documentation thanks to the editable installation, so thanks a lot. However, it still gets stuck on collecting... for the tests. – Delosari Jul 10 '23 at 15:36

0 Answers0