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. Therefore In this tutorial of “How to,” you will know how to use Pytest in Pycharm and configure it.
What is Pytest?
Pytest is one of the popular testing frameworks for testing or writing the test codes for the database, API e.t.c. It has many advantages over other testing modules. Like it has very simple and easy syntax, can automatically detect tests, run tests in Parallel e.t.c. I am going into detail about Pytest. Here I will only tell you how to configure and use Pytest in Pycharm.
How to use Pytest in Pycharm?
In this section, you will learn step by step to configure and use Pytest in Pycharm.
Step 1: Create a New Virtual Python Interpreter
One of the best practices in Python is to create a Virtual Environment for every new project. It is done to get rid of any conflicts. I am assuming that you have already installed Python 3.xx version for this tutorial. Open your command prompt and type the following command
python -m venv pytest-tutorial
It will create a virtual environment pytest-tutorial for you
Step 2: Activate the Virtual Environment
Use the following command to activate the above-created environment pytest-tutorial. You will see the output like this
Step 3: Install the Pytest
Install the Python Pytest Module using the below command and pytest command to check pytest is properly installed or not.
pip3 install pytest
Step 4: Create or Open an existing Project.
For this tutorial, I am creating a new project and selecting the above created virtual environment. Name your project and click on Project Interpreter and select the above pytest-tutorial environment.
Step 5: Create a Simple Python Test file
In this step, I will first create a simple python test file and run this to test. But before running you have to first add configuration for the Python testing. Click on the add configuration and click on the plus site and select Python Tests>>pytest and add the required field. For example, in Name, I am putting Unit test and in target select the simple_test.py path. After that run the test and see if it failed or pass. If it is showing then you have properly configured the pytest in Pycharm.
def test_AssertTrue(): assert False
def test_AssertTrue(): assert True
That’s all you have successfully configured the pytest in Pycharm.