[Tutor] Order of unittests
Peter Otten
__peter__ at web.de
Fri Oct 15 08:53:44 EDT 2021
On 12/10/2021 09:58, Albert-Jan Roskam wrote:
> Hi,
> How can I write a unittest where tge tesrs are run in the order in which
> they are defined? I tried the code below. I prefer not to change the names
> of the tests.
The following monkey patch seems to work:
# shadow the dir() built-in with an order-preserving analogue
def my_dir(obj):
return list(vars(obj))
unittest.loader.dir = my_dir
# disable sorting
unittest.defaultTestLoader.sortTestMethodsUsing = None
> Thank you in advance!
> Albert-Jan
> (base) albertjan at debian:~/Downloads$ cat sometest.py
> import unittest
>
> unittest.defaultTestLoader.sortTestMethodsUsing = lambda *args: -1
>
> print("wanted order: import, mutate, export, delete")
>
> class SomeTest(unittest.TestCase):
> def test_import(self):
> pass
> def test_mutate(self):
> pass
> def test_export(self):
> pass
> def test_delete(self):
> pass
>
> if __name__ == "__main__":
> unittest.main(verbosity=2)
>
> (base) albertjan at debian:~/Downloads$ python sometest.py
> wanted order: import, mutate, export, delete
> test_mutate (__main__.SomeTest) ... ok
> test_import (__main__.SomeTest) ... ok
> test_export (__main__.SomeTest) ... ok
> test_delete (__main__.SomeTest) ... ok
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list