Skip to content

Instantly share code, notes, and snippets.

@deangrant
Last active July 27, 2022 06:04
Show Gist options
  • Save deangrant/ab8ba25fbf2e9e7aecac7c638ec5a986 to your computer and use it in GitHub Desktop.
Save deangrant/ab8ba25fbf2e9e7aecac7c638ec5a986 to your computer and use it in GitHub Desktop.
Template code for running test cases in order using the python unit testing framwork
import unittest
# User defined funtion to compare method names when sorting them in order
# to be executed in the unit test.
def unit_test_order():
order = {}
def ordered(f):
order[f.__name__] = len(order)
return f
def compare(a, b):
return [1, -1][order[a] < order[b]]
return ordered, compare
ordered, compare = unit_test_order()
unittest.defaultTestLoader.sortTestMethodsUsing = compare
# Base class for test case to be executed in order.
class SortMethodsTemplate(
unittest.TestCase
)
# Runs first test case
@ordered
def test_run_first(
self
):
...
# Runs second second test case
@ordered
def test_run_second(
self
):
...
if __name__ = '__main__':
unittest.main(
verbosity=2
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment