Python unittest 控制测试用例执行顺序(按声明顺序

2018-11-14  本文已影响0人  Jiafu
# encoding=utf8

"""一个unittest的demo,按函数声明的顺序执行测试用例
"""

import unittest


class TestOrder(unittest.TestCase):
    def test_b(self):
        print 'test_b success'

    def test_a(self):
        print 'test_a success'


if __name__ == '__main__':
    loader = unittest.TestLoader()

    def ln(f):
        return getattr(TestOrder, f).im_func.func_code.co_firstlineno
    loader.sortTestMethodsUsing = lambda a, b: cmp(ln(a), ln(b))
    test_suite1 = loader.loadTestsFromTestCase(TestOrder)
    test_suites = unittest.TestSuite([test_suite1])
    test_runner = unittest.TextTestRunner(verbosity=2)
    test_runner.run(test_suites)


上一篇下一篇

猜你喜欢

热点阅读