Random Test Case Executor

  • Share this:

Code introduction


The function randomly selects and executes a specified number of test cases from a provided list of test cases. It is commonly used to select and execute test cases randomly to verify the robustness and randomness of the code.


Technology Stack : nose, random

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import nose
import random

def random_test_case_generator(test_cases, num_cases=1):
    """
    Generate random test cases from a list of test cases.

    :param test_cases: List of test case functions.
    :param num_cases: Number of test cases to generate.
    :return: Randomly selected test cases.
    """
    selected_cases = random.sample(test_cases, num_cases)
    for case in selected_cases:
        case()

# Usage example:
# Define some test case functions
def test_case_1():
    assert 2 + 2 == 4

def test_case_2():
    assert "hello" == "world"

def test_case_3():
    assert [1, 2, 3] == [1, 2, 3]

# Generate random test cases
random_test_case_generator([test_case_1, test_case_2, test_case_3])                
              
Tags: