How to write and run unit tests for a Python plugin in 2024?

Answered

Hey guys,

I am developing a PyCharm-Plugin (my 1st one) that uses the Python Debugger and accesses the PyDebugProcess instances and things related to it. Now, I am facing the challenge to test my classes and methods that interact with the Debugging Process. Currently, I am using JUnit4 and I've read the docs as well as some old quesions here, but I am still quite lost how to setup these tests properly.

In my imagination, I would like to have a dummy python project inside my testData folder and would like to simulate/run it with breakpoints so that I am able to get access to debugging related content. The question for me is, how do I setup a fixture that I can work with? I've seen PyTestCase class, mentioned in another old post here, but it is not shipped with the IntelliJ Testing Framework. To sum up, I am not sure how to do it the right way.

Can you help me with it? My build ranges are currently 223-241.* and I am on Build 241.14494.240.

Thanks in advance!

0
1 comment
Official comment

Hey Julian,

Thank you for your interest in developing plugins for PyCharm.

You are correct in your understanding. You will need a dummy Python project and a real Python interpreter to run the debug process. For this, you should use the PyEnvTestCase base class. Refer to the PythonDebuggerTest class for a detailed example.

Unfortunately, the test framework classes are not included in any Maven packages. Here are your two options:

  1. Copy and Paste: Copy the necessary classes from the IntelliJ Community repository on GitHub.
  2. Write Your Own Classes: Create your own classes inspired by the existing code.

Regarding the existing framework, it is important to note that it requires the Python interpreter to be installed with the Gradle script available at https://github.com/JetBrains/intellij-community/tree/master/python/setup-test-environment. Set the PYCHARM_PYTHONS environment variable to point to the location where the test Python environment will be installed and run ./gradlew build in that directory. It is advisable to install the test environments outside your project to avoid heavy indexing.

Please don't hesitate to ask any further questions.

Best regards,

Andrey

Please sign in to leave a comment.