Click here to Skip to main content
13,006,705 members (61,800 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.4K views
Posted 7 Nov 2015

Using pytest Testing Tool to test Python Code by Configuring PyCharm IDE

, 8 Nov 2015
Rate this:
Please Sign up or sign in to vote.
The aim of this article is to bring all the related information of configuring the PyCharm IDE with pytest Testing Tool in one place. Here, I'll not dwell deep into how testing is done in Python. But I'll discuss the proper way of configuring PyCharm IDE with pytest testing

Introduction

The aim of this article is to bring all the related information of configuring the PyCharm IDE with pytest Testing Tool in one place. Here, I’ll not dwell deep into how testing is done in Python. But I'll discuss the proper way of configuring PyCharm IDE with pytest testing tool to get the things up and running. This article is tested on a Windows 10 machine running PyCharm Professional Edition 4.5 with Python 2.7.9 interpreter.

Background

PyCharm is a popular IDE for Python Programming. More information about PyCharm can be obtained at https://www.jetbrains.com/pycharm/.

As a rule, the code we develop should be thoroughly tested before its release. Testing is a very important topic and has been discussed in great detail in the fine material. More information about pytest testing tool can be obtained at http://pytest.org/

To gain more knowledge about testing techniques in general please refer to below books.

  1. Clean Code: A Handbook of Agile Software Craftsmanship
  2. Agile Testing: A Practical Guide for Testers and Agile Teams
  3. Software Testing: A Craftsman’s Approach, Fourth Edition
  4. Pragmatic Unit Testing in Java 8 with JUnit
  5. Practical Unit Testing with JUnit and Mockito
  6. Effective Unit Testing: A guide for Java developers
  7. The Art of Unit Testing: with Examples in .NET

Even though some of these books target other programming languages, still the knowledge that you gain from reading these books can be applied to Python Testing also.

Steps Involved in Configuring PyCharm to work with pytest Testing Tool

Here I assume that you have already configured your PyCharm IDE to run Python Interpreter. If not, you can download anaconda from https://www.continuum.io/downloads and use it to configure the Python interpreter in PyCharm IDE (refer to this article http://bit.ly/1WE5xqn for more information).

Step 1: In order to follow this tutorial, create a folder called PyArticle in C: drive. Install pytest testing tool using pip command as shown below.

1

Step 2: Invoke PyCharm IDE and go to File ⇒ New Project. Specify the path to the folder you created and also specify the name of the project. In our case the name of the project is TestExample. Press Create button. This takes you to the PyCharm editor.

2

Step 3: Right Click on TestExample folder on the left pane of the PyCharm editor. Select New ⇒ Python File

3

Step 4: Give the file name as add_two_numbers

4

Step 5: Write the below given code in add_two_numbers.py file. This is a very simple code that adds up two numbers and returns the result.

5

Step 6: Now go to File ⇒ Settings

6

Step 7: In the Settings window, click on Python Integrated Tools in the right pane and select py.test as the Default test runner. Click on OK button.

7

Step 8: Now right click on TestExample directory and select New ⇒ Directory.

8

Step 9: Enter the directory name as Tests. We will have all our test files in a separate directory which is a good practice.

9

Step 10: Right click on Tests directory and select New ⇒ Python File.

10

Step 11: Give the file name as test_add_two_numbers and click OK.

11

Step 12: Write the below code in test_add_two_numbers.py file. Notice that the word test_ has been prefixed for the test method.

All test methods should start with test_.

This is a very important step in the configuration and if the test method doesn’t start with test_ then you won’t be able to get proper output.

assert is used to check whether the condition is True or False.

12

Step 13: Go to Run -> Edit Configurations.

13

Step 14: In the Run/Debug Configurations window click on the + button found on the upper left corner. Go to Python tests and select py.test. Click OK button.

14

Step 15: On the left pane you can see Python tests is created. In the right pane for the Name text box give the value as py.test in test_add_two_numbers.py You should always prefix the Python test file name with the words py.test in.

For the Target text box specify the path to the Python test file.

For the Working directory text box specify the path to the directory which has the Python test files.

Click on OK button.

15

Step 16: Right click on the Python test file and select Run. As you can see in the Test Results the green color indicates that the test has successfully passed.

16

Step 17: What if you want to specify more than one test method? Lets find out the process to achieve the same. Create another python file called multiply_two_numbers and fill it up with the code shown below.

17

Create another Python test file called test_queue_example.

18

Fill it up with the code shown below.

19

While configuring the Python test file as shown in Step 13 – 15 check the Options check box and give the value as -q. Here q stands for queue.

20

Click on OK button after all the configurations are done.

21

Now right click on the latest test file and run. As you can see one test got passed while the other got failed.

22

Change the code in test_queue_example as shown below and run the test program again. You can see that both the tests have passed.

23

Happy Coding!!!

Points of Interest

Testing our code is a very important aspect of software development. Using testing tools like pytest along with PyCharm IDE goes in a long way in developing quality code.

The post Using pytest Testing Tool to test Python Code by Configuring PyCharm IDE appeared first on Software Artist.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gowrishankarnath
Engineer Dr Ambedkar Institute of Technology
India India
Gowrishankar.S is currently working as Associate Professor in the Department of Computer Science and Engineering at Dr Ambedkar Institute of Technology,Bangalore, Karnataka, India. He has rich experience of serving both Academia and Industry in various capacities. His research interests includes Internet of Things, Data Science and Wireless Communications.

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 8 Nov 2015
Article Copyright 2015 by Gowrishankarnath
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid