Testing your Python code with PyCharm

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

About Dmitry Filippov

Product Marketing Manager at JetBrains
This entry was posted in Cool Feature. Bookmark the permalink.

22 Responses to Testing your Python code with PyCharm

  1. ivanelson says:

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

  2. Cezar says:

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

  3. paul harley says:

    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:

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

      :type request: FixtureRequest

  4. paul harley says:

    …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.

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

  6. Oleksandr says:

    How to configurate pycharm test runner using django-nosetests?

    • Dmitry Filippov says:

      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:

    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” ?

    • Dmitry Filippov says:

      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:

    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 !?


    • mohammed says:

      note : i’m using community version

    • Ernst Haagsman says:

      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:

        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 !

  9. Andres Ocker says:

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

  10. VC says:

    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:

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

Leave a Reply

Your email address will not be published. Required fields are marked *