Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Python package unit tests in PyCharm

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.

like image 290
malenkiy_scot Avatar asked Mar 06 '14 10:03

malenkiy_scot


People also ask

How do I run a unit test in Python?

If you're using the PyCharm IDE, you can run unittest or pytest by following these steps: In the Project tool window, select the tests directory. On the context menu, choose the run command for unittest . For example, choose Run 'Unittests in my Tests…'.

How do I run an automation script in PyCharm?

Simply click the Toggle auto-run in the test tool window, then run your tests once.


2 Answers

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.

like image 196
this.myself Avatar answered Oct 29 '22 12:10

this.myself


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
like image 41
xmedeko Avatar answered Oct 29 '22 12:10

xmedeko