Randomly Selected Test Case Executor

  • Share this:

Code introduction


This function uses the pytest library to randomly select a test case and run it. It accepts two arguments, arg1 and arg2, where arg1 is used for the test case and arg2 is used for the assertion result. If the test case passes, it returns True; otherwise, it raises an exception.


Technology Stack : Python, pytest

Code Type : The type of code

Code Difficulty : Advanced


                
                    
import random
import pytest

def test_random_function(arg1, arg2):
    """
    This function uses the pytest library to randomly select a test case and run it.
    """
    test_cases = [
        pytest.mark.parametrize("x", [1, 2, 3])(lambda x: x * x),
        pytest.mark.parametrize("x", [0, 1, -1])(lambda x: x + x),
        pytest.mark.parametrize("x", [True, False])(lambda x: x == not x)
    ]

    selected_test = random.choice(test_cases)
    result = selected_test(arg1)
    assert result == arg2                
              
Tags: