Creating unit tests with PyCharm

We have talked about various features when it comes to examining the results from previously created unit tests in PyCharm. However, the support for testing from PyCharm does not stop there, and in this section, we will learn how to create unit tests within PyCharm.

In the same folder that we have been working with, Chapter06/Testing, open the counter.py file, which contains the following code:

import threading
import sys; sys.setswitchinterval(.000001)


class Counter:
def __init__(self, target, num_threads):
self.value = 0
self.target = target
self.num_threads = num_threads

def update(self):
current_value = self.value
self.value = current_value + 1

def run(self):
threads = [threading.Thread(target=self.update)
for _ in range(self.target)]

for t in threads:
t.start()

for t in threads:
t.join()

This file contains a class called Counter, which is a multi-threaded class utilizing the threading module. Note that you don't need to understand this code in depth; we will only be using this class as the starting point to create our tests in PyCharm. With that said, the following are a few more details regarding the Counter class:

  • Upon initialization, the class takes in two integers: target and num_thread.
  • When the run() class method is invoked, it will increment its value field (which starts at 0) until the field reaches the target parameter using the update() class method.
  • The value field will be incremented across multiple threads (the number of which is specified by the num_threads parameter).
  • At the beginning, we are setting the switch interval of the system to 0.000001 seconds. Don't worry about this for now.

Now, we would like to test this class to see whether it is actually able to do the incrementation process we described previously correctly, using unit tests. To do that, perform the following steps:

  1. Move your cursor/caret to the class declaration (line 5 in the original file).
  2. From the menu bar, choose Navigate > Test or evoke the corresponding keyboard shortcut. From here, a small popup window will appear, listing all available tests that involve the Counter class:

Creating tests in PyCharm
  1. As we illustrated previously, there should already be one available test called TestCounter (stored in the test_counter_reference.py file). This is a reference script for the tests that we are attempting to create, so ignore that option for now and go ahead and choose the Create New Test option.
  2. Another popup window will appear, this time listing out the specifications of the test file. In this window, you can customize the test file name, its location, and the name of the test class. Generally, the default options that are automatically filled in by PyCharm are already appropriate.

    The most important option in this window is the Test method section, in which we can choose which methods are to be tested. As you select a specific method in this section, a corresponding boilerplate test method will be generated inside the target test class. For now, we will simply hit the OK button without choosing either of the two methods included.
  3. PyCharm will then generate the test file accordingly and open it in the editor. The code that's included in this file should be similar to the following:
from unittest import TestCase


class TestCounter(TestCase):
pass
  1. The preceding is the skeleton for our test class, which is named TestCounter and inherits from the TestCase superclass from the unittest module. To complete this test file, our job is to design individual test methods in the TestCounter class. In each test method, we need to check whether a Counter object (in the counter.py file) can correctly increment its value to the desired target.

With that in mind, modify your current TestCounter class in the newly created test file to contain the following test methods (you can also reference the test_counter_reference.py file):

from unittest import TestCase
from counter import Counter


class TestCounter(TestCase):

def test_small(self):
small_counter = Counter(5, 5)
small_counter.run()

self.assertEqual(small_counter.value, 5)

def test_med(self):
med_counter = Counter(10, 8)
med_counter.run()

self.assertEqual(med_counter.value, 10)

def test_large(self):
large_counter = Counter(500, 20)
large_counter.run()

self.assertEqual(large_counter.value, 500)

And that is how unit tests are created in PyCharm. We can see that, with the generation of boilerplate code and a code skeleton, PyCharm offers ways for developers to effectively save time from manually creating scripts and entering repetitive code patterns, thus increasing their productivity.

Additionally, there is a theoretical discussion on the test methods we just created for the Counter class, which I include in the next subsection. This discussion does not pertain to PyCharm and its usage, but covers some abstract points about generally designing test cases, as well as specifically in concurrent Python programs.

I recommend going through this if you are interested in learning more about the theory of testing in programming. If, on the other hand, you would like to simply move on to the next main sections, you can do so—we will be discussing the process of debugging in PyCharm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset