Testing your Python code with PyCharm

Dmitry Filippov

Everybody knows that getting used to writing the testing code in parallel with the running code is a good habit. Used wisely, this method helps you define more precisely your code’s intent and find bugs ASAP, thus saving many hours of debugging that would otherwise have been needed in later phases of the software development cycle.

There is a bunch of testing frameworks available for Python which helps you with keeping your code consistent. PyCharm supports most well-known testing frameworks, such as: Attest, Doctest, Nosetest, py.test and of course Unittest.

We have prepared the Creating and running a Python unit test tutorial. From this simple tutorial you’ll learn how PyCharm helps creating and running Python unit tests with a couple of shortcuts. Try it, use it, and do not forget to explore the graphical test runner embedded in PyCharm.

As always, we appreciate your feedback in the comments below or in our forum!

Develop with pleasure!
– PyCharm Team

Comments below can no longer be edited.

22 Responses to Testing your Python code with PyCharm

  1. ivanelson says:

    July 17, 2013

    This IDE should have feature was “Drag” and “Drop” For Python and PyQt.

    • Dmitry Filippov says:

      July 18, 2013

      What do you mean by “Drag” and “Drop” For Python and PyQt?

      • Bulat says:

        July 20, 2015

        Visual interface builder to drag and drop buttons ans text areas to a form, I think

  2. Cezar says:

    July 21, 2013

    Sadly I’ve never been able to get it to work with django-nose tests

    • Dmitry Filippov says:

      July 22, 2013

      What was the problem? Have you filed an issue to youtrack?

  3. paul harley says:

    November 7, 2014

    In PyCharm 3.4.1, the ‘Find Usages’ and ‘Go To…’ context-menu items don’t work for fixtures. This is quite annoying.

    e.g. given this test (in test.py)
    def test_sale_http_response(self, fixture_create_sale):

    • Allan Lewis says:

      August 13, 2015

      I’m not sure if find_usages is soluble, but if you add a type hint in your docstring you will get code completion:
      @pytest.fixture()
      def driver(request):
      """Return a `WebDriver` instance with the app loaded.

      :type request: FixtureRequest
      """

  4. paul harley says:

    November 7, 2014

    …Continue from my above post….

    if I right-click on ‘fixture_create_sale’ and select ‘Find Usages’ I am not shown the fixture implementation. It should be easily found as it resides in the contest.py in the same folder as test.py.

    Is there something else I need to do. This seems pretty basic stuff to me, and I am surprised it doesn’t just work.

    • Carlo says:

      August 22, 2017

      Almost 3 years later and it is yet to work

  5. Feature Spotlight: Python Code Coverage with PyCharm | JetBrains PyCharm Blog says:

    June 20, 2015

    […] covered how PyCharm helps you write clean, quality, maintainable code as well as test it with the integrated graphical test runner. This post will supplement earlier blog posts by highlighting Code Coverage support available in […]

  6. Oleksandr says:

    January 29, 2016

    How to configurate pycharm test runner using django-nosetests?

    • Dmitry Filippov says:

      February 1, 2016

      1. File | Settings (Preferences) | Tools | Python Integrated Tools | Default Test Runner set to Nosetests
      2. Then Run | Edit Configurations | Add new configuration | Django Tests

  7. Alessandro says:

    October 23, 2016

    PyCharm 2016.2.3.

    As in previous versions I’m not able to run tests (“no test were found”) without specifing a Pattern (unitest). There is a (hidden) default ?
    No matter if files are named “MyClass.py” or “test_MyClass.py”.
    All tests are in classes derived from TestCase and the test method starts with “test_”.
    I selected “All in folder” but nothing.
    I have to set “*” in Pattern and it works.

    Another thing.
    I created test configuraton “All tests” using “Nosetests” instead of “Unitests”.
    When I realized the error I have deleted configuration and I have created a new “Unitests” with the same name.
    No matter, it continued to use “Nosetests” giving me some error in the console about nose configuration.

    Can you explain the first behavior, about the “need of Pattern” ?
    Alessa

    • Dmitry Filippov says:

      October 25, 2016

      Hi, as it might lead to a larger discussion and probably to reporting bugs or usability problems, may I ask you to address your question to the tech support team? jb.gg/pycharm-support

  8. mohammed says:

    January 17, 2017

    hi ..
    when i write simple code with pycharm like :

    print “hello world!”

    it is started testing and show some Errors ..

    how i can fix it please !?

    thanks

    • mohammed says:

      January 17, 2017

      note : i’m using community version

    • Ernst Haagsman says:

      January 17, 2017

      Hi! Your code isn’t a unit test so you wouldn’t be able to run it as a unit test. To use your code run it using a ‘Pure Python’ run configuration, the easiest way to do this is to right-click anywhere in your code, and to click ‘Run ‘. If you’d like to know more about unit testing you could read this tutorial: https://jeffknupp.com/blog/2013/12/09/improve-your-python-understanding-unit-testing/

      • mohammed says:

        January 17, 2017

        hi !
        i’m trying this code and try ” run ” more a way :

        print “ok”

        but the pycharm is start testing and give this :
        ——!—–
        Testing started at 10:16 PM …
        Traceback (most recent call last):
        File “C:\Program Files\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pycharm\utrunner.py”, line 153, in
        modules = [loadSource(a[0])]
        File “C:\Program Files\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pycharm\utrunner.py”, line 65, in loadSource
        module = imp.load_source(moduleName, fileName)
        File “C:\Users\Dr moh\new\hashex.py”, line 2
        print “ok”
        ^
        IndentationError: unexpected indent

        Process finished with exit code 1
        ——-!———-

        do you have any idea ?
        thx a lot !

        • mohammed says:

          January 17, 2017

          thx i’m reinstall pycharm and solved this . thx

  9. Andres Ocker says:

    July 14, 2017

    There may be noticeably a bundle to learn about this. I assume you made certain nice points in options also.

  10. VC says:

    January 31, 2019

    Is the graphical test runner embedded only in PyCharm premium and NOT the Community version? Do you have any recommendations for a coverage tool in the Community version?

    • Ernst Haagsman says:

      February 5, 2019

      The test runner is available in Community Edition as well. Coverage is a feature only available in Professional Edition.

Subscribe

Subscribe for updates