[Tutor] Order of unittests

Albert-Jan Roskam sjeik_appie at hotmail.com
Sat Oct 16 04:43:38 EDT 2021


   On 15 Oct 2021 14:53, Peter Otten <__peter__ at web.de> wrote:

     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!
     >

   ===>> Hi Peter,
   Thank you! That looks quite compact, so I'll give that a try on Monday. I
   could even use a lambda for my_dir(). I thought dir() was (re)implemented
   by defining, or in this case injecting, a __dir__ special method?
   Best wishes,
   Albert-Jan


More information about the Tutor mailing list