Running pytest on existing docker container
Setup:
Python Django project, using docker-compose to describe various depending services (like RabbitMQ, etc.)
I'm using Intellij IDEA Ultimate 2017.2 with the python plugin.
To run tests, I have one working configuration: I created a remote docker-compose Python SDK, with a corresponding run configuration.
The problem is that whenever I'm running tests, IDEA first shows a window "docker-compose up", and apparently creates the corresponding container. This is slow.
To make it faster, I'd like to just run docker-compose up myself (I could do it in a terminal, for example), and configure IDEA to run the tests on the already running container.
To do this, I created a new remote docker Python SDK, with a corresponding run configuration.
When I try to execute the tests, the following problem is printed:
8384bd3d19a7:python -u /opt/.pycharm_helpers/pycharm/_jb_pytest_runner.py
Testing started at 11:29 ...
Traceback (most recent call last):
File "/opt/.pycharm_helpers/pycharm/_jb_pytest_runner.py", line 5, in <module>
import pytest
ModuleNotFoundError: No module named 'pytest'
Apparently, python doesn't recognize the pytest module, which is in requirements-dev.txt, and installed in the Dockerfile. When looking at the SDK settings for the remote docker, under "packages", I indeed cannot see pytest. (In the working remote docker-compose SDK, I do see pytest.)
Does somebody know what's going on, and how I could fix this?
Thanks in advance!
Please sign in to leave a comment.
Did you ever end up coming up with a fix? I'm in a similar situation where I'd like my pytest tests to be run within an existing container. Spinning up a new container is too slow and hampers my iteration speed.
Unfortunately, no. I ended up setting up everything locally, without docker.
Nuts, I am in the same boat.
How is it support does the same things we are in the instructions and it works? I would LOVE to have this feature working (and sort of desperately need it), but their inaction to resolve this issue is really hurting me using this IDE.
If you wish to reuse the existing container, then you should use the exec command in the run configuration.
Have you tried it?
Hi Sergey,
I have *not* tried it. Can you elaborate? How can I use exec in the run configuration?
Andreas,
I'm not sure about run configuration, but you can create an external tool that runs "docker exec" on an existing container, and run it from PyCharm.
Is there a way to set up the "exec" command so we can attach the debugger? From a terminal I can run my tests like "docker-compose exec servicename python -m pytest -c ./pytest.ini ", but don't see how to do this through a configuration that will let me attach the debugger.
What I'd like to achieve, is that I can e.g. right click on a pytest test and click "run", and then the test is executed in the container (because e.g. it depends on another service like a database, which only runs in docker-compose). If I understand correctly, the way to run pytest tests from the IDE, is with the pytest run configuration.
But I can't find a way to tell pytest to execute in the container.
When I execute one test, the following command line is issued (example):
/Users/theuser/.pyenv/versions/alpha-3.7.7/bin/python "/Users/anpr/Library/Application Support/JetBrains/IntelliJIdea2020.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --target test_it_example.py::test_function_to_execute
But that will not run in the container, on the host.
Does anybody know how to run tests on the container?
There doesn't seem to be a proper way to do that. Please feel to submit a feature request (https://youtrack.jetbrains.com/issues), and though not convenient, the workaround with external tool I mentioned above seems to work.
Any update on this?
I would love to see this feature as well. Thank you Andreas Profous for so eloquently and carefully expressing this concept.