6

I just 'ported' a Python package I'm writing to PyCharm and having a bit of trouble running unit tests for the whole package from the IDE.

In __init__.py for the package I have load_tests function that goes over all modules in the package and loads relevant tests. It runs splendidly with:

$python -m unittest my_package

However, when I try running it from PyCharm (by selecting the top directory in the Projects window and hitting Ctrl+Shift+F10) I get No tests were found in the Run window, and

...\python.exe ...\pycharm\utrunner.py .../my_package/ true
Testing started at ...
Process finished with exit code 0
Empty test suite.

in the console window.

I took a quick look at PyCharm's utrunner.py and it seems that it is looking for modules with a certain pattern (that start with test). I would like to preserve the present vanilla approach. How can I configure PyCharm to use load_tests from __init__.py while modifying the code as little as possible?

By the way, test suites for individual modules run just fine from PyCharm.

Using PyCharm 3.1 Community Edition, Python 2.7.

0

2 Answers 2

5

This answer has been written considering PyCharm 3.4.

Had the same problem, found solution for the problem in this answer, hope I understood your question right:

https://stackoverflow.com/a/12242021/2427749

I configured my Python Test runner configuration like this:

  • Checked 'All in Folder'
  • Pattern is the Regular Expression '.*_Test.py' (without quotes)
  • Checked 'Inspect only subclass for unittest.TestCase'.

Now it finds my unitests in my subfolders named like classToBeTested_Test.py

by the way, I'm facing another problem now: the unit test cannot import the module to be tested. Cause of different root folder I think.

Sign up to request clarification or add additional context in comments.

5 Comments

You can add a different root folder in Settings > Project Structure > Add Content Root.
Just an FYI, for others... it took me a little while, but "pattern" uses a regex, not a regular shell glob like *Test.py. If use something like that the test runner will crash. I didn't realize it wanted a regex until I looked at the stack trace!
Just to complete for those folks which might not know all about regex, a single start ( * ) will not search recursively, use two stars for that. e.g.: **_test.py
@Memophysic at least for me, that's illegal syntax in regex (multiple repeat with no previous pattern). That looks like glob.
@VillasV Indeed, but it works in PyCharm. I'm unaware of how the JetBrain folks implemented it though. PyCharm is coded in Java after all. Anyways ** is quite common now, even glob works with it since 3.5.
2

With PyCharm 2016.2 use:

  • Test: Script
  • Script: /path/to/tests/__init__.py
  • Check Inspect only subclasses of unittest.TestCase. This causes utrunner to use unittest.TestLoader.loadTestsFromModule() and that method call load_tests() if present in the module.

I.e. the command is

python C:\python\pycharm\helpers\pycharm\utrunner.py /path/to/tests/__init__.py true

I had to remove the tests directory from the sys.path in the __init__.py as well (see PY-15889):

basedir = os.path.dirname(__file__)
try:
    sys.path.remove(basedir)
except ValueError:
    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.