How does PyCharm determine if a file/folder has unit tests?

This question was asked in another post (http://forum.jetbrains.com/thread/PyCharm-1446), but there wasn't an answer given.

I've been having some problems with PyCharm not recognizing tests, but first and foremost, I'd like to understand how it works. I went looking through the community edition source code to try to figure it out, but was unsuccessful since it is a large codebase that I'm unfamiliar with. Even saying "look in this source file" might be enough.

I can describe my problem, but without knowing how PyCharm detects tests, it'll be kind of vague and full of details that are of questionable relevance:

The problem is that some files/directories don't have the context menu option to run unit tests despite having unit tests in them.

I have three projects open at once in a single PyCharm window.

Each project has a folder structure like:

MyProject
  myproject
    test

Inside the 'test' folder are some tests that frequently use a subclass of unittest.TestCase (Tornado's AsyncHTTPTestCase, for example).

On one project, if I right-click the test folder, I'll have the expected "Run 'Unittests in...'" menu. In another project it will instead only have "Run 'Doctests in...'" (though I don't have any doctests). I've seen times when both of those options are available as well, but I'm not seeing that at the moment on any of my projects.

Sometimes if I open a "broken" project individually, PyCharm will recognize the unit tests and let me run them, but not always. Through various reopenings of projects and restarting PyCharm, I've sometimes gotten all projects to work as expected. Sometimes restarting PyCharm will also "break" a project that had been working previously.

If I understood how PyCharm does its test detection, I could probably narrow it down a bit.

Thanks,
Eric
2 comments
Comment actions Permalink
Hi Eric,

the logic behind creating test run configurations is located at PythonTestConfigurationProducer.
For the folders PyCharm checks, that either folder name contains "test" or it's marked as content/source root.

Please note, if you have configuration associated with this location (in Run->Edit Configurations..) PyCharm will not propose to create a new one.
0

Please sign in to leave a comment.