Python3

Python unittest order 03: Parame

2022-03-25  本文已影响0人  JaedenKil
import unittest

from parameterized import parameterized


# noinspection PyMethodParameters,PyMethodMayBeStatic
def generate():
    return [
        ["JackTest", "Jack", "21"],
        ["AndyTest", "Andy", "25"],
        ["ZedTest", "Zed", "18"],
        ["BradTest", "Brad", "30"]
    ]


def get_test_name_index(method_name_str):
    test_name = method_name_str.split("_")[-1]
    generator = generate()
    for i in range(len(generator)):
        if test_name in generator[i]:
            return i


def cmp(a, b):
    return (a > b) - (a < b)


# noinspection PyUnusedLocal
def reorder(self, x, y):
    return cmp(get_test_name_index(x), get_test_name_index(y))


unittest.TestLoader.sortTestMethodsUsing = reorder


class OrderTest(unittest.TestCase):

    # noinspection PyMethodParameters,PyUnusedLocal
    def custom_name_func(testcase_func, param_num, param):
        return testcase_func.__name__ + '_' + param.args[0]

    @parameterized.expand(generate(), name_func=custom_name_func)
    def test_show(self, test_name, name, age):
        print("Test name: '%s', Name: '%s', age: '%s'" % (test_name, name, age))
python3 -m pytest -vv .\OrderedParameterizedTest.py

OrderedParameterizedTest.py::OrderTest::test_show_JackTest PASSED  [ 25%] 
OrderedParameterizedTest.py::OrderTest::test_show_AndyTest PASSED  [ 50%] 
OrderedParameterizedTest.py::OrderTest::test_show_ZedTest PASSED   [ 75%] 
OrderedParameterizedTest.py::OrderTest::test_show_BradTest PASSED  [100%] 
上一篇下一篇

猜你喜欢

热点阅读