1. Install Pytest and configure testing

Firstly, let's install the Pytest package. Go to the File → Settings →  Project → Python Interpreter → click + button → open Available Packages and type Pytest in the search field like below:

Select pytest and click Install Package. 

Once the Pytest package is installed, PyCharm detects it and makes it the default project test runner. At any time you can change a test runner in the project settings.

2. Create new test using PyCharm

For tutorial purposes let's create another test for divide function. 

  • From the context menu choose Go To → Test → Create New Test


  • In the Create Test dialog, specify test settings.


  • In the test_operations.py new test called test_divide appeared. Now you can replace the template code with your own ideas.
Template generated by PyCharm
def test_divide():
    assert False


Final test
def test_divide():
    assert divide(10, 5) == 2
    assert divide(200, 1) == 200
    with pytest.raises(ZeroDivisionError):
        divide(1, 0)

3. Run a test

Click the green triangle and select Run 'pytest for test_oper...' to run the test:


PyCharm automatically creates a Pytest Run configuration.


After running the test you can inspect test results:




The scientific work is published for the realization of the international project co-financed by Polish Ministry of Science and Higher Education in 2019 from financial resources of the program entitled "PMW"; Agreement No. 5040/H2020/Euratom/2019/2

This work has been carried out within the framework of the EUROfusion Consortium and has received funding from the Euratom research and training programme 2014–2020 under grant agreement No 633053. The views and opinions expressed herein do not necessarily reflect those of the European Commission or ITER

  • No labels