美文网首页Python3
Python unittest order 01

Python unittest order 01

作者: JaedenKil | 来源:发表于2022-01-16 01:26 被阅读0次
    import unittest
    
    
    def cmp(a, b):
        return (a > b) - (a < b)
    
    
    s = {
        'test_0_test': 1,
        'test_first_test': 0,
        'test_last_test': 2,
    }
    
    
    def reorder(self, x, y):
        return cmp(s.get(x, 99), s.get(y, 99))
    
    
    unittest.TestLoader.sortTestMethodsUsing = reorder
    
    
    # noinspection PyMethodMayBeStatic
    class OrderTest(unittest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            print("setUpClass")
    
        @classmethod
        def tearDownClass(cls):
            print("tearDownClass")
    
        def setUp(self):
            print("setUp")
    
        def tearDown(self):
            print("tearDown")
    
        def test_0_test(self):
            print("test_0_test")
    
        def test_first_test(self):
            print("test_first_test")
    
        def test_last_test(self):
            print("test_last_test")
    
        def test_unknown_test(self):
            print("test_unknown_test")
    
    $ python3 -m pytest -s main.py -vv
    =============================================================================================== test session starts =============================================================================================== platform win32 -- Python 3.7.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Python37\python3.exe
    cachedir: .pytest_cache
    rootdir: D:\PythonProject\UnitTest
    collected 4 items
    
    main.py::OrderTest::test_first_test setUpClass
    setUp
    test_first_test
    tearDown
    PASSED
    main.py::OrderTest::test_0_test setUp
    test_0_test
    tearDown
    PASSED
    main.py::OrderTest::test_last_test setUp
    test_last_test
    tearDown
    PASSED
    main.py::OrderTest::test_unknown_test setUp
    test_unknown_test
    tearDown
    PASSEDtearDownClass
    

    相关文章

      网友评论

        本文标题:Python unittest order 01

        本文链接:https://www.haomeiwen.com/subject/nkzxhrtx.html