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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
malenkiy_scot
  • 16,415
  • 6
  • 64
  • 87

2 Answers2

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.

Community
  • 1
  • 1
this.myself
  • 2,101
  • 2
  • 22
  • 36
  • You can add a different root folder in `Settings > Project Structure > Add Content Root`. – simonzack Aug 15 '14 at 22:25
  • 1
    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! – Andrew Feb 04 '15 at 04:27
  • 2
    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 Aug 03 '16 at 17:05
  • @Memophysic at least for me, that's illegal syntax in regex (multiple repeat with no previous pattern). That looks like glob. – villasv Nov 27 '16 at 15:26
  • @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. – Memophysic Nov 27 '16 at 16:39
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
xmedeko
  • 7,336
  • 6
  • 55
  • 85