5 Tips to Improve Your Unit Tests in Python

Photo by Nick Fewings on Unsplash

1) @patch.object is your friend

The @patch decorator has some difficulties:

from unittest.mock import patch@patch("folder1.folder2.file1.class1.func1")
def test_example(self, func1):
pass
@patch("folder1.folder2.file1.class1.func1")
def test_example_2(self, func1):
pass
from unittest.mock import patch
from folder1.folder2.file1 import class1
@patch.object(class1, "func1")
def test_example(self, func1):
pass
@patch.object(class1, "func1")
def test_example_2(self, func1):
pass

2) Avoid using global variables

Using global variables in unit tests may result in incorrect test results.

def remove_from_list(name_list, name):
name_list.remove(name)
return name_list


def count_list(name_list):
return len(name_list)
names = ["Michael", "John"]class ExampleTests(TestCase):

def test_remove_from_list(self):
self.assertEqual(remove_from_list(names, "Michael"), ["John"])

def test_count_list(self):
self.assertEqual(count_list(names), 2)
FAILED tests/test_example.py::ExampleTests::test_count_list - AssertionError: 1 != 2
def get_names():
return ["Michael", "John"]


class ExampleTests(TestCase):

def test_remove_from_list(self):
self.assertEqual(remove_from_list(get_names(), "Michael"), ["John"])

def test_count_list(self):
self.assertEqual(count_list(get_names()), 2)
# from copy import copydef test_remove_from_list(self):
self.assertEqual(remove_from_list(copy(names), "Michael"), ["John"])

3) Get rid of unnecessary mock variables

When you create a mock object you must create a function argument

@patch.object(class1, "func1", return_value=["abc"])
def test_example(self, mock_func1):
pass
@patch.object(class1, "func1", Mock(return_value=["abc"]))
def test_example(self):
pass

4) @patch.multiple is your friend

Suppose you will need to create a mock object for two different methods of a class. That’s what @patch.multiple is for

@patch.multiple(
class1,
get_name=Mock(return_value="Michael"),
get_age=Mock(return_value=25),
)
def test_example(self):
self.assertEqual("Michael", class1().get_name())
self.assertEqual(25, class1().get_age())

5) Faker is also your friend

Faker is a Python package that generates fake data for you. In my previous test functions, I ran my test functions with the same strings (Michael and John).

names = [faker.name(), faker.name()]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store