Unit Testing in Python

Let's learn about Pytest and Unittest

Before getting started

What is Unit Testing?

: A repetitive activity of verifying that individual code units within a module or application work as expected

Why Unit Testing is Necessary

  • Helps you think about how to write code

  • Helps determine whether implementation choices are appropriate when deciding what needs to be done

  • Allows you to find bugs quickly and fix them easily

  • Can improve code quality

  • Simplifies integration and improves design

  • Streamlines the debugging process

Python test frameworks

  • Unittest

  • Doctest

  • Pytest

What is Unittest?

  • The default test framework that comes with the Python standard library

  • Not as broad in scope as other third-party test frameworks

    • Provides only the functionality needed to write unit tests suitable for most projects

  • Was influenced by Java's JUnit testing framework

    • Therefore, you cannot write test functions without creating a class for testing

Unittest example

Unittest Fixtures

: Refers to the process of setup before a test is run or clean-up after it finishes

  • setUp()

    • A method called before each test method is invoked

  • setUpClass()

    • A method called only once when the class starts

    • The method must have the @classmethod decorator and cls must be passed as an argument

    • ex)

  • tearDown()

    • A method called after each test finishes

    • Runs even if an exception occurs during the test

    • However, it is only called if the setUp() method was successful

  • tearDownClass()

    • A method called only once after the class finishes

    • Similarly, the method must have the @classmethod decorator and cls must be passed as an argument

    • ex)

What is Pytest?

  • Unlike Unittest, Pytest allows you to create test methods as functions that follow specific naming conventions in a module, rather than requiring a Class

Pytest Fixtures

  • Provides fixtures in a unique way unlike other testing frameworks

  • Makes it easy to identify which test uses which fixture

  • ex)

XFail

  • Allows you to indicate that a test function is expected to fail

    • Can be used when writing tests that are supposed to fail

      • ex) Test code to prevent incorrect data types

  • ex)

Last updated