CodeRef
CodeRef
  • Software
    • VSCode
  • Midjourney_AI
    • Midjourney Cheat Sheet
  • aws
    • S3 Reference
    • Services
  • bash
    • .bashrc File Contents
    • CAN
    • _System Config File Locations
    • argument-length-limits
    • Conditionals
    • Data Structures
    • File Permissions
    • File Syncing
    • File System
    • Functions
    • General
    • Loops
    • My Functions
    • Networking
    • Number Operations
    • OpenVPN
    • Operators
    • Resource Management
    • Serial RS232
    • Spinning Wheel Animation
    • SSH
    • Text Operations
    • Environment Variables
  • cpp
    • ChatGPT | Pointers vs. References
    • arrays
    • Classes
    • Data Types / Structures
    • Enumerated Classes
    • Exception Handling
    • Function Objects
    • Functions
    • I/O
    • Loops
    • Macros
    • Namespaces
    • New Features
    • Pointers
    • Scope
    • Smart Pointers
    • Raw String Literals
    • Style Guide
    • Switch Case
    • Templating
    • How to Use tinyxml2
    • Useful Libraries
    • google-test
    • Conditionals
    • Rule of Three/Five
    • Optional Parameters
    • Keywords
    • Filesystem
    • Random
    • Casting
    • tools
  • git
    • Code Review Dependency Strategy
    • Git Bisect Guide
    • Git Reference
    • removing-cherry-picks
    • Useful Tools
    • Graphite Reference
  • js
    • functions
    • Javascript Reference
  • linux
    • Display
    • Dual Boot with Windows
    • File System
    • NVIDIA
    • Sending/Receiving TCP/UDP Messages in Ubuntu
    • dynamically_linked_binaries
  • markdown
    • Images
    • obsidian-reference
  • python
    • Classes
    • Exceptions
    • Functions
    • Operations
    • Python Reference
    • unittest_command-line-args
    • unittest_magicmock_GPT
    • unittest_mock
    • unittest_printing
    • unittest_subtest
    • useful-stuff
    • jupyter
    • poetry
  • ros
    • _ROS Cheat Sheet
    • Create New Workspace
    • Install ROS
    • Node Sample - Listener
    • Node Sample - Talker
    • Node Template
    • Setup
    • urdf
  • excel
    • excel-reference
  • windows
    • File System
    • WSL - Windows Subsystem for Linux
    • WSL
  • software_engineering
    • uncle_bob_lectures
      • Overview
      • Lesson 01 - Notes
  • web
    • Front End
    • Hugo
    • new_frontend_tools
  • sql
    • cheatsheet
Powered by GitBook
On this page
  1. python

unittest_subtest

If you test multiple conditions within a single test function, rather than repeating code or writing a helper function that runs the same test code with different inputs each time, you can use the subtest feature.

It will loop through each test case and run the same test sequence on each one.

unittest will keep track of which subTest fails and run the others even if one fails.

from my_module import divide_numbers

def test_divide_numbers(self):
    # Tuples of dividend, divisor, quotient, and assertion method.
    test_cases = [
        (4, 2, 2, self.assertEqual),
        (18, 3, 6, self.assertEqual),
        (9, 0, None, self.assertIsNone),
    ]

    for dividend, divisor, quotient, assertion_method in test_cases:
        # If mocking, you can put `mock_return=mock_object.return_value`
        # in the subTest constructor.
        with self.subTest(mock_return=mock_object.return_value):
            result = divide_numbers(dividend, divisor)
            assertion_method(result, quotient)

You can pass any arbitrary keyword args to subTest() to give context. This allows the subtests to list error messages specific to the subtest case.

for dividend, divisor, quotient, assertion_method in test_cases:
    with self.subTest(dividend=dividend,
                      divisor=divisor,
                      quotient=quotient):
        result = divide_numbers(dividend, divisor)
        assertion_method(result, quotient)

Last updated 11 months ago