avatarRoushanak Rahmat, PhD

Summarize

Python Unit Testing

Image by Michael Dziedzic from Unsplash

What is Unit Testing?

It is a type of software testing where individual units or components of a software application are tested in isolation from the rest of the system. The goal of a unit test is to validate that each unit of the software is working as intended.

Unit testing can be easily done by writing test functions that exercise the code and check that it behaves as expected.

In Python, there are several unit test libraries such as:

  • unittest: is a built-in Python library works similar to JUnit framework in Java.
  • unittest2: is a backport of new features in the unittest library.
  • pytest: is more feature-rich and easier to use than unittest.
  • nose: is a third-party library that extends unittest with additional features such as test discovery.
  • doctest: a built-in Python library which is a lightweight option for simple tests.
  • behave: is a BDD framework for Python. It allows you to write tests in natural language, making them more readable and understandable to non-technical stakeholders.
  • Testify: is a third-party test framework that is an alternative to unittest with more readable and expressive syntax for writing test cases.
  • Lettuce: is a BDD framework for Python that is based on Cucumber. It allows you to write tests in natural language and supports test parameterization and data-driven testing.
  • Robot Framework: is a generic test automation framework for acceptance testing and acceptance test-driven development (ATDD) which can be used for testing various application types including web, mobile and desktop.
  • Selenium: can be used with Python and other programming languages to automate browser interactions and test the functionality of web applications.
  • Hypothesis: is a property-based testing framework which checks the properties of a function or class by generating random inputs.
  • mock: allows replacing parts of your system under test with mock objects and make assertions about how they have been used.
  • trial: used for the Twisted networking library.

Example code using the pytest library

Here is an example of a simple function for classifying animals, called classify_animal(), and a test for that function using the pytest library:

# File: animal_classification.py
def classify_animal(animal):
    if animal == "dog":
        return "mammal"
    elif animal == "snake":
        return "reptile"
    else:
        return "unknown"

# File: test_animal_classification.py
import pytest
import classify_animal

def test_classify_animal():
    assert classify_animal("dog") == "mammal"
    assert classify_animal("snake") == "reptile"
    assert classify_animal("octopus") == "unknown"


if __name__ == '__main__':
    pytest.main()

You can run the test by running the command pytest test_animal_classification.py in your terminal and it will return output as:

============================= test session starts ==============================
Python 3.8.3, pytest-6.1.2, py-1.10.0, pluggy-0.13.1
rootdir: /Desktop/Animal
collected 1 item                                                               

test_animal_classification.py .                                            [100%]

============================== 1 passed in 0.01s ===============================
Python
Python Programming
Unit Testing
Unittest
Pytest
Recommended from ReadMedium