Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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:

Image Added

Image Added

Select pytest and click Install Package. 

Info

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.

Create new test using PyCharm

For tutorial purposes let's create another test for divide() method. 

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

Image Added

Image Added


  • In the Create Test dialog, specify test settings.

Image Added


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


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

Run a test