Run python unittest
by test definition order:
import unittest
import inspect
def cmp(a, b):
return (a > b) - (a < b)
def get_line_no(method_name_str):
return inspect.getsourcelines(getattr(OrderTest, method_name_str))[1]
print()
# noinspection PyUnusedLocal
def reorder(self, x, y):
return cmp(get_line_no(x), get_line_no(y))
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_2_test(self):
print("test_2_test")
def test_3_test(self):
print("test_3_test")
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
collecting ...
collected 6 items
main.py::OrderTest::test_2_test setUpClass
setUp
test_2_test
tearDown
PASSED
main.py::OrderTest::test_3_test setUp
test_3_test
tearDown
PASSED
main.py::OrderTest::test_0_test setUp
test_0_test
tearDown
PASSED
main.py::OrderTest::test_first_test setUp
test_first_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
网友评论