Monday, July 27, 2026
HomeLanguagesPython unittest – How to nicely mock a web crawler function?

Python unittest – How to nicely mock a web crawler function?

Prerequisite: Python | Unit Test Objects Patching

Unit Testing is the first level of software testing where the smallest testable parts of the software are tested. This is used to validate that each unit of the software performs as designed. The Unittest framework is python’s xUnit style framework. Before deep-diving into “How to nicely mock a web crawler” let us understand some fundamentals about that.

What is mock?

Mock is a submodule (class) of unittest module. The mock module allows us to replace particular portions of the whole system that we are testing with mock objects.

Steps to be performed:

  • Import mock class from unittest.mock module.
  • Create an instance of the Mock class.
  • Set mock object’s method.
  • Print result

Example:

Let’s understand mock by mimicking another python class. In this example, we will see methods that were called on our mocked class, also what parameters were passed to them.

Python3




# importing mock from unittest.mock module
from unittest.mock import Mock
  
# defining instance of our mock
our_mock = Mock()
  
# defining mock object’s __str__ method
our_mock.__str__ = Mock(return_value='neveropen Mocking Example')
  
# executing str function for output
print(str(our_mock))


Output:

neveropen Mocking Example
Previous article
Next article
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6972 POSTS0 COMMENTS