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
网友评论