设计思路:

获取成员变量class.__dict__.keys()

filter过滤符合要求成员,由于3.x成员dict属性是支持有序的

from inspect import isfunction
import unittest
def raw_members(class_name:object): """ get all unsort members test_* name list eg it also work well : list(filter(lambda x: not x.startswith("__") and not x.endswith("__") and x.startswith("test_") ,TestHetero.__dict__.keys())) """ return list(filter(lambda x: not x.startswith("__") and not x.endswith("__") and x.startswith("test_") and isfunction(eval(class_name.__name__+".%s"%x)) and callable(eval(class_name.__name__+".%s"%x )), class_name.__dict__.keys()))
第二步:
制作suite原始顺序添加的suite:

suite=unittest.TestSuite()
suite.addTests([TestHetero("%s"%value) for value in raw_members(TestHetero)])
print(suite)
unittest.TextTestRunner(verbosity=2).run(suite)

结果验证:并没有按照ascii自动排序而是按自己书写顺序执行,改装结束

<unittest.suite.TestSuite tests=[<__main__.TestHetero testMethod=test_Account>, <__main__.TestHetero testMethod=test_user>, <__main__.TestHetero testMethod=test_001>, <__main__.TestHetero testMethod=test_002>]>
test_Account (__main__.TestHetero) ... ok
Account
user
001
test_user (__main__.TestHetero) ... ok
002
test_001 (__main__.TestHetero) ... ok
test_002 (__main__.TestHetero) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK

测试类:

class TestHetero(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        pass
    @classmethod
    def tearDownClass(self):
        pass

    def  test_Account(self):
        print("Account")

    def test_user(self):
        print("user")
    def test_001(self):
        # unittest.TestLoader.getTestCaseNames()
        print("001")
    def test_002(self):

        print("002")

  

01-09 02:56