How to use Pytest in Pycharm?

Data Scientist Programmers deal with many functions. They create and refactor various functions for performing fast computations. Even if you are a normal programmers you have to refactor the code from time to time. I can definitely say most of you work on the Pycharm. For the best refactoring, you have to use the test cases. and refactoring the code. Thus to Setup and use testing in Pycharm is a major problem.

How to use Pytest in Pycharm?

  • Write the following sample.py code :

def add(a, b):
    return a + b
  • To test the code for the sample.py file, let’s create create a test_sample.py and write the following code:

import sample
import pytest
@pytest.mark.parametrize('num1, num2, result',
                         [
                             (90, 3, 93),
                             ('Hello', ' World', 'Hello World'),
                             (15.5, 25.5, 41)
                         ]
                         )
def test_add(num1, num2, result):
    assert sample.add(num1, num2) == result
  • Install the Pytest in your Pycharm

To install Pytest in your Pycharm: Go to settings -> Project -> Project Interpreter

Install Pytest and after installation you should get like this:

  • Now simply, run your test file and below you will get your test output:

  • Now, if you want to add parameter to your test, go to the top right corner and click on edit configurations:

  • If we want to use verbose simple type -v or any argument in your choice in the Additional Arguments options and the apply :

  • Run once again and you should see the output like this: