zoukankan      html  css  js  c++  java
  • unittest 测试套件使用汇总篇

    # coding=utf-8
    import unittest
    from inspect import isfunction


    def usage():
    """also unittest.__dict__"""
    print(unittest.__all__)


    def stdout_writeText():
    suite = unittest.TestSuite()
    tests = unittest.TestLoader().loadTestsFromTestCase(TestHetero)
    suite.addTests(tests)
    with open('test_result.txt', 'w', encoding='utf-8') as file:
    runner = unittest.TextTestRunner(stream=file, descriptions='这是第一次执行用例的测试报告!', verbosity=2)
    runner.run(suite)


    def asciisort_members(instance_cls):
    """
    instance_cls is class instance object,return : members method name list of class
    for exp: ['test_001', 'test_002', 'test_Account', 'test_user']

    """
    self = instance_cls
    return (list(filter(lambda m: not m.startswith("__")
    and not m.endswith("__") and m.startswith("test_") and
    callable(getattr(self, m)), dir(self))))


    def unsort_functions(cls_name:object):
    """cls_name is TestClass Name,return test_* function suite"""
    dict_items = list(
    filter(lambda x: not x[0].startswith("__") and x[0].startswith("test_"), cls_name.__dict__.items()))
    functions = [v for k, v in dict_items if isfunction(v)]
    suite = unittest.TestSuite()
    suite.addTests(functions)
    return suite


    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()))


    def getCaseNameList(cls_name):
    """default ascii sort 0-9 A-Z,a-z ,return list """
    return unittest.TestLoader().getTestCaseNames(cls_name)


    def ascii_makeSuite(cls_name):
    """also unittest.makeSuite(cls_name)"""
    return unittest.loader.makeSuite(cls_name)


    def ascii_loaderClass(cls_name):
    """return suite according to className"""
    return unittest.TestLoader().loadTestsFromTestCase(cls_name)


    def ascii_loaderMoudle(moudleName):
    """unittest.TestLoader().loadTestsFromModule(Hetero),Hetero.py is a module"""
    return unittest.TestLoader().loadTestsFromModule(moudleName)


    def loader_method(fun_name: str):
    """ unittest.TestLoader().loadTestsFromName("Hetero.TestHetero.test_user")"""
    return unittest.TestLoader().loadTestsFromName(fun_name)


    def loader_methods(fun_namelist: list):
    """unittest.TestLoader().loadTestsFromNames(["Hetero.TestHetero.test_user","Hetero.TestHetero.test_001"])"""
    return unittest.TestLoader().loadTestsFromNames(fun_namelist)


    def ascii_defaultloader(cls_name):
    return unittest.defaultTestLoader.loadTestsFromTestCase(cls_name)


    def discovers(test_dir):
    """return suite"""
    discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
    return discover


    def suite_addtests(iter_list):
    """[TestHetero("test_001"),TestHetero("test_002")]"""
    suite = unittest.TestSuite()
    suite.addTests(iter_list)
    return suite


    def suite_addtest(func_method):
    """TestHetero("test_001") : format is className("method_name") """
    suite = unittest.TestSuite()
    suite.addTests(func_method)
    return suite


    class TestHetero(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
    pass
    @classmethod
    def tearDownClass(cls):
    pass

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

    def test_user(self):
    print("user")

    def test_001(self):
    print("001")
    def test_002(self):
    print("002")

    if __name__ == '__main__':
    #方法一
    suite=unittest.TestSuite()
    suite.addTests([TestHetero("%s"%value) for value in raw_members(TestHetero)])
    print(suite)
    unittest.TextTestRunner(verbosity=2).run(suite)
    #方法二
    suite2=unsort_functions(TestHetero)
    unittest.TextTestRunner(verbosity=2).run(suite2)

    test_Account (__main__.TestHetero) ... ok
    test_user (__main__.TestHetero) ... ok
    test_001 (__main__.TestHetero) ... ok
    test_002 (__main__.TestHetero) ... ok

    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s

    OK

  • 相关阅读:
    js基础练习题(2)
    js基础练习题(1)
    DOM-BOM-EVENT(7)
    DOM-BOM-EVENT(6)
    DOM-BOM-EVENT(5)
    DOM-BOM-EVENT(4)
    DOM-BOM-EVENT(3)
    Spark学习笔记--Spark在Windows下的环境搭建(转)
    idea下关联spark源码环境(转)
    Spark 性能相关参数配置详解-任务调度篇
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/11882503.html
Copyright © 2011-2022 走看看